refactor(storage): use original filenames and fix download UX

- Store uploaded files as files/{ulid}/{original-name} instead of files/{ulid}/raw
- Store transcoded videos as {stem}-{resolution}.mp4 (e.g. foo-720p.mp4)
- Store transcoded images as {stem}-{width}p.webp (e.g. foo-1920p.webp)
- Add download?: boolean param to presign - only sets Content-Disposition: attachment when true
- Local storage handler only sets Content-Disposition when ?download=1 query param is present
- Add POST /api/files/download-url endpoint for key-based single file downloads
  - Uses ResourceType.Asset for permission check
  - Validates key belongs to the asset's storage directory
- Fix existing download-links API to pass download: true to presign
- Fix breadcrumb navbar download: video shows resolution submenu, others download directly
- Fix video player download: calls download-url API, shows correct file type label
- Remove filename query param hack from presign URLs
- Add sanitizeFilename() and stemFromKey() utility helpers
- Add key field to VideoTranscode DTO interface
This commit is contained in:
yiling
2026-07-02 17:39:23 +08:00
parent 114cdca4ee
commit b7367bb129
22 changed files with 562 additions and 84 deletions
+66
View File
@@ -0,0 +1,66 @@
import { describe, it, expect } from 'vitest'
import { sanitizeFilename, stemFromKey } from './filename'
describe('sanitizeFilename', () => {
it('returns the filename unchanged when it is already safe', () => {
expect(sanitizeFilename('foo.mp4')).toBe('foo.mp4')
expect(sanitizeFilename('my-video_final.mov')).toBe('my-video_final.mov')
})
it('replaces slashes and backslashes with underscores', () => {
expect(sanitizeFilename('path/to/file.mp4')).toBe('path_to_file.mp4')
expect(sanitizeFilename('path\\to\\file.mp4')).toBe('path_to_file.mp4')
})
it('replaces NUL bytes and control characters', () => {
expect(sanitizeFilename('foo\x00bar.mp4')).toBe('foo_bar.mp4')
expect(sanitizeFilename('foo\x01bar.mp4')).toBe('foo_bar.mp4')
})
it('trims leading and trailing dots and whitespace', () => {
expect(sanitizeFilename('.hidden')).toBe('hidden')
expect(sanitizeFilename('...dotted...')).toBe('dotted')
expect(sanitizeFilename(' spaced ')).toBe('spaced')
expect(sanitizeFilename(' .mixed. ')).toBe('mixed')
})
it('collapses multiple consecutive underscores', () => {
expect(sanitizeFilename('a//b')).toBe('a_b')
expect(sanitizeFilename('a///b')).toBe('a_b')
})
it('returns "file" for empty or whitespace-only input', () => {
expect(sanitizeFilename('')).toBe('file')
expect(sanitizeFilename(' ')).toBe('file')
expect(sanitizeFilename('...')).toBe('file')
})
it('preserves unicode characters', () => {
expect(sanitizeFilename('视频文件.mp4')).toBe('视频文件.mp4')
expect(sanitizeFilename('café.png')).toBe('café.png')
})
it('preserves spaces within the filename', () => {
expect(sanitizeFilename('my video file.mp4')).toBe('my video file.mp4')
})
})
describe('stemFromKey', () => {
it('extracts the stem from a key with extension', () => {
expect(stemFromKey('files/01ABC/foo.mp4')).toBe('foo')
expect(stemFromKey('files/01ABC/my-video.mov')).toBe('my-video')
})
it('extracts the stem from a key without extension', () => {
expect(stemFromKey('files/01ABC/raw')).toBe('raw')
})
it('handles keys with multiple dots in filename', () => {
expect(stemFromKey('files/01ABC/my.video.file.mp4')).toBe('my.video.file')
})
it('handles keys with no directory', () => {
expect(stemFromKey('foo.mp4')).toBe('foo')
expect(stemFromKey('raw')).toBe('raw')
})
})
+36
View File
@@ -0,0 +1,36 @@
import path from 'path'
/**
* Sanitize a filename for safe use as an S3 key segment.
* - Replaces characters unsafe for S3/filesystems with '_'
* - Trims leading/trailing whitespace and dots
* - Falls back to 'file' if the result is empty
*/
export function sanitizeFilename(filename: string): string {
// Replace NUL bytes, slashes, backslashes, and control characters
// eslint-disable-next-line no-control-regex
let safe = filename.replace(/[\x00-\x1f\x7f/\\]/g, '_')
// Trim leading/trailing whitespace and dots
safe = safe.replace(/^[\s.]+|[\s.]+$/g, '')
// Collapse multiple underscores
safe = safe.replace(/_+/g, '_')
if (!safe) {
return 'file'
}
return safe
}
/**
* Extract the filename stem (without extension) from a storage key.
* e.g. 'files/01ABC.../foo.mp4' → 'foo'
* 'files/01ABC.../raw' → 'raw'
*/
export function stemFromKey(key: string): string {
const basename = path.basename(key)
const ext = path.extname(basename)
return ext ? basename.slice(0, -ext.length) : basename
}