fix(upload): correct .wma MIME type to audio prefix on upload and add tests

This commit is contained in:
yiling
2026-07-05 15:57:29 +08:00
parent 8d336836cc
commit ccfcdce23c
4 changed files with 51 additions and 4 deletions
+33
View File
@@ -388,4 +388,37 @@ describe('UploadService', () => {
expect(result.data[0].id).toBe(task.id)
expect(result.data[0].name).toBe('test-upload-task')
})
it('should correct the mediaType for .wma files that browser incorrectly reports as video', async () => {
const req = {
parentId: parentId,
files: [
{
name: 'song.wma',
id: '1',
size: 100,
type: 'file' as const,
mediaType: 'video/x-ms-wma',
children: [],
},
{
name: 'other_song.WMA',
id: '2',
size: 200,
type: 'file' as const,
mediaType: 'video/x-ms-asf',
children: [],
},
],
}
const resp = await uploadService.createUploadTask(userId, req)
expect(resp.taskId).toBeDefined()
const song1 = await prisma.asset.findFirst({ where: { name: 'song.wma' } })
expect(song1?.mediaType).toBe('audio/x-ms-wma')
const song2 = await prisma.asset.findFirst({ where: { name: 'other_song.WMA' } })
expect(song2?.mediaType).toBe('audio/x-ms-asf')
})
})
+6 -1
View File
@@ -134,13 +134,18 @@ export class UploadService {
key = `files/${ulid()}/${sanitizeFilename(file.name)}`
}
let mediaType = file.mediaType
if (mediaType && file.name.toLowerCase().endsWith('.wma') && mediaType.startsWith('video/')) {
mediaType = mediaType.replace(/^video\//, 'audio/')
}
const newAsset = await tx.asset.create({
data: {
name: file.name,
type: assetType,
storageKey: key ? { create: { key } } : undefined,
sortIndex: newSortIndex,
mediaType: file.mediaType,
mediaType: mediaType,
status: assetType === AssetType.folder ? AssetStatus.uploaded : AssetStatus.uploading,
sizeByte: file.size,
creator: userId ? { connect: { id: userId } } : undefined,
@@ -513,7 +513,10 @@ export function FileBrowser({
fileCount: 0,
type: 'file',
status: hasUrl ? 'uploading' : 'error',
mediaType: f.file.type || null,
mediaType:
f.file.name.toLowerCase().endsWith('.wma') && f.file.type?.startsWith('video/')
? f.file.type.replace(/^video\//, 'audio/')
: f.file.type || null,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
}
@@ -846,13 +849,17 @@ export function FileBrowser({
}
const fileWithId = { file, id: ulid() }
fileWithIds.push(fileWithId)
let mediaType = file.type
if (fileName.toLowerCase().endsWith('.wma') && mediaType?.startsWith('video/')) {
mediaType = mediaType.replace(/^video\//, 'audio/')
}
currentLevel.push({
name: fileName,
size: file.size,
type: 'file',
id: fileWithId.id,
children: [],
mediaType: file.type,
mediaType,
})
}
filesToUpload = fileWithIds
+3 -1
View File
@@ -139,7 +139,9 @@ export const longAudioVideoAsset: AssetInfo = {
export const SAMPLE_AUDIO_URL = '/sample-audio.mp4'
export const SAMPLE_AUDIO_DURATION = 3.0
export const SAMPLE_AUDIO_COMPUTED_TOTAL_FRAMES = Math.round(SAMPLE_AUDIO_DURATION * SAMPLE_FRAME_RATE)
export const SAMPLE_AUDIO_COMPUTED_TOTAL_FRAMES = Math.round(
SAMPLE_AUDIO_DURATION * SAMPLE_FRAME_RATE,
)
export const sampleAudioAsset: AssetInfo = {
...sampleVideoAsset,