mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 18:36:52 +00:00
* feat(webui): remove original-file display fallbacks, use proxies only WebUI now never uses the raw original file for display. All viewers (image, video, compare, pdf) render exclusively from transcoded proxies; when no proxy exists the viewer shows an unavailable placeholder instead of falling back to media.original. - getBestTranscode: drop the isRaw fallback candidates - video-viewer / compare-video-pane: remove the synthetic 'Original' resolution entry and the original.downloadUrl hasMedia gate - image-viewer / compare-image-pane: remove original.downloadUrl default - pdf-viewer: require pdfTranscode.url (no original fallback) - download menus (video-control-bar, breadcrumb-nav): use media.original.key for the 'Original' download option instead of the isRaw transcode marker entry Backend: - transcode-video workflow no longer pushes the isRaw:true marker entry into videoTranscodes (it pointed at the original file key) - remove media.original.downloadUrl from the DTO, asset service mapping, MediaInfo DB type, and transcode workflows/activities (it was only consumed by the removed display fallbacks; downloads already use key via the download-url endpoints) Tests/fixtures updated; docs updated to state the proxy-only display policy. * refactor: remove isRaw field via data migration, clean legacy media JSON Add a Prisma data migration that cleans legacy media JSONB on both assets and watermark_files: - drop the raw-original marker entries (isRaw: true) from videoTranscodes / imageTranscodes — they pointed at the original file key and were only used by the removed display fallbacks / download menus, which now use media.original.key directly - strip the isRaw field from remaining transcode entries - drop original.downloadUrl (removed from the AssetInfo DTO) The migration is generated via prisma migrate dev --create-only and applied automatically by migrate deploy in production. It is guarded by a test (packages/db/src/clean-media-migration.test.ts) that seeds legacy media JSON, runs the migration SQL, and asserts the transform on both tables. With the data cleaned, remove the isRaw field entirely from the codebase: - DTO schemas + ImageTranscode/VideoTranscode interfaces - PrismaJson MediaInfo transcode types - watermark activity isRaw skip (and its test), watermark proxy mapping, agent-embedding transcode selection, webui transcode filters, and all test fixtures AGENTS.md: clarify that the no-manual-migration rule applies to DDL; data migrations (pure SQL transforms, no schema change) are allowed inside a --create-only generated migration and must be tested.
403 lines
12 KiB
TypeScript
403 lines
12 KiB
TypeScript
import { prisma, WatermarkFileStatus } from '@shumai/db'
|
|
import '@shumai/db/src/prisma-json-types'
|
|
import { setupTestDbHooks } from '@shumai/db/test'
|
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
|
import { transcodeService } from '@shumai/core/src/transcode/transcode'
|
|
import {
|
|
initWatermarkFileActivity,
|
|
waitForWatermarkFileActivity,
|
|
transcodeWatermarkMediaActivity,
|
|
completeWatermarkFileActivity,
|
|
} from './watermark'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
vi.mock('child_process', () => ({
|
|
execFile: vi.fn(
|
|
(_cmd: string, args: string[], cb: (err: unknown, stdout: string, stderr: string) => void) => {
|
|
const outFile = args[args.length - 1]
|
|
if (outFile && typeof outFile === 'string') {
|
|
try {
|
|
fs.writeFileSync(outFile, 'fake-transcoded-output')
|
|
} catch {
|
|
// ignore directory creation errors in mock
|
|
}
|
|
}
|
|
cb(null, '', '')
|
|
},
|
|
),
|
|
}))
|
|
|
|
vi.mock('@shumai/core/src/s3/s3', () => ({
|
|
s3Service: {
|
|
getObject: vi.fn(),
|
|
putObject: vi.fn(),
|
|
presign: vi.fn(),
|
|
downloadToFile: vi.fn().mockImplementation(async (_b, _k, dest) => {
|
|
fs.writeFileSync(dest, 'fake-raw')
|
|
}),
|
|
downloadMediaToTmp: vi.fn().mockImplementation(async () => ({
|
|
filePath: '/tmp/raw-file',
|
|
tmpDir: '/tmp',
|
|
})),
|
|
deleteObject: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
vi.mock('@shumai/core/src/transcode/transcode', () => ({
|
|
transcodeService: {
|
|
getVideoInfo: vi.fn(),
|
|
getImageInfo: vi.fn(),
|
|
createTempDir: vi.fn().mockReturnValue('/tmp'),
|
|
removeDir: vi.fn(),
|
|
renderSvgToPng: vi.fn().mockResolvedValue(Buffer.from('fake-overlay-png')),
|
|
downscaleImageToPng: vi.fn().mockResolvedValue({
|
|
buffer: Buffer.from('fake-block-png'),
|
|
width: 32,
|
|
height: 32,
|
|
}),
|
|
compositeOverlayToWebpFile: vi.fn().mockImplementation(async (_in, _overlay, out) => {
|
|
fs.writeFileSync(out, 'fake-webp')
|
|
}),
|
|
transcodeVideo: vi.fn().mockImplementation(async (params) => {
|
|
fs.writeFileSync(params.outputFile, 'fake-mp4')
|
|
}),
|
|
},
|
|
}))
|
|
|
|
describe('Watermark Activities', () => {
|
|
setupTestDbHooks()
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
describe('initWatermarkFileActivity', () => {
|
|
async function seedAssetAndConfig() {
|
|
const asset = await prisma.asset.create({
|
|
data: { name: 'wm-target.png', type: 'file', status: 'processed' },
|
|
})
|
|
const config = await prisma.watermarkConfig.create({
|
|
data: {
|
|
hash: 'init-hash-' + Math.random(),
|
|
config: { blocks: [] },
|
|
},
|
|
})
|
|
return { asset, config }
|
|
}
|
|
|
|
it('creates a processing row when none exists', async () => {
|
|
const { asset, config } = await seedAssetAndConfig()
|
|
const res = await initWatermarkFileActivity({
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
})
|
|
expect(res.action).toBe('created')
|
|
const row = await prisma.watermarkFile.findUnique({
|
|
where: {
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
assetId_watermarkConfigId: {
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
},
|
|
},
|
|
})
|
|
expect(row?.status).toBe(WatermarkFileStatus.processing)
|
|
})
|
|
|
|
it('returns completed action when a completed watermark file exists', async () => {
|
|
const { asset, config } = await seedAssetAndConfig()
|
|
await prisma.watermarkFile.create({
|
|
data: {
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
status: WatermarkFileStatus.completed,
|
|
},
|
|
})
|
|
const res = await initWatermarkFileActivity({
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
})
|
|
expect(res.action).toBe('completed')
|
|
})
|
|
|
|
it('returns processing action when a processing row exists', async () => {
|
|
const { asset, config } = await seedAssetAndConfig()
|
|
await prisma.watermarkFile.create({
|
|
data: {
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
status: WatermarkFileStatus.processing,
|
|
},
|
|
})
|
|
const res = await initWatermarkFileActivity({
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
})
|
|
expect(res.action).toBe('processing')
|
|
})
|
|
|
|
it('returns failed action when a failed row exists', async () => {
|
|
const { asset, config } = await seedAssetAndConfig()
|
|
await prisma.watermarkFile.create({
|
|
data: {
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
status: WatermarkFileStatus.failed,
|
|
},
|
|
})
|
|
const res = await initWatermarkFileActivity({
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
})
|
|
expect(res.action).toBe('failed')
|
|
})
|
|
|
|
it('waits for watermark file completion in waitForWatermarkFileActivity', async () => {
|
|
const { asset, config } = await seedAssetAndConfig()
|
|
const wf = await prisma.watermarkFile.create({
|
|
data: {
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
status: WatermarkFileStatus.processing,
|
|
},
|
|
})
|
|
|
|
setTimeout(async () => {
|
|
await prisma.watermarkFile.update({
|
|
where: { id: wf.id },
|
|
data: { status: WatermarkFileStatus.completed },
|
|
})
|
|
}, 50)
|
|
|
|
const res = await waitForWatermarkFileActivity({
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
})
|
|
expect(res.status).toBe(WatermarkFileStatus.completed)
|
|
})
|
|
})
|
|
|
|
describe('transcodeWatermarkMediaActivity', () => {
|
|
const bucket = process.env.S3_BUCKET || 'shumai'
|
|
|
|
async function seedAsset(name: string, mediaType: string, key: string, proxyType: string) {
|
|
const storageKey = await prisma.storageKey.create({ data: { key } })
|
|
return prisma.asset.create({
|
|
data: {
|
|
name,
|
|
type: 'file',
|
|
mediaType,
|
|
status: 'processed',
|
|
storageKeyId: storageKey.id,
|
|
media: {
|
|
duration: 0,
|
|
filesize: 0,
|
|
frames: 0,
|
|
proxyType,
|
|
videoTranscodes: [{ key, width: 100, height: 100, resolution: '100p' }],
|
|
imageTranscodes: [{ key, width: 100, height: 100, quality: 90, format: 'webp' }],
|
|
videoPreview: { width: 100, height: 100 },
|
|
finishedAt: new Date().toISOString(),
|
|
metadata: {
|
|
originalWidth: 100,
|
|
originalHeight: 100,
|
|
duration: 0,
|
|
bitRate: 0,
|
|
frameRate: 0,
|
|
totalFrames: 0,
|
|
startTimecode: '00:00:00:00',
|
|
hasAudio: false,
|
|
format: {},
|
|
},
|
|
original: {
|
|
key,
|
|
filesizeInBytes: 0,
|
|
codec: '',
|
|
},
|
|
} as PrismaJson.MediaInfo,
|
|
},
|
|
})
|
|
}
|
|
|
|
async function seedConfig() {
|
|
return prisma.watermarkConfig.create({
|
|
data: {
|
|
config: {
|
|
blocks: [
|
|
{
|
|
id: 'b1',
|
|
type: 'text',
|
|
x: 0.5,
|
|
y: 0.5,
|
|
opacity: 0.5,
|
|
rotation: 0,
|
|
text: 'TEST WM',
|
|
size: 0.2,
|
|
color: '#FF0000',
|
|
},
|
|
],
|
|
},
|
|
hash: 'test-hash-' + Math.random(),
|
|
},
|
|
})
|
|
}
|
|
|
|
it('produces a watermarked webp proxy for an image asset', async () => {
|
|
const assetKey = 'files/e2e-wm/test.png'
|
|
const asset = await seedAsset('test.png', 'image/png', assetKey, 'image')
|
|
const config = await seedConfig()
|
|
|
|
vi.mocked(transcodeService.getImageInfo).mockResolvedValue({
|
|
originalWidth: 100,
|
|
originalHeight: 100,
|
|
duration: 0,
|
|
bitRate: 0,
|
|
frameRate: 0,
|
|
totalFrames: 0,
|
|
hasAudio: false,
|
|
mimeType: 'image/png',
|
|
})
|
|
vi.mocked(s3Service.putObject).mockResolvedValue(undefined as never)
|
|
|
|
// compositeOverlayToWebpFile is mocked, but the activity stats the output
|
|
// file afterwards, so pre-create it at the expected path.
|
|
const outFileName = `test-watermark-${config.id}-100x100.webp`
|
|
const outFilePath = path.join('/tmp', outFileName)
|
|
fs.writeFileSync(outFilePath, Buffer.from('fake webp'))
|
|
|
|
const media = await transcodeWatermarkMediaActivity({
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
})
|
|
|
|
expect(media.proxyType).toBe('image')
|
|
expect(media.imageTranscodes?.length).toBeGreaterThan(0)
|
|
expect(media.imageTranscodes?.[0].key).toContain('watermark-')
|
|
expect(transcodeService.renderSvgToPng).toHaveBeenCalled()
|
|
expect(transcodeService.compositeOverlayToWebpFile).toHaveBeenCalledWith(
|
|
expect.stringContaining('test.png'),
|
|
Buffer.from('fake-overlay-png'),
|
|
outFilePath,
|
|
100,
|
|
100,
|
|
)
|
|
expect(s3Service.putObject).toHaveBeenCalledWith(
|
|
bucket,
|
|
expect.stringContaining('watermark-'),
|
|
expect.anything(),
|
|
expect.any(Number),
|
|
'image/webp',
|
|
)
|
|
|
|
fs.rmSync(outFilePath, { force: true })
|
|
})
|
|
|
|
it('produces a watermarked mp4 proxy for a video asset (ffmpeg path)', async () => {
|
|
const assetKey = 'files/e2e-wm/video.mp4'
|
|
const asset = await seedAsset('video.mp4', 'video/mp4', assetKey, 'video')
|
|
const config = await seedConfig()
|
|
|
|
vi.mocked(transcodeService.getVideoInfo).mockResolvedValue({
|
|
originalWidth: 1920,
|
|
originalHeight: 1080,
|
|
duration: 10,
|
|
bitRate: 1000,
|
|
frameRate: 30,
|
|
totalFrames: 300,
|
|
startTimecode: '00:00:00:00',
|
|
hasAudio: true,
|
|
mimeType: 'video/mp4',
|
|
})
|
|
vi.mocked(s3Service.putObject).mockResolvedValue(undefined as never)
|
|
|
|
// The ffmpeg execFile call is mocked, but the activity stats the output
|
|
// file afterwards, so pre-create it at the expected path.
|
|
const stem = 'video'
|
|
const configId = config.id
|
|
const outFilePath = path.join('/tmp', `${stem}-watermark-${configId}-100p.mp4`)
|
|
fs.writeFileSync(outFilePath, Buffer.from('fake mp4'))
|
|
|
|
const media = await transcodeWatermarkMediaActivity({
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
})
|
|
|
|
expect(media.proxyType).toBe('video')
|
|
expect(media.videoTranscodes?.length).toBeGreaterThan(0)
|
|
expect(media.videoTranscodes?.[0].key).toContain('watermark-')
|
|
expect(s3Service.putObject).toHaveBeenCalledWith(
|
|
bucket,
|
|
expect.stringContaining('watermark-'),
|
|
expect.anything(),
|
|
expect.any(Number),
|
|
'video/mp4',
|
|
)
|
|
|
|
fs.rmSync(outFilePath, { force: true })
|
|
})
|
|
})
|
|
|
|
describe('completeWatermarkFileActivity', () => {
|
|
it('upserts a completed watermark file with media', async () => {
|
|
const asset = await prisma.asset.create({
|
|
data: { name: 'wm-complete.png', type: 'file', status: 'processed' },
|
|
})
|
|
const config = await prisma.watermarkConfig.create({
|
|
data: {
|
|
hash: 'complete-hash-' + Math.random(),
|
|
config: { blocks: [] },
|
|
},
|
|
})
|
|
const media: PrismaJson.MediaInfo = {
|
|
duration: 0,
|
|
filesize: 0,
|
|
frames: 0,
|
|
proxyType: 'image',
|
|
imageTranscodes: [
|
|
{ key: 'files/x/out.webp', width: 100, height: 100, quality: 90, format: 'webp' },
|
|
],
|
|
videoTranscodes: [],
|
|
videoPreview: { width: 100, height: 100 },
|
|
finishedAt: new Date().toISOString(),
|
|
metadata: {
|
|
originalWidth: 100,
|
|
originalHeight: 100,
|
|
duration: 0,
|
|
bitRate: 0,
|
|
frameRate: 0,
|
|
totalFrames: 0,
|
|
startTimecode: '00:00:00:00',
|
|
hasAudio: false,
|
|
format: {},
|
|
},
|
|
original: {
|
|
key: 'files/x/original.png',
|
|
filesizeInBytes: 0,
|
|
codec: '',
|
|
},
|
|
}
|
|
|
|
await completeWatermarkFileActivity({
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
mediaInfo: media,
|
|
status: WatermarkFileStatus.completed,
|
|
})
|
|
|
|
const row = await prisma.watermarkFile.findUnique({
|
|
where: {
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
assetId_watermarkConfigId: {
|
|
assetId: asset.id,
|
|
watermarkConfigId: config.id,
|
|
},
|
|
},
|
|
})
|
|
expect(row?.status).toBe(WatermarkFileStatus.completed)
|
|
expect(row?.media).toBeDefined()
|
|
})
|
|
})
|
|
})
|