From 6f6625bc0ddf2b24f69a25a79e008b4635bb92be Mon Sep 17 00:00:00 2001 From: xiaowang <2161656029@qq.com> Date: Sun, 8 Nov 2026 22:54:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=AB=AF=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=EF=BC=9A=E5=85=AC=E5=91=8A=E8=B6=85=E8=BF=87?= =?UTF-8?q?=205MB=20=E8=87=AA=E5=8A=A8=E5=88=87=E6=8D=A2=E5=88=86=E7=89=87?= =?UTF-8?q?=EF=BC=8C=E5=8E=BB=E9=99=A4=205MB=20=E6=8F=90=E5=89=8D=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=EF=BC=8C=E4=BB=85=E5=9C=A8=E5=88=86=E7=89=87=E6=80=BB?= =?UTF-8?q?=E9=87=8F=E8=B6=85=20500MB=20=E6=97=B6=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/utils/chunk-upload.test.ts | 29 ++++++++++++++++++- JC-Salon-Frontend/src/utils/chunk-upload.ts | 22 ++++++++++++++ .../src/utils/heic-image.test.ts | 9 ------ JC-Salon-Frontend/src/utils/heic-image.ts | 14 ++------- .../src/views/admin/AnnouncementsView.vue | 23 +++++++++++++-- .../src/views/admin/ShopsView.vue | 10 +++---- .../src/views/admin/SystemMessagesView.vue | 6 ++-- 7 files changed, 80 insertions(+), 33 deletions(-) diff --git a/JC-Salon-Frontend/src/utils/chunk-upload.test.ts b/JC-Salon-Frontend/src/utils/chunk-upload.test.ts index f91fdffc..f42203da 100644 --- a/JC-Salon-Frontend/src/utils/chunk-upload.test.ts +++ b/JC-Salon-Frontend/src/utils/chunk-upload.test.ts @@ -1,11 +1,24 @@ import { describe, expect, it, vi } from 'vitest' -import { createChunkUploadTask, type ChunkUploadApi } from './chunk-upload' +import { + assertChunkUploadFileSize, + CHUNK_UPLOAD_TOO_LARGE_MESSAGE, + MAX_CHUNK_UPLOAD_TOTAL_SIZE, + createChunkUploadTask, + shouldUseChunkUpload, + type ChunkUploadApi +} from './chunk-upload' const createFile = (size: number) => new File([new Uint8Array(size)], 'large.png', { type: 'image/png', lastModified: 1 }) +const createFileWithSize = (size: number) => { + const file = createFile(1) + Object.defineProperty(file, 'size', { value: size }) + return file +} + describe('chunk-upload', () => { it('uploads chunks with the requested concurrency', async () => { const releaseCallbacks: Array<() => void> = [] @@ -81,4 +94,18 @@ describe('chunk-upload', () => { expect.any(Object) ) }) + + it('小文件走直传,超过 5MB 走分片', () => { + const fiveMb = 5 * 1024 * 1024 + + expect(shouldUseChunkUpload(createFileWithSize(fiveMb - 1))).toBe(false) + expect(shouldUseChunkUpload(createFileWithSize(fiveMb))).toBe(false) + expect(shouldUseChunkUpload(createFileWithSize(fiveMb + 1))).toBe(true) + }) + + it('分片总量超过 500MB 才提示', () => { + expect(() => assertChunkUploadFileSize(createFileWithSize(MAX_CHUNK_UPLOAD_TOTAL_SIZE))).not.toThrow() + expect(() => assertChunkUploadFileSize(createFileWithSize(MAX_CHUNK_UPLOAD_TOTAL_SIZE + 1))) + .toThrow(CHUNK_UPLOAD_TOO_LARGE_MESSAGE) + }) }) diff --git a/JC-Salon-Frontend/src/utils/chunk-upload.ts b/JC-Salon-Frontend/src/utils/chunk-upload.ts index 1cfc3bf1..a6e7b963 100644 --- a/JC-Salon-Frontend/src/utils/chunk-upload.ts +++ b/JC-Salon-Frontend/src/utils/chunk-upload.ts @@ -1,4 +1,26 @@ import type { AxiosProgressEvent } from 'axios' +import { MAX_IMAGE_UPLOAD_SIZE } from '@/utils/image-upload' +import { convertHeicToJpegFile } from '@/utils/heic-image' + +/** 单文件超过直传上限(Nacos max-file-size 5MB)时改走分片上传。 */ +export const shouldUseChunkUpload = (file: File) => file.size > MAX_IMAGE_UPLOAD_SIZE + +/** 分片任务总量上限,与后端 UploadSecurityProperties.maxTotalSize 一致。 */ +export const MAX_CHUNK_UPLOAD_TOTAL_SIZE = 500 * 1024 * 1024 +export const CHUNK_UPLOAD_TOO_LARGE_MESSAGE = '图片超过 500MB 上限,请压缩后上传' + +export const assertChunkUploadFileSize = (file: File) => { + if (file.size > MAX_CHUNK_UPLOAD_TOTAL_SIZE) { + throw new Error(CHUNK_UPLOAD_TOO_LARGE_MESSAGE) + } +} + +/** 分片前的统一预处理:HEIC/HEIF 先转 JPEG,再校验分片总量上限。 */ +export const prepareChunkUploadFile = async (file: File) => { + const prepared = await convertHeicToJpegFile(file) + assertChunkUploadFileSize(prepared) + return prepared +} export interface ChunkUploadSession { uploadId: string diff --git a/JC-Salon-Frontend/src/utils/heic-image.test.ts b/JC-Salon-Frontend/src/utils/heic-image.test.ts index d59afe34..7fdc66ab 100644 --- a/JC-Salon-Frontend/src/utils/heic-image.test.ts +++ b/JC-Salon-Frontend/src/utils/heic-image.test.ts @@ -9,12 +9,9 @@ vi.mock('heic2any', () => ({ import { convertHeicToJpegFile, HEIC_CONVERT_FAILED_MESSAGE, - HEIC_CONVERT_TOO_LARGE_MESSAGE, isHeicFile } from './heic-image' -const FIVE_MB = 5 * 1024 * 1024 - const createHeicFile = (name = 'live.heic', type = 'image/heic') => new File( [new Uint8Array(8)], name, @@ -66,10 +63,4 @@ describe('heic-image', () => { await expect(convertHeicToJpegFile(createHeicFile())).rejects.toThrow(HEIC_CONVERT_FAILED_MESSAGE) }) - - it('转换后超过 5MB 时抛出超限提示', async () => { - convertMock.mockResolvedValue(new Blob([new Uint8Array(FIVE_MB + 1)])) - - await expect(convertHeicToJpegFile(createHeicFile())).rejects.toThrow(HEIC_CONVERT_TOO_LARGE_MESSAGE) - }) }) diff --git a/JC-Salon-Frontend/src/utils/heic-image.ts b/JC-Salon-Frontend/src/utils/heic-image.ts index 42c9d183..332200cc 100644 --- a/JC-Salon-Frontend/src/utils/heic-image.ts +++ b/JC-Salon-Frontend/src/utils/heic-image.ts @@ -1,10 +1,7 @@ -import { MAX_IMAGE_UPLOAD_SIZE } from '@/utils/image-upload' - const HEIC_EXTENSIONS = ['.heic', '.heif'] const HEIC_MIME_TYPES = new Set(['image/heic', 'image/heif', 'image/heic-sequence', 'image/heif-sequence']) export const HEIC_CONVERT_FAILED_MESSAGE = '该图片格式无法转换,请导出为 JPG 后再上传' -export const HEIC_CONVERT_TOO_LARGE_MESSAGE = '转换后的图片超过 5MB 上限,请压缩后重试' type HeicConverter = (options: { blob: Blob @@ -38,12 +35,9 @@ const loadHeicConverter = async () => { /** * 把 HEIC/HEIF 静态帧转成 JPEG 的 File,非 HEIC/HEIF 原样返回。 - * 转换失败或转换后超过 5MB 上限时抛出带明确提示的 Error,调用方不得继续上传。 + * 转换失败时抛出带明确提示的 Error,调用方不得继续上传。 */ -export const convertHeicToJpegFile = async ( - file: File, - maxSize = MAX_IMAGE_UPLOAD_SIZE -): Promise => { +export const convertHeicToJpegFile = async (file: File): Promise => { if (!isHeicFile(file)) { return file } @@ -66,9 +60,5 @@ export const convertHeicToJpegFile = async ( lastModified: file.lastModified }) - if (convertedFile.size > maxSize) { - throw new Error(HEIC_CONVERT_TOO_LARGE_MESSAGE) - } - return convertedFile } diff --git a/JC-Salon-Frontend/src/views/admin/AnnouncementsView.vue b/JC-Salon-Frontend/src/views/admin/AnnouncementsView.vue index 95514641..daf6afec 100644 --- a/JC-Salon-Frontend/src/views/admin/AnnouncementsView.vue +++ b/JC-Salon-Frontend/src/views/admin/AnnouncementsView.vue @@ -79,7 +79,7 @@ import { message, Modal } from 'ant-design-vue' */ import { adminAnnouncementApi, adminFileApi, type AnnouncementVO, type MessageTargetOptionsVO } from '@/api/admin' import { extractRichText, renderRichContent } from '@/utils/rich-content' -import { convertHeicToJpegFile } from '@/utils/heic-image' +import { createChunkUploadTask, prepareChunkUploadFile, shouldUseChunkUpload } from '@/utils/chunk-upload' /** loading is a reactive state variable used by the template and event handlers. */ const loading = ref(false) @@ -158,11 +158,28 @@ const openCreate = () => { formOpen.value = true } +const uploadAnnouncementImageInChunks = async (file: File) => { + const { response } = await createChunkUploadTask({ + file, + scene: 'admin-announcement-image', + concurrency: 3, + api: { + initUpload: adminFileApi.initChunkUpload, + uploadChunk: adminFileApi.uploadChunk, + completeUpload: adminFileApi.completeChunkUpload, + cancelUpload: adminFileApi.cancelChunkUpload + } + }).promise + return response +} + const uploadAnnouncementImage = async (file: File) => { imageUploading.value = true try { - const uploadCandidate = await convertHeicToJpegFile(file) - const uploaded = await adminFileApi.uploadFile(uploadCandidate) + const uploadCandidate = await prepareChunkUploadFile(file) + const uploaded = shouldUseChunkUpload(uploadCandidate) + ? await uploadAnnouncementImageInChunks(uploadCandidate) + : await adminFileApi.uploadFile(uploadCandidate) if (uploaded?.id) form.imageFileIds.push(uploaded.id) message.success('图片已上传,提交后会进入审核') } catch (error) { diff --git a/JC-Salon-Frontend/src/views/admin/ShopsView.vue b/JC-Salon-Frontend/src/views/admin/ShopsView.vue index 7ae37ea2..dd465ecd 100644 --- a/JC-Salon-Frontend/src/views/admin/ShopsView.vue +++ b/JC-Salon-Frontend/src/views/admin/ShopsView.vue @@ -646,8 +646,8 @@ import { adminShopApi, adminFileApi, adminSecureIntegrationConfigApi, type Shop * Purpose: ChunkUploadTask supports ShopsView.vue by holding or executing the chunk upload task page or API role. * Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow. */ -import { createChunkUploadTask, type ChunkUploadTask } from '@/utils/chunk-upload' -import { convertHeicToJpegFile, isHeicFile } from '@/utils/heic-image' +import { createChunkUploadTask, prepareChunkUploadFile, type ChunkUploadTask } from '@/utils/chunk-upload' +import { isHeicFile } from '@/utils/heic-image' import { buildDraftStorageKey, clearDraftRecord, loadDraftRecord, saveDraftRecord } from '@/utils/form-draft' import { usePermission } from '@/utils/permission' import { runWithGlobalLoading } from '@/utils/action-loading' @@ -1782,7 +1782,7 @@ const handleUploadBanner: UploadProps['customRequest'] = async (options) => { file: uploadFile, scene: 'admin-shop-banner', concurrency: 3, - preprocessFile: convertHeicToJpegFile, + preprocessFile: prepareChunkUploadFile, api: { initUpload: adminFileApi.initChunkUpload, uploadChunk: adminFileApi.uploadChunk, @@ -1864,7 +1864,7 @@ const handleUploadImage: UploadProps['customRequest'] = async (options) => { file: uploadFile, scene: 'admin-shop-image', concurrency: 3, - preprocessFile: convertHeicToJpegFile, + preprocessFile: prepareChunkUploadFile, api: { initUpload: adminFileApi.initChunkUpload, uploadChunk: adminFileApi.uploadChunk, @@ -1946,7 +1946,7 @@ const handleUploadThemeBackground: UploadProps['customRequest'] = async (options file: uploadFile, scene: 'admin-shop-theme-background', concurrency: 3, - preprocessFile: convertHeicToJpegFile, + preprocessFile: prepareChunkUploadFile, api: { initUpload: adminFileApi.initChunkUpload, uploadChunk: adminFileApi.uploadChunk, diff --git a/JC-Salon-Frontend/src/views/admin/SystemMessagesView.vue b/JC-Salon-Frontend/src/views/admin/SystemMessagesView.vue index 27fcf526..ffe0ca35 100644 --- a/JC-Salon-Frontend/src/views/admin/SystemMessagesView.vue +++ b/JC-Salon-Frontend/src/views/admin/SystemMessagesView.vue @@ -169,8 +169,8 @@ import SystemMessageCard from '@/components/SystemMessageCard.vue' * Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow. */ import { adminFileApi, adminSystemMessageApi, type SystemMessageCreateRequest, type SystemMessageVO } from '@/api/admin' -import { createChunkUploadTask } from '@/utils/chunk-upload' -import { convertHeicToJpegFile, isHeicFile } from '@/utils/heic-image' +import { createChunkUploadTask, prepareChunkUploadFile } from '@/utils/chunk-upload' +import { isHeicFile } from '@/utils/heic-image' import type { IdType } from '@/utils/id' /** @@ -403,7 +403,7 @@ const handleImageUpload = async (options: any) => { file, scene: 'admin-system-message-image', concurrency: 3, - preprocessFile: convertHeicToJpegFile, + preprocessFile: prepareChunkUploadFile, api: { initUpload: adminFileApi.initChunkUpload, uploadChunk: adminFileApi.uploadChunk,