✨ feat(upload): 支持后台配置上传分片大小
This commit is contained in:
1 parent
5b78ec6fbc
commit
272741ee49
11 files changed
+64
-5
No files matched your search
@@ -8,6 +8,9 @@
|
|||||||
:id="inputId"
|
:id="inputId"
|
||||||
type="number"
|
type="number"
|
||||||
:value="modelValue"
|
:value="modelValue"
|
||||||
|
:min="min"
|
||||||
|
:max="max"
|
||||||
|
:step="step"
|
||||||
class="min-h-11 w-28 rounded-md border px-4 py-2.5 shadow-sm outline-none transition-all duration-200 focus:border-indigo-500 focus:ring-2 focus:ring-indigo-500"
|
class="min-h-11 w-28 rounded-md border px-4 py-2.5 shadow-sm outline-none transition-all duration-200 focus:border-indigo-500 focus:ring-2 focus:ring-indigo-500"
|
||||||
:class="[
|
:class="[
|
||||||
isDarkMode
|
isDarkMode
|
||||||
@@ -24,7 +27,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, inject } from 'vue'
|
import { computed, inject } from 'vue'
|
||||||
|
|
||||||
const props = defineProps<{ label: string; modelValue: number; suffix: string }>()
|
const props = defineProps<{ label: string; modelValue: number; suffix: string; min?: number; max?: number; step?: number }>()
|
||||||
const inputId = computed(() => `setting-number-${props.label.toLowerCase().replace(/[^a-z0-9\u4e00-\u9fa5]+/gi, '-')}`)
|
const inputId = computed(() => `setting-number-${props.label.toLowerCase().replace(/[^a-z0-9\u4e00-\u9fa5]+/gi, '-')}`)
|
||||||
const isDarkMode = inject('isDarkMode')
|
const isDarkMode = inject('isDarkMode')
|
||||||
const emit = defineEmits<{ 'update:modelValue': [value: number] }>()
|
const emit = defineEmits<{ 'update:modelValue': [value: number] }>()
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ export function useSendFlow() {
|
|||||||
const sentRecordActions = createSentRecordActions(notifyCopyResult)
|
const sentRecordActions = createSentRecordActions(notifyCopyResult)
|
||||||
const { requestCancel, needsCleanup, retryCleanup, isUploadInterrupted, resetPresignUpload, submitFile, submitText } = useSendSubmit({
|
const { requestCancel, needsCleanup, retryCleanup, isUploadInterrupted, resetPresignUpload, submitFile, submitText } = useSendSubmit({
|
||||||
getMaxFileSize: () => configStore.uploadSizeLimit,
|
getMaxFileSize: () => configStore.uploadSizeLimit,
|
||||||
|
getChunkSizeMB: () => config.value.chunkSizeMB,
|
||||||
notify: (message, type) => alertStore.showAlert(message, type),
|
notify: (message, type) => alertStore.showAlert(message, type),
|
||||||
translate: t,
|
translate: t,
|
||||||
onProgress: (progress) => {
|
onProgress: (progress) => {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ type Translate = (
|
|||||||
|
|
||||||
type UseSendSubmitOptions = {
|
type UseSendSubmitOptions = {
|
||||||
getMaxFileSize: () => number
|
getMaxFileSize: () => number
|
||||||
|
getChunkSizeMB: () => number
|
||||||
notify: (message: string, type: AlertType) => void
|
notify: (message: string, type: AlertType) => void
|
||||||
translate: Translate
|
translate: Translate
|
||||||
onProgress: (progress: UploadProgress) => void
|
onProgress: (progress: UploadProgress) => void
|
||||||
@@ -51,6 +52,7 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
|||||||
expireValue,
|
expireValue,
|
||||||
expireStyle,
|
expireStyle,
|
||||||
cancellation: active,
|
cancellation: active,
|
||||||
|
chunkSizeMB: options.getChunkSizeMB(),
|
||||||
onHashCalculated: options.onHashCalculated,
|
onHashCalculated: options.onHashCalculated,
|
||||||
onFinalizing: options.onFinalizing,
|
onFinalizing: options.onFinalizing,
|
||||||
onProgress: (progress: UploadProgress) => {
|
onProgress: (progress: UploadProgress) => {
|
||||||
|
|||||||
@@ -161,6 +161,11 @@ export function useSystemConfig() {
|
|||||||
return Promise.resolve(false)
|
return Promise.resolve(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const size = config.value.chunkSizeMB
|
||||||
|
if (!Number.isInteger(size) || size < 1 || size > 100) {
|
||||||
|
alertStore.showAlert('分片大小必须是 1–100 MB 的整数', 'error')
|
||||||
|
return Promise.resolve(false)
|
||||||
|
}
|
||||||
return updateConfig(buildSubmitPayload())
|
return updateConfig(buildSubmitPayload())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ export const CHUNK_SIZE = 90 * 1024 * 1024
|
|||||||
|
|
||||||
type ChunkedUploadOptions = {
|
type ChunkedUploadOptions = {
|
||||||
cancellation?: UploadCancellation
|
cancellation?: UploadCancellation
|
||||||
|
chunkSizeMB?: number
|
||||||
expireValue: number
|
expireValue: number
|
||||||
expireStyle: string
|
expireStyle: string
|
||||||
onFinalizing?: () => void
|
onFinalizing?: () => void
|
||||||
@@ -68,7 +69,7 @@ const runChunkedFile = async (
|
|||||||
const initResponse = await FileService.initChunkUpload({
|
const initResponse = await FileService.initChunkUpload({
|
||||||
file_name: file.name,
|
file_name: file.name,
|
||||||
file_size: file.size,
|
file_size: file.size,
|
||||||
chunk_size: Math.min(CHUNK_SIZE, file.size),
|
chunk_size: Math.min((Number.isInteger(options.chunkSizeMB) && options.chunkSizeMB! >= 1 && options.chunkSizeMB! <= 100 ? options.chunkSizeMB! * 1024 * 1024 : CHUNK_SIZE), file.size),
|
||||||
file_hash: fileHash
|
file_hash: fileHash
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ export interface ConfigState {
|
|||||||
trashRetentionDays: number
|
trashRetentionDays: number
|
||||||
opacity: number
|
opacity: number
|
||||||
enableChunk: number
|
enableChunk: number
|
||||||
|
chunkSizeMB: number
|
||||||
s3_access_key_id: string
|
s3_access_key_id: string
|
||||||
background: string
|
background: string
|
||||||
showAdminAddr: number
|
showAdminAddr: number
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export type PublicConfig = SystemConfig & {
|
|||||||
openUpload: number
|
openUpload: number
|
||||||
max_save_seconds: number
|
max_save_seconds: number
|
||||||
enableChunk: number
|
enableChunk: number
|
||||||
|
chunkSizeMB: number
|
||||||
notify_title?: string
|
notify_title?: string
|
||||||
notify_content?: string
|
notify_content?: string
|
||||||
page_explain?: string
|
page_explain?: string
|
||||||
@@ -22,7 +23,8 @@ export const DEFAULT_PUBLIC_CONFIG: PublicConfig = {
|
|||||||
expireStyle: ['day'],
|
expireStyle: ['day'],
|
||||||
openUpload: 1,
|
openUpload: 1,
|
||||||
max_save_seconds: 0,
|
max_save_seconds: 0,
|
||||||
enableChunk: 0
|
enableChunk: 0,
|
||||||
|
chunkSizeMB: 90
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_CONFIG_STATE: ConfigState = {
|
export const DEFAULT_CONFIG_STATE: ConfigState = {
|
||||||
@@ -44,6 +46,7 @@ export const DEFAULT_CONFIG_STATE: ConfigState = {
|
|||||||
trashRetentionDays: 30,
|
trashRetentionDays: 30,
|
||||||
opacity: 0.9,
|
opacity: 0.9,
|
||||||
enableChunk: DEFAULT_PUBLIC_CONFIG.enableChunk,
|
enableChunk: DEFAULT_PUBLIC_CONFIG.enableChunk,
|
||||||
|
chunkSizeMB: DEFAULT_PUBLIC_CONFIG.chunkSizeMB,
|
||||||
s3_access_key_id: '',
|
s3_access_key_id: '',
|
||||||
background: '',
|
background: '',
|
||||||
showAdminAddr: 0,
|
showAdminAddr: 0,
|
||||||
@@ -85,6 +88,7 @@ export function toPublicConfig(config: Partial<ConfigState> | null | undefined):
|
|||||||
openUpload: config.openUpload,
|
openUpload: config.openUpload,
|
||||||
max_save_seconds: config.max_save_seconds,
|
max_save_seconds: config.max_save_seconds,
|
||||||
enableChunk: config.enableChunk,
|
enableChunk: config.enableChunk,
|
||||||
|
chunkSizeMB: config.chunkSizeMB ?? DEFAULT_PUBLIC_CONFIG.chunkSizeMB,
|
||||||
notify_title: config.notify_title,
|
notify_title: config.notify_title,
|
||||||
notify_content: config.notify_content,
|
notify_content: config.notify_content,
|
||||||
page_explain: config.page_explain,
|
page_explain: config.page_explain,
|
||||||
|
|||||||
@@ -378,7 +378,7 @@ onMounted(() => {
|
|||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<SettingSwitch
|
<SettingSwitch
|
||||||
v-if="config.file_storage === 'local'"
|
v-if="config.file_storage === 'local' || config.file_storage === 'webdav'"
|
||||||
:label="t('manage.settings.chunkUploadNote')"
|
:label="t('manage.settings.chunkUploadNote')"
|
||||||
:model-value="config.enableChunk"
|
:model-value="config.enableChunk"
|
||||||
:enabled-text="t('common.enabled')"
|
:enabled-text="t('common.enabled')"
|
||||||
@@ -627,6 +627,11 @@ onMounted(() => {
|
|||||||
</h3>
|
</h3>
|
||||||
|
|
||||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||||
|
<div class="space-y-2">
|
||||||
|
<SettingNumberInput v-model="config.chunkSizeMB" label="上传分片大小" suffix="MB" :min="1" :max="100" :step="1" />
|
||||||
|
<p class="text-xs leading-5" :class="isDarkMode ? 'text-gray-400' : 'text-gray-500'">1–100 MB,默认 90 MB。保存后用于新上传,已开始的上传保留原分片大小。仅启用分片上传时生效。</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<SettingNumberInput
|
<SettingNumberInput
|
||||||
v-model="config.uploadMinute"
|
v-model="config.uploadMinute"
|
||||||
:label="t('manage.settings.uploadPerMinute')"
|
:label="t('manage.settings.uploadPerMinute')"
|
||||||
|
|||||||
@@ -83,3 +83,9 @@ Playwright Page。Vite 固定运行于 `127.0.0.1:5180`;分别传入 `http`
|
|||||||
发送页“中断上传”按钮和清理反馈,不会访问线上文件。
|
发送页“中断上传”按钮和清理反馈,不会访问线上文件。
|
||||||
`historyStatus.test.mjs` 验证回收站原因共用标签、已清理、恢复后的现存状态。
|
`historyStatus.test.mjs` 验证回收站原因共用标签、已清理、恢复后的现存状态。
|
||||||
分片使用原始请求体发送,最大 100 MiB,避免 multipart 额外开销超过 CDN 上限。
|
分片使用原始请求体发送,最大 100 MiB,避免 multipart 额外开销超过 CDN 上限。
|
||||||
|
|
||||||
|
## 可配置分片大小
|
||||||
|
|
||||||
|
`uploadStrategy.integration.test.mjs` 验证配置决定新会话请求大小,服务端返回的
|
||||||
|
会话大小决定续传切片。`chunkSizeSettings.browser.mjs` 验证真实设置页面拒绝无效值、
|
||||||
|
保存/刷新后保留以及发送页读取公共配置;全部 API 拦截为隔离数据。
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import assert from 'node:assert/strict'
|
||||||
|
// Isolated app/API fixture. Never changes deployed configuration.
|
||||||
|
export async function runChunkSizeSettingsCheck(page, base='http://127.0.0.1:5180/theme-assets/') {
|
||||||
|
let size=90, saves=0, requested
|
||||||
|
await page.addInitScript(()=>localStorage.setItem('token','fixture-only-token'))
|
||||||
|
await page.route('**/api/v1/config',r=>r.fulfill({json:{code:200,detail:{config:{name:'fixture',expireStyle:['day'],max_save_seconds:604800,enableChunk:1,chunkSizeMB:size,uploadSize:1073741824,openUpload:1}}}}))
|
||||||
|
await page.route('**/admin/verify',r=>r.fulfill({json:{code:200,detail:{is_admin:true}}}))
|
||||||
|
await page.route('**/admin/config/get',r=>r.fulfill({json:{code:200,detail:{name:'fixture',file_storage:'webdav',chunkSizeMB:size,enableChunk:1,uploadSize:1073741824,max_save_seconds:604800,expireStyle:['day'],openUpload:1}}}))
|
||||||
|
await page.route('**/admin/config/update',r=>{size=r.request().postDataJSON().chunkSizeMB;saves++;return r.fulfill({json:{code:200}})})
|
||||||
|
await page.route('**/chunk/upload/init/**',r=>{requested=Number(new URLSearchParams(r.request().postData()).get('chunk_size'));return r.fulfill({json:{code:400,message:'stop after checking configured size'}})})
|
||||||
|
await page.goto(base+'#/admin/settings',{waitUntil:'networkidle'})
|
||||||
|
const field=page.getByRole('spinbutton',{name:'上传分片大小',exact:true});await field.waitFor()
|
||||||
|
assert.equal(await field.inputValue(),'90')
|
||||||
|
await field.fill('0');await page.getByRole('button',{name:'保存设置',exact:true}).click();await page.waitForTimeout(100);assert.equal(saves,0)
|
||||||
|
await field.fill('2');await page.getByRole('button',{name:'保存设置',exact:true}).click();await page.waitForTimeout(200);assert.equal(size,2);assert.equal(saves,1)
|
||||||
|
await page.reload({waitUntil:'networkidle'});assert.equal(await field.inputValue(),'2')
|
||||||
|
await page.goto(base+'#/send',{waitUntil:'networkidle'});await page.locator('input[type=file]').setInputFiles({name:'configured.txt',mimeType:'text/plain',buffer:Buffer.alloc(3*1024*1024,97)})
|
||||||
|
await page.locator('button[type=submit]').click();await page.waitForTimeout(500);assert.equal(requested,2*1024*1024)
|
||||||
|
console.log('settings: reject invalid, save/reload 2MB, new upload requests 2MB: passed')
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict'
|
|||||||
import {createServer} from 'vite'
|
import {createServer} from 'vite'
|
||||||
let server,upload,service
|
let server,upload,service
|
||||||
before(async()=>{
|
before(async()=>{
|
||||||
server=await createServer({server:{middlewareMode:true,watch:null},appType:'custom'})
|
server=await createServer({optimizeDeps:{noDiscovery:true,include:[]},server:{middlewareMode:true,watch:null},appType:'custom'})
|
||||||
service=(await server.ssrLoadModule('/src/services/file.ts')).FileService
|
service=(await server.ssrLoadModule('/src/services/file.ts')).FileService
|
||||||
upload=(await server.ssrLoadModule('/src/services/upload-strategy.ts')).uploadChunkedFile
|
upload=(await server.ssrLoadModule('/src/services/upload-strategy.ts')).uploadChunkedFile
|
||||||
})
|
})
|
||||||
@@ -28,3 +28,14 @@ test('small file initialization never requests a chunk bigger than the file',asy
|
|||||||
service.initChunkUpload=async request=>{size=request.chunk_size;return {code:200,detail:{existed:true,code:'existing'}}}
|
service.initChunkUpload=async request=>{size=request.chunk_size;return {code:200,detail:{existed:true,code:'existing'}}}
|
||||||
try{await upload(new File(['small'],'a.txt'),{expireValue:1,expireStyle:'day'});assert.equal(size,5)}finally{service.initChunkUpload=originalInit;globalThis.window=previousWindow}
|
try{await upload(new File(['small'],'a.txt'),{expireValue:1,expireStyle:'day'});assert.equal(size,5)}finally{service.initChunkUpload=originalInit;globalThis.window=previousWindow}
|
||||||
})
|
})
|
||||||
|
test('configured chunk size determines new requests; server session size controls resumed slicing',async()=>{
|
||||||
|
const original={init:service.initChunkUpload,chunk:service.uploadChunk,complete:service.completeChunkUpload};const previousWindow=globalThis.window;globalThis.window={isSecureContext:true}
|
||||||
|
let requested;const slices=[]
|
||||||
|
service.initChunkUpload=async request=>{requested=request.chunk_size;return {code:200,detail:{upload_id:'configured',chunk_size:1048576,uploaded_chunks:[0]}}}
|
||||||
|
service.uploadChunk=async(id,index,blob)=>{slices.push([index,blob.size]);return {code:200}}
|
||||||
|
service.completeChunkUpload=async()=>({code:200,detail:{code:'CONFIG01'}})
|
||||||
|
try{
|
||||||
|
await upload(new File([new Uint8Array(3*1048576+1)],'configured.bin'),{expireValue:1,expireStyle:'day',chunkSizeMB:2})
|
||||||
|
assert.equal(requested,2*1048576);assert.deepEqual(slices,[[1,1048576],[2,1048576],[3,1]])
|
||||||
|
}finally{service.initChunkUpload=original.init;service.uploadChunk=original.chunk;service.completeChunkUpload=original.complete;globalThis.window=previousWindow}
|
||||||
|
})
|
||||||
Reference in new issue
Block a user