feat(asset): replace mediaType/mimeType logic with proxyType

This commit is contained in:
yiling
2026-07-20 10:22:36 +08:00
parent c45a1d9ee9
commit f158d04686
40 changed files with 219 additions and 119 deletions
+29 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { detectSupportedMimeType } from './mime'
import { detectSupportedMimeType, getProxyType } from './mime'
describe('detectSupportedMimeType', () => {
it('should detect JPEG', () => {
@@ -39,3 +39,31 @@ describe('detectSupportedMimeType', () => {
expect(detectSupportedMimeType(buffer)).toBeNull()
})
})
describe('getProxyType', () => {
it('should detect image proxyType', () => {
expect(getProxyType('image/png', 'test.png')).toBe('image')
expect(getProxyType('image/jpeg', 'photo.jpg')).toBe('image')
})
it('should detect video proxyType', () => {
expect(getProxyType('video/mp4', 'clip.mp4')).toBe('video')
})
it('should detect audio proxyType', () => {
expect(getProxyType('audio/mpeg', 'song.mp3')).toBe('audio')
})
it('should detect pdf proxyType for pdf, csv, txt, and doc files', () => {
expect(getProxyType('application/pdf', 'doc.pdf')).toBe('pdf')
expect(getProxyType('text/plain', 'notes.txt')).toBe('pdf')
expect(getProxyType('text/csv', 'data.csv')).toBe('pdf')
expect(getProxyType('application/msword', 'letter.doc')).toBe('pdf')
expect(getProxyType(null, 'file.txt')).toBe('pdf')
})
it('should return null for unsupported files', () => {
expect(getProxyType('application/zip', 'archive.zip')).toBeNull()
expect(getProxyType(null, 'unknown.bin')).toBeNull()
})
})
+37
View File
@@ -1,3 +1,40 @@
export type ProxyType = 'image' | 'video' | 'audio' | 'pdf'
export function getProxyType(
mediaType?: string | null,
filename?: string | null,
): ProxyType | null {
const lowerMediaType = mediaType?.toLowerCase() || ''
const lowerFilename = filename?.toLowerCase() || ''
if (lowerMediaType.startsWith('image/')) return 'image'
if (lowerMediaType.startsWith('video/')) return 'video'
if (lowerMediaType.startsWith('audio/')) return 'audio'
if (
lowerMediaType === 'application/pdf' ||
lowerMediaType === 'text/plain' ||
lowerMediaType === 'text/csv' ||
lowerMediaType.includes('msword') ||
lowerMediaType.includes('officedocument') ||
lowerMediaType.includes('vnd.ms-') ||
lowerMediaType.startsWith('text/') ||
lowerFilename.endsWith('.pdf') ||
lowerFilename.endsWith('.txt') ||
lowerFilename.endsWith('.csv') ||
lowerFilename.endsWith('.doc') ||
lowerFilename.endsWith('.docx') ||
lowerFilename.endsWith('.xls') ||
lowerFilename.endsWith('.xlsx') ||
lowerFilename.endsWith('.ppt') ||
lowerFilename.endsWith('.pptx')
) {
return 'pdf'
}
return null
}
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
export function detectSupportedMimeType(buffer: Uint8Array): string | null {