管理端图片上传:公告超过 5MB 自动切换分片,去除 5MB 提前拦截,仅在分片总量超 500MB 时提示
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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<File> => {
|
||||
export const convertHeicToJpegFile = async (file: File): Promise<File> => {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user