mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 20:29:28 +00:00
feat(asset): replace mediaType/mimeType logic with proxyType
This commit is contained in:
@@ -2000,8 +2000,10 @@ export class AssetService {
|
||||
private async toPreviewInfo(asset: Asset | AssetWithIncludes): Promise<PreviewInfo | null> {
|
||||
if (!asset.media) return null
|
||||
|
||||
const proxyType = (asset.media?.proxyType || null) as 'image' | 'video' | 'audio' | 'pdf' | null
|
||||
|
||||
let thumbnailUrl = undefined
|
||||
if (asset.mediaType?.startsWith('image/') && asset.media.thumbnail?.key) {
|
||||
if (proxyType === 'image' && asset.media.thumbnail?.key) {
|
||||
thumbnailUrl = await s3Service.presign(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
asset.media.thumbnail.key,
|
||||
@@ -2024,8 +2026,6 @@ export class AssetService {
|
||||
)
|
||||
}
|
||||
|
||||
const proxyType = (asset.media?.proxyType || null) as 'image' | 'video' | 'audio' | 'pdf' | null
|
||||
|
||||
return {
|
||||
mediaType: asset.mediaType,
|
||||
proxyType,
|
||||
@@ -2057,11 +2057,18 @@ export class AssetService {
|
||||
a.asset.storageKey.key,
|
||||
'GET',
|
||||
)
|
||||
const attachmentProxyType = (a.asset.media?.proxyType || null) as
|
||||
| 'image'
|
||||
| 'video'
|
||||
| 'audio'
|
||||
| 'pdf'
|
||||
| null
|
||||
attachments.push({
|
||||
id: a.id,
|
||||
assetId: a.asset.id,
|
||||
url,
|
||||
mediaType: a.asset.mediaType,
|
||||
proxyType: attachmentProxyType,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,6 +181,7 @@ describe('ChatService', () => {
|
||||
name: 'doc.jpg',
|
||||
type: 'file',
|
||||
mediaType: 'image/jpeg',
|
||||
media: { proxyType: 'image' },
|
||||
status: 'processed',
|
||||
projectId: project.id,
|
||||
parentId: childFolder.id,
|
||||
|
||||
@@ -232,7 +232,8 @@ export class ChatService {
|
||||
where: { id: fileId },
|
||||
include: { storageKey: true },
|
||||
})
|
||||
if (file && file.mediaType?.startsWith('image/') && file.storageKey?.key) {
|
||||
const isImage = (file?.media as PrismaJson.MediaInfo | null)?.proxyType === 'image'
|
||||
if (file && isImage && file.storageKey?.key) {
|
||||
resolvedImageUrls.push(file.storageKey.key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,6 +402,7 @@ export class NotificationService {
|
||||
id: a.id,
|
||||
name: a.name,
|
||||
mediaType: a.mediaType || undefined,
|
||||
proxyType: (a.media as PrismaJson.MediaInfo | null)?.proxyType || undefined,
|
||||
preview: previewUrl || undefined,
|
||||
thumbnailUrl: thumbnailUrl || undefined,
|
||||
originalWidth: originalWidth || undefined,
|
||||
|
||||
@@ -22,6 +22,7 @@ import { ImageTranscoder, PdfTranscoder, VideoTranscoder } from '@shumai/transco
|
||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||
import { ulid } from 'ulid'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
import { getProxyType } from '@shumai/core/src/utils/mime'
|
||||
|
||||
export class UploadService {
|
||||
constructor(private readonly prismaClient: typeof prisma = prisma) {}
|
||||
@@ -300,16 +301,14 @@ export class UploadService {
|
||||
},
|
||||
})
|
||||
|
||||
const isVideo = asset.mediaType?.startsWith('video/')
|
||||
const isImage = asset.mediaType?.startsWith('image/')
|
||||
const isAudio = asset.mediaType?.startsWith('audio/')
|
||||
const isPdf =
|
||||
asset.mediaType === 'application/pdf' ||
|
||||
asset.mediaType === 'text/plain' ||
|
||||
asset.mediaType === 'text/csv' ||
|
||||
asset.name.toLowerCase().endsWith('.pdf') ||
|
||||
asset.name.toLowerCase().endsWith('.txt') ||
|
||||
asset.name.toLowerCase().endsWith('.csv')
|
||||
const proxyType =
|
||||
(asset.media as PrismaJson.MediaInfo | null)?.proxyType ||
|
||||
getProxyType(asset.mediaType, asset.name)
|
||||
|
||||
const isVideo = proxyType === 'video'
|
||||
const isImage = proxyType === 'image'
|
||||
const isAudio = proxyType === 'audio'
|
||||
const isPdf = proxyType === 'pdf'
|
||||
|
||||
if (autofillAgent && (isVideo || isImage)) {
|
||||
await tx.workflowTask.create({
|
||||
@@ -327,7 +326,7 @@ export class UploadService {
|
||||
})
|
||||
}
|
||||
|
||||
if (!isVideo && !isImage && !isAudio && !isPdf) {
|
||||
if (!proxyType) {
|
||||
await tx.asset.update({
|
||||
where: { id: asset.id },
|
||||
data: { status: AssetStatus.processed },
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user