mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 06:16:35 +00:00
Merge pull request #72 from shumaiOne/feat/fix-file-type-population
fix: automatically populate file_type system metadata field
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
} from './transcode'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { transcodeService } from '../transcode'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
|
||||
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
s3Service: {
|
||||
@@ -17,6 +18,7 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||
putObject: vi.fn(),
|
||||
headObject: vi.fn(),
|
||||
presign: vi.fn(),
|
||||
downloadToFile: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
@@ -32,11 +34,20 @@ vi.mock('../transcode', () => ({
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@shumai/core/src/metadata/metadata', () => ({
|
||||
metadataService: {
|
||||
updateAssetMetadata: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('fs', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('fs')>()
|
||||
return {
|
||||
...actual,
|
||||
readFileSync: vi.fn().mockReturnValue(Buffer.from('fake data')),
|
||||
existsSync: vi.fn().mockReturnValue(true),
|
||||
mkdirSync: vi.fn(),
|
||||
unlinkSync: vi.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
@@ -47,7 +58,7 @@ describe('Transcode Activities', () => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('should call getVideoInfo for video assets', async () => {
|
||||
it('should call getVideoInfo and set file_type to video', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'v.mp4', type: 'file', status: 'uploaded' },
|
||||
})
|
||||
@@ -74,15 +85,14 @@ describe('Transcode Activities', () => {
|
||||
|
||||
expect(transcodeService.getVideoInfo).toHaveBeenCalledWith('/tmp/v.mp4')
|
||||
expect(result.duration).toBe(10)
|
||||
expect(result.metadata?.originalWidth).toBe(1920)
|
||||
expect(result.metadata?.videoCodec).toBe('Advanced Video Coding')
|
||||
expect(result.metadata?.audioCodec).toBe('MPEG-4 Audio')
|
||||
expect(result.metadata?.audioChannels).toBe(2)
|
||||
expect(result.metadata?.audioSampleRate).toBe(48000)
|
||||
expect(result.metadata?.audioBitDepth).toBe(16)
|
||||
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
||||
asset.id,
|
||||
expect.arrayContaining([{ key: 'file_type', value: 'video' }]),
|
||||
true,
|
||||
)
|
||||
})
|
||||
|
||||
it('should call getImageInfo for image assets', async () => {
|
||||
it('should call getImageInfo and set file_type to image', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'i.png', type: 'file', status: 'uploaded' },
|
||||
})
|
||||
@@ -96,15 +106,72 @@ describe('Transcode Activities', () => {
|
||||
mimeType: 'image/png',
|
||||
})
|
||||
|
||||
const result = await getMediaInfoActivity({
|
||||
await getMediaInfoActivity({
|
||||
assetId: asset.id,
|
||||
filePath: '/tmp/i.png',
|
||||
mediaType: 'image/png',
|
||||
})
|
||||
|
||||
expect(transcodeService.getImageInfo).toHaveBeenCalledWith('/tmp/i.png')
|
||||
expect(result.metadata?.originalWidth).toBe(800)
|
||||
expect(result.metadata?.duration).toBe(0)
|
||||
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
||||
asset.id,
|
||||
expect.arrayContaining([{ key: 'file_type', value: 'image' }]),
|
||||
true,
|
||||
)
|
||||
})
|
||||
|
||||
it('should set file_type to audio for audio files', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'a.mp3', type: 'file', status: 'uploaded' },
|
||||
})
|
||||
|
||||
await getMediaInfoActivity({
|
||||
assetId: asset.id,
|
||||
filePath: '/tmp/a.mp3',
|
||||
mediaType: 'audio/mpeg',
|
||||
})
|
||||
|
||||
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
||||
asset.id,
|
||||
expect.arrayContaining([{ key: 'file_type', value: 'audio' }]),
|
||||
true,
|
||||
)
|
||||
})
|
||||
|
||||
it('should set file_type to document for pdf/text files', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'd.pdf', type: 'file', status: 'uploaded' },
|
||||
})
|
||||
|
||||
await getMediaInfoActivity({
|
||||
assetId: asset.id,
|
||||
filePath: '/tmp/d.pdf',
|
||||
mediaType: 'application/pdf',
|
||||
})
|
||||
|
||||
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
||||
asset.id,
|
||||
expect.arrayContaining([{ key: 'file_type', value: 'document' }]),
|
||||
true,
|
||||
)
|
||||
})
|
||||
|
||||
it('should set file_type to file for unknown types', async () => {
|
||||
const asset = await prisma.asset.create({
|
||||
data: { name: 'u.xyz', type: 'file', status: 'uploaded' },
|
||||
})
|
||||
|
||||
await getMediaInfoActivity({
|
||||
assetId: asset.id,
|
||||
filePath: '/tmp/u.xyz',
|
||||
mediaType: 'application/octet-stream',
|
||||
})
|
||||
|
||||
expect(metadataService.updateAssetMetadata).toHaveBeenCalledWith(
|
||||
asset.id,
|
||||
expect.arrayContaining([{ key: 'file_type', value: 'file' }]),
|
||||
true,
|
||||
)
|
||||
})
|
||||
|
||||
it('should update asset media field', async () => {
|
||||
|
||||
@@ -19,6 +19,23 @@ export async function getMediaInfoActivity(params: {
|
||||
}): Promise<PrismaJson.MediaInfo> {
|
||||
const isVideo = params.mediaType.startsWith('video/')
|
||||
const isImage = params.mediaType.startsWith('image/')
|
||||
const isAudio = params.mediaType.startsWith('audio/')
|
||||
const isDocument =
|
||||
params.mediaType.startsWith('text/') ||
|
||||
params.mediaType === 'application/pdf' ||
|
||||
params.mediaType.includes('msword') ||
|
||||
params.mediaType.includes('officedocument') ||
|
||||
params.mediaType.includes('vnd.ms-')
|
||||
|
||||
const fileType = isVideo
|
||||
? 'video'
|
||||
: isAudio
|
||||
? 'audio'
|
||||
: isImage
|
||||
? 'image'
|
||||
: isDocument
|
||||
? 'document'
|
||||
: 'file'
|
||||
|
||||
const mediaInfo: PrismaJson.MediaInfo = {
|
||||
duration: 0,
|
||||
@@ -39,6 +56,10 @@ export async function getMediaInfoActivity(params: {
|
||||
},
|
||||
}
|
||||
|
||||
const metadataUpdates: { key: string; value: string | number }[] = [
|
||||
{ key: 'file_type', value: fileType },
|
||||
]
|
||||
|
||||
if (isVideo) {
|
||||
const info = await transcodeService.getVideoInfo(params.filePath)
|
||||
mediaInfo.duration = info.duration
|
||||
@@ -57,13 +78,13 @@ export async function getMediaInfoActivity(params: {
|
||||
audioBitDepth: info.audioBitDepth,
|
||||
format: {},
|
||||
}
|
||||
const metadataUpdates: { key: string; value: string | number }[] = [
|
||||
metadataUpdates.push(
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
{ key: 'duration', value: info.duration },
|
||||
{ key: 'bitRate', value: info.bitRate / 1000 },
|
||||
{ key: 'frame_rate', value: info.frameRate },
|
||||
]
|
||||
)
|
||||
if (info.videoCodec) metadataUpdates.push({ key: 'video_codec', value: info.videoCodec })
|
||||
if (info.audioCodec) metadataUpdates.push({ key: 'audio_codec', value: info.audioCodec })
|
||||
if (info.audioChannels !== undefined)
|
||||
@@ -72,8 +93,6 @@ export async function getMediaInfoActivity(params: {
|
||||
metadataUpdates.push({ key: 'audio_sample_rate', value: info.audioSampleRate })
|
||||
if (info.audioBitDepth !== undefined)
|
||||
metadataUpdates.push({ key: 'audio_bit_depth', value: info.audioBitDepth })
|
||||
|
||||
await metadataService.updateAssetMetadata(params.assetId, metadataUpdates, true)
|
||||
} else if (isImage) {
|
||||
const info = await transcodeService.getImageInfo(params.filePath)
|
||||
mediaInfo.metadata = {
|
||||
@@ -85,16 +104,14 @@ export async function getMediaInfoActivity(params: {
|
||||
hasAudio: false,
|
||||
format: {},
|
||||
}
|
||||
await metadataService.updateAssetMetadata(
|
||||
params.assetId,
|
||||
[
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
],
|
||||
true,
|
||||
metadataUpdates.push(
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
)
|
||||
}
|
||||
|
||||
await metadataService.updateAssetMetadata(params.assetId, metadataUpdates, true)
|
||||
|
||||
return mediaInfo
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user