Files
shumai/packages/transcode/src/workflows/transcode-image.ts
T
Yiling-J 5fe22ac52c feat(transcode): add initial camera RAW image support (#435)
- Add centralized RAW file extension set and detection helper for 23 common camera RAW formats
- Update getProxyType and EXTENSION_MIME_TYPES to route RAW formats into the image pipeline
- Add exiftool-vendored runtime dependency and mark as external in build-npm script
- Implement embedded JPEG extraction to temporary file with priority JpgFromRaw2 -> JpgFromRaw -> PreviewImage
- Read and apply EXIF orientation from RAW container to prevent rotation/mirroring issues
- Integrate into TranscodeService.getImageInfo and transcodeImage with proper temporary file cleanup
- Add hasUsablePreview guard in transcodeImageWorkflow to gracefully handle RAW files without embedded previews
- Add unit, service, workflow, and E2E tests for RAW processing
2026-09-29 21:09:27 +08:00

115 lines
3.2 KiB
TypeScript

import type { WorkflowTask } from '@shumai/db'
import '@shumai/db/src/prisma-json-types'
import { executeActivity, getActivities } from '@shumai/workflow-core'
import {
getWorkerQueueAndStartTask,
fetchAssetWithKey,
completeTask,
failTask,
cleanupTmpDir,
} from './common'
export async function transcodeImageWorkflow(task: WorkflowTask): Promise<void> {
let tmpDir: string | undefined
let workerQueue = ''
try {
workerQueue = await getWorkerQueueAndStartTask(task)
const {
updateAssetStatusActivity,
getMediaInfoActivity,
transcodeImageActivity,
updateAssetMediaActivity,
downloadMediaToTmpActivity,
createEmbeddingTaskIfEnabledActivity,
createAutofillTaskIfEnabledActivity,
} = getActivities()
await executeActivity(workerQueue, updateAssetStatusActivity, {
assetId: task.assetId,
status: 'processing',
})
const { asset, key } = await fetchAssetWithKey(workerQueue, task.assetId)
const download = await executeActivity(workerQueue, downloadMediaToTmpActivity, {
assetKey: key,
})
const { filePath } = download
tmpDir = download.tmpDir
const spec = task.payload?.transcode || {}
const mediaInfo = await executeActivity(workerQueue, getMediaInfoActivity, {
filePath,
assetId: asset.id,
proxyType: 'image',
mediaType: asset.mediaType || '',
})
mediaInfo.original = {
key,
filesizeInBytes: 0,
codec: '',
}
const metadata = mediaInfo.metadata
const hasUsablePreview = metadata && metadata.originalWidth > 0 && metadata.originalHeight > 0
const isImage = mediaInfo.proxyType === 'image'
if (isImage && hasUsablePreview && metadata) {
const imageSpec: PrismaJson.ImageTranscode = {
width: metadata.originalWidth,
height: metadata.originalHeight,
quality: 90,
format: 'webp',
}
const imageTranscode = await executeActivity(workerQueue, transcodeImageActivity, {
assetKey: key,
filePath,
imageSpec,
})
mediaInfo.imageTranscodes.push(imageTranscode)
}
if (spec.thumbnail && hasUsablePreview) {
const thumbTranscode = await executeActivity(workerQueue, transcodeImageActivity, {
assetKey: key,
filePath,
imageSpec: { width: 300, height: 300, quality: 80, format: 'webp', isPreview: true },
})
mediaInfo.thumbnail = thumbTranscode
}
await executeActivity(workerQueue, updateAssetMediaActivity, {
assetId: asset.id,
mediaInfo,
})
await executeActivity(workerQueue, updateAssetStatusActivity, {
assetId: asset.id,
status: 'processed',
})
await executeActivity(workerQueue, createEmbeddingTaskIfEnabledActivity, {
assetId: asset.id,
teamId: task.teamId,
projectId: task.projectId,
})
await executeActivity(workerQueue, createAutofillTaskIfEnabledActivity, {
assetId: asset.id,
teamId: task.teamId,
projectId: task.projectId,
})
await completeTask(workerQueue, task.id)
} catch (err) {
console.error(`transcodeImageWorkflow failed for task ${task.id}:`, err)
await failTask(workerQueue, task.id, err)
throw err
} finally {
await cleanupTmpDir(workerQueue, tmpDir)
}
}