mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 04:14:30 +00:00
fix(transcode): co-locate screenshots and derived artifacts in asset storage directory (#399)
* fix(transcode): co-locate screenshots and derived artifacts in asset storage directory * refactor(transcode): centralize derived artifact directory resolution and strengthen upload assertions
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { sanitizeFilename, stemFromKey } from './filename'
|
||||
import { getDerivedArtifactDirectory, sanitizeFilename, stemFromKey } from './filename'
|
||||
|
||||
describe('sanitizeFilename', () => {
|
||||
it('returns the filename unchanged when it is already safe', () => {
|
||||
@@ -64,3 +64,21 @@ describe('stemFromKey', () => {
|
||||
expect(stemFromKey('raw')).toBe('raw')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getDerivedArtifactDirectory', () => {
|
||||
it('extracts the directory from a standard storage key', () => {
|
||||
expect(getDerivedArtifactDirectory('files/01ABC/video.mp4')).toBe('files/01ABC')
|
||||
expect(getDerivedArtifactDirectory('projects/p1/doc.pdf', 'asset-123')).toBe('projects/p1')
|
||||
expect(getDerivedArtifactDirectory('a/b/c/file.txt')).toBe('a/b/c')
|
||||
})
|
||||
|
||||
it('falls back to files/<assetId> when assetKey has no directory segment', () => {
|
||||
expect(getDerivedArtifactDirectory('video.mp4', 'asset-123')).toBe('files/asset-123')
|
||||
expect(getDerivedArtifactDirectory('doc.pdf', '01M21XYZ')).toBe('files/01M21XYZ')
|
||||
})
|
||||
|
||||
it('returns empty string when assetKey has no directory segment and assetId is omitted or null', () => {
|
||||
expect(getDerivedArtifactDirectory('video.mp4')).toBe('')
|
||||
expect(getDerivedArtifactDirectory('video.mp4', null)).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -34,3 +34,19 @@ export function stemFromKey(key: string): string {
|
||||
const ext = path.extname(basename)
|
||||
return ext ? basename.slice(0, -ext.length) : basename
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the parent directory for derived artifacts (e.g. screenshots, proxies, annotations).
|
||||
* Prefers the directory of the asset's storage key, falling back to `files/<assetId>` if the key
|
||||
* has no directory prefix.
|
||||
*
|
||||
* e.g. 'files/01ABC.../video.mp4' → 'files/01ABC...'
|
||||
* 'video.mp4' (with assetId '01XYZ...') → 'files/01XYZ...'
|
||||
*/
|
||||
export function getDerivedArtifactDirectory(assetKey: string, assetId?: string | null): string {
|
||||
const dir = path.posix.dirname(assetKey)
|
||||
if (dir === '.' || !dir) {
|
||||
return assetId ? `files/${assetId}` : ''
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user