diff --git a/frontend/src/pages/HomePage.vue b/frontend/src/pages/HomePage.vue index 7688a67..f1e47bc 100644 --- a/frontend/src/pages/HomePage.vue +++ b/frontend/src/pages/HomePage.vue @@ -20,8 +20,13 @@ interface UploadItem { const auth = useAuthStore() +type CompressionMode = 'percent' | 'size' + const options = reactive({ + mode: 'percent' as CompressionMode, // 压缩模式:百分比 / 目标大小 compressionRate: 60, + targetSize: '' as string, // 目标大小数值 + targetUnit: 'KB' as 'KB' | 'MB', // 目标大小单位 maxWidth: '' as string, maxHeight: '' as string, }) @@ -105,18 +110,33 @@ function toInt(v: string): number | undefined { return Math.floor(n) } +function getTargetSizeBytes(): number | undefined { + if (options.mode !== 'size') return undefined + const size = Number(options.targetSize) + if (!Number.isFinite(size) || size <= 0) return undefined + return options.targetUnit === 'MB' ? size * 1024 * 1024 : size * 1024 +} + async function runOne(item: UploadItem) { item.status = 'compressing' item.error = undefined try { + const compressOptions = options.mode === 'percent' + ? { + compression_rate: options.compressionRate, + max_width: toInt(options.maxWidth), + max_height: toInt(options.maxHeight), + } + : { + target_size_bytes: getTargetSizeBytes(), + max_width: toInt(options.maxWidth), + max_height: toInt(options.maxHeight), + } + const result = await compressFile( item.file, - { - compression_rate: options.compressionRate, - max_width: toInt(options.maxWidth), - max_height: toInt(options.maxHeight), - }, + compressOptions, auth.token, ) @@ -460,7 +480,31 @@ async function resendVerification() {
压缩参数
-