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
+2 -1
View File
@@ -6,6 +6,7 @@ import * as path from 'path'
import { s3Service } from '@shumai/core/src/s3/s3' import { s3Service } from '@shumai/core/src/s3/s3'
import { detectSupportedMimeType } from '@shumai/core/src/utils/mime' import { detectSupportedMimeType } from '@shumai/core/src/utils/mime'
import { ulid } from 'ulid' import { ulid } from 'ulid'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
function getMimeType(filePath: string): string { function getMimeType(filePath: string): string {
try { try {
@@ -64,7 +65,7 @@ export function createCreateFileTool(userId: string): AgentTool<typeof createFil
const mimeType = getMimeType(absolutePath) const mimeType = getMimeType(absolutePath)
// Generate compliant S3 key matching normal file upload format // Generate compliant S3 key matching normal file upload format
const s3Key = `files/${ulid()}/raw` const s3Key = `files/${ulid()}/${sanitizeFilename(path.basename(absolutePath))}`
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType) await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
const result = await executeAgentToolWorkflow({ const result = await executeAgentToolWorkflow({
+2 -1
View File
@@ -6,6 +6,7 @@ import * as path from 'path'
import { s3Service } from '@shumai/core/src/s3/s3' import { s3Service } from '@shumai/core/src/s3/s3'
import { detectSupportedMimeType } from '@shumai/core/src/utils/mime' import { detectSupportedMimeType } from '@shumai/core/src/utils/mime'
import { ulid } from 'ulid' import { ulid } from 'ulid'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
function getMimeType(filePath: string): string { function getMimeType(filePath: string): string {
try { try {
@@ -65,7 +66,7 @@ export function createCreateVersionTool(userId: string): AgentTool<typeof create
const mimeType = getMimeType(absolutePath) const mimeType = getMimeType(absolutePath)
// Generate compliant S3 key matching normal file upload format // Generate compliant S3 key matching normal file upload format
const s3Key = `files/${ulid()}/raw` const s3Key = `files/${ulid()}/${sanitizeFilename(path.basename(absolutePath))}`
await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType) await s3Service.uploadFileToKey(absolutePath, s3Key, mimeType)
const result = await executeAgentToolWorkflow({ const result = await executeAgentToolWorkflow({
+27
View File
@@ -299,5 +299,32 @@ const route = new Hono<{ Variables: { user: User } }>()
const files = await assetService.getDownloadLinks(req.ids) const files = await assetService.getDownloadLinks(req.ids)
return c.json({ files }) return c.json({ files })
}) })
.post(
'/files/download-url',
zValidator('json', z.object({ key: z.string().min(1), assetId: z.string().min(1) })),
async (c) => {
const user = c.get('user')
const req = c.req.valid('json')
const { key, assetId } = req
// Validate key format
if (!key.startsWith('files/')) {
return c.json({ error: 'Invalid key' }, 400)
}
// Verify user has read access to the asset
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Asset,
id: assetId,
})
// Verify key belongs to the asset and generate download URL
const url = await assetService.getDownloadUrl(assetId, key)
return c.json({ url })
},
)
export default route export default route
+22
View File
@@ -147,6 +147,28 @@ const route = app
return handlePublicShareError(c, err) return handlePublicShareError(c, err)
} }
}) })
.post(
'/shares/:shareId/files/:fileId/download-url',
zValidator('json', z.object({ key: z.string().min(1) })),
async (c) => {
const fileId = c.req.param('fileId')
const password = c.req.header('x-share-password')
const { key } = c.req.valid('json')
try {
await shareService.verifyPublicAccess(fileId, password)
if (!key.startsWith('files/')) {
return c.json({ error: 'Invalid key' }, 400)
}
const url = await assetService.getDownloadUrl(fileId, key)
return c.json({ url })
} catch (err) {
return handlePublicShareError(c, err)
}
},
)
.get( .get(
'/shares/:shareId/files/:fileId/comments', '/shares/:shareId/files/:fileId/comments',
zValidator('query', paginationParamsSchema), zValidator('query', paginationParamsSchema),
+19 -11
View File
@@ -39,7 +39,7 @@ describe('S3 API', () => {
} }
}) })
it('GET /files/* with filename query parameter sets Content-Disposition header', async () => { it('GET /files/* sets Content-Disposition: attachment only when download=1 query param is present', async () => {
// We need to capture the onFound callback from serveStatic options // We need to capture the onFound callback from serveStatic options
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
let onFoundCallback: ((path: string, c: any) => void) | undefined let onFoundCallback: ((path: string, c: any) => void) | undefined
@@ -54,26 +54,34 @@ describe('S3 API', () => {
const { default: route } = await import('./s3') const { default: route } = await import('./s3')
const app = new Hono().route('/files', route) const app = new Hono().route('/files', route)
// First, verify onFound is called through the mock await app.request('/files/b1/test.webp?download=1')
await app.request('/files/b1/test.webp?filename=my-file.txt')
expect(onFoundCallback).toBeDefined() expect(onFoundCallback).toBeDefined()
// Test the callback directly // Test with download=1
const mockContext = { const mockContextWithDownload = {
req: { req: { query: (key: string) => (key === 'download' ? '1' : undefined) },
query: (key: string) => (key === 'filename' ? 'my-file.txt' : undefined),
},
header: vi.fn(), header: vi.fn(),
} }
if (onFoundCallback) { if (onFoundCallback) {
onFoundCallback('/files/b1/test.webp', mockContext) onFoundCallback('/files/b1/test.webp', mockContextWithDownload)
expect(mockContext.header).toHaveBeenCalledWith( expect(mockContextWithDownload.header).toHaveBeenCalledWith(
'Content-Disposition', 'Content-Disposition',
'attachment; filename="my-file.txt"', 'attachment',
) )
} }
// Test without download param
const mockContextWithout = {
req: { query: () => undefined },
header: vi.fn(),
}
if (onFoundCallback) {
onFoundCallback('/files/b1/test.webp', mockContextWithout)
expect(mockContextWithout.header).not.toHaveBeenCalled()
}
}) })
it('PUT /files/:bucket/:key calls s3Service.putObject', async () => { it('PUT /files/:bucket/:key calls s3Service.putObject', async () => {
+2 -4
View File
@@ -9,10 +9,8 @@ const route = new Hono()
root: './data', root: './data',
rewriteRequestPath: (path) => path.replace(/^\/files\//, ''), rewriteRequestPath: (path) => path.replace(/^\/files\//, ''),
onFound: (_path, c) => { onFound: (_path, c) => {
const filename = c.req.query('filename') if (c.req.query('download') === '1') {
if (filename) { c.header('Content-Disposition', 'attachment')
const safeFilename = filename.replace(/["\r\n]/g, '_')
c.header('Content-Disposition', `attachment; filename="${safeFilename}"`)
} }
}, },
}), }),
+99
View File
@@ -2222,4 +2222,103 @@ describe('AssetService — natural sort by name', () => {
expect(childActiveDb?.status).toBe('uploaded') expect(childActiveDb?.status).toBe('uploaded')
}) })
}) })
describe('getDownloadUrl', () => {
it('should return a presigned download URL for a key in the same directory', async () => {
const asset = await prisma.asset.create({
data: {
name: 'test.mp4',
type: AssetType.file,
status: AssetStatus.uploaded,
storageKey: {
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
},
},
})
const url = await assetService.getDownloadUrl(
asset.id,
'files/01TESTULID000000000000000/test-720p.mp4',
)
expect(url).toBe('http://mock-s3-url')
})
it('should return a presigned download URL for the original key', async () => {
const asset = await prisma.asset.create({
data: {
name: 'test.mp4',
type: AssetType.file,
status: AssetStatus.uploaded,
storageKey: {
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
},
},
})
const url = await assetService.getDownloadUrl(
asset.id,
'files/01TESTULID000000000000000/test.mp4',
)
expect(url).toBe('http://mock-s3-url')
})
it('should throw if the key does not share the same parent directory', async () => {
const asset = await prisma.asset.create({
data: {
name: 'test.mp4',
type: AssetType.file,
status: AssetStatus.uploaded,
storageKey: {
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
},
},
})
await expect(
assetService.getDownloadUrl(asset.id, 'files/01OTHERULIDXXXXXXXXXX/hack.mp4'),
).rejects.toThrow('Key does not belong to this asset')
})
it('should throw if the key contains path traversal segments', async () => {
const asset = await prisma.asset.create({
data: {
name: 'test.mp4',
type: AssetType.file,
status: AssetStatus.uploaded,
storageKey: {
create: { key: 'files/01TESTULID000000000000000/test.mp4' },
},
},
})
await expect(
assetService.getDownloadUrl(
asset.id,
'files/01TESTULID000000000000000/../01OTHERULIDXXXXXXXXXX/secret.mp4',
),
).rejects.toThrow('Key does not belong to this asset')
})
it('should throw if the asset does not exist', async () => {
await expect(
assetService.getDownloadUrl('nonexistent-id', 'files/01TESTULID000000000000000/test.mp4'),
).rejects.toThrow('Asset not found or has no storage key')
})
it('should throw if the asset has no storage key', async () => {
const asset = await prisma.asset.create({
data: {
name: 'folder',
type: AssetType.folder,
status: AssetStatus.uploaded,
},
})
await expect(
assetService.getDownloadUrl(asset.id, 'files/01TESTULID000000000000000/test.mp4'),
).rejects.toThrow('Asset not found or has no storage key')
})
})
}) })
+37 -20
View File
@@ -842,7 +842,6 @@ export class AssetService {
process.env.S3_BUCKET || 'shumai', process.env.S3_BUCKET || 'shumai',
info.media.original.key, info.media.original.key,
'GET', 'GET',
a.name,
) )
} }
} }
@@ -1583,7 +1582,7 @@ export class AssetService {
} }
} }
const media = latestVersion.media as PrismaJson.MediaInfo | null let media = latestVersion.media as PrismaJson.MediaInfo | null
if (media && media.videoPreview?.key) { if (media && media.videoPreview?.key) {
media.videoPreview.url = await s3Service.presign( media.videoPreview.url = await s3Service.presign(
process.env.S3_BUCKET || 'shumai', process.env.S3_BUCKET || 'shumai',
@@ -1593,21 +1592,19 @@ export class AssetService {
} }
const key = latestVersion.storageKey?.key const key = latestVersion.storageKey?.key
if (media && key) { if (key) {
const fileName = if (!media) {
a.type === AssetType.version_stack && (a.name === '' || !a.name) media = {
? latestVersion.name original: null,
: a.name videoTranscodes: [],
imageTranscodes: [],
} as unknown as PrismaJson.MediaInfo
}
media.original = { media.original = {
key, key,
downloadUrl: await s3Service.presign( downloadUrl: await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET'),
process.env.S3_BUCKET || 'shumai',
key,
'GET',
fileName,
),
filesizeInBytes: latestVersion.sizeByte, filesizeInBytes: latestVersion.sizeByte,
codec: '', // TODO: extract from metadata if needed codec: '',
} }
} }
@@ -1845,12 +1842,7 @@ export class AssetService {
const key = file.storageKeyId ? storageKeyMap.get(file.storageKeyId) : null const key = file.storageKeyId ? storageKeyMap.get(file.storageKeyId) : null
if (!key) return null if (!key) return null
const url = await s3Service.presign( const url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true)
process.env.S3_BUCKET || 'shumai',
key,
'GET',
file.name,
)
return { return {
id: file.id, id: file.id,
@@ -1863,6 +1855,31 @@ export class AssetService {
return downloadLinks.filter((link): link is { id: string; name: string; url: string } => !!link) return downloadLinks.filter((link): link is { id: string; name: string; url: string } => !!link)
} }
/**
* Generate a presigned download URL for a given key, verifying it belongs to the asset's
* storage directory. This ensures users can only download files (original, transcodes, etc.)
* that belong to an asset they have access to.
*/
async getDownloadUrl(assetId: string, key: string): Promise<string> {
const asset = await this.prismaClient.asset.findUnique({
where: { id: assetId },
select: { storageKey: { select: { key: true } } },
})
if (!asset?.storageKey?.key) {
throw new Error('Asset not found or has no storage key')
}
// Verify the requested key shares the same parent directory as the asset's storage key
const storageKey = asset.storageKey.key
const assetDir = storageKey.substring(0, storageKey.lastIndexOf('/') + 1)
if (key.includes('..') || !key.startsWith(assetDir)) {
throw new Error('Key does not belong to this asset')
}
return s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true)
}
private async toPreviewInfo(asset: Asset | AssetWithIncludes): Promise<PreviewInfo | null> { private async toPreviewInfo(asset: Asset | AssetWithIncludes): Promise<PreviewInfo | null> {
if (!asset.media) return null if (!asset.media) return null
+33 -6
View File
@@ -117,17 +117,44 @@ describe('S3Service implementations', () => {
delete process.env.PRESIGNED_URL_EXPIRES_IN delete process.env.PRESIGNED_URL_EXPIRES_IN
}) })
it('should pass filename to presign', async () => { it('should not set contentDisposition for normal GET presign', async () => {
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false) const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
await s3.presign('bucket', 'key', 'GET', 'test.txt') await s3.presign('bucket', 'key', 'GET')
expect(s3PresignSpy).toHaveBeenCalledWith(
'key',
expect.not.objectContaining({
contentDisposition: expect.anything(),
}),
)
})
it('should set contentDisposition attachment when download is true', async () => {
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
await s3.presign('bucket', 'key', 'GET', true)
expect(s3PresignSpy).toHaveBeenCalledWith( expect(s3PresignSpy).toHaveBeenCalledWith(
'key', 'key',
expect.objectContaining({ expect.objectContaining({
contentDisposition: 'attachment; filename="test.txt"', contentDisposition: 'attachment',
}), }),
) )
}) })
it('should not cache download presign URLs', async () => {
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
s3PresignSpy.mockClear()
s3PresignSpy.mockReturnValueOnce('http://download-url-1')
s3PresignSpy.mockReturnValueOnce('http://download-url-2')
const url1 = await s3.presign('bucket', 'key', 'GET', true)
const url2 = await s3.presign('bucket', 'key', 'GET', true)
expect(url1).toBe('http://download-url-1')
expect(url2).toBe('http://download-url-2')
expect(s3PresignSpy).toHaveBeenCalledTimes(2)
})
}) })
describe('LocalStorageService', () => { describe('LocalStorageService', () => {
@@ -182,9 +209,9 @@ describe('S3Service implementations', () => {
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt') expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt')
}) })
it('should generate a local presign URL with filename', async () => { it('should generate a local presign URL with download param', async () => {
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', 'original.txt') const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true)
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?filename=original.txt') expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?download=1')
}) })
it('should throw an error for unsupported presign methods', async () => { it('should throw an error for unsupported presign methods', async () => {
+10 -10
View File
@@ -54,7 +54,7 @@ export interface S3Service {
listObjects(bucket: string, prefix: string): Promise<string[]> listObjects(bucket: string, prefix: string): Promise<string[]>
uploadFile(filePath: string, contentType: string): Promise<string> uploadFile(filePath: string, contentType: string): Promise<string>
uploadFileToKey(filePath: string, key: string, contentType: string): Promise<void> uploadFileToKey(filePath: string, key: string, contentType: string): Promise<void>
presign(bucket: string, key: string, method: string, filename?: string): Promise<string> presign(bucket: string, key: string, method: string, download?: boolean): Promise<string>
} }
export class S3StorageService implements S3Service { export class S3StorageService implements S3Service {
@@ -203,16 +203,16 @@ export class S3StorageService implements S3Service {
}) })
} }
async presign(bucket: string, key: string, method: string, filename?: string): Promise<string> { async presign(bucket: string, key: string, method: string, download?: boolean): Promise<string> {
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10) const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const expiresInSeconds = expireHours * 3600 const expiresInSeconds = expireHours * 3600
// Cache time is 2/3 of expire time, rounded to minute // Cache time is 2/3 of expire time, rounded to minute
const cacheMinutes = Math.round((expireHours * 60 * 2) / 3) const cacheMinutes = Math.round((expireHours * 60 * 2) / 3)
const cacheTtlMs = cacheMinutes * 60 * 1000 const cacheTtlMs = cacheMinutes * 60 * 1000
const cacheKey = `${bucket}:${key}:${filename || ''}` const cacheKey = `${bucket}:${key}`
if (method === 'GET') { if (method === 'GET' && !download) {
const cached = this.presignCache.get(cacheKey) const cached = this.presignCache.get(cacheKey)
if (cached) return cached if (cached) return cached
} }
@@ -225,13 +225,13 @@ export class S3StorageService implements S3Service {
method: method as any, method: method as any,
} }
if (filename) { if (download) {
options.contentDisposition = `attachment; filename="${filename}"` options.contentDisposition = 'attachment'
} }
const url = this.client.presign(key, options) const url = this.client.presign(key, options)
if (method === 'GET') { if (method === 'GET' && !download) {
this.presignCache.set(cacheKey, url, cacheTtlMs) this.presignCache.set(cacheKey, url, cacheTtlMs)
} }
@@ -435,7 +435,7 @@ export class LocalStorageService implements S3Service {
await fs.promises.copyFile(filePath, destPath) await fs.promises.copyFile(filePath, destPath)
} }
async presign(bucket: string, key: string, method: string, filename?: string): Promise<string> { async presign(bucket: string, key: string, method: string, download?: boolean): Promise<string> {
if (method !== 'GET' && method !== 'PUT') { if (method !== 'GET' && method !== 'PUT') {
throw new Error(`Invalid method: ${method}`) throw new Error(`Invalid method: ${method}`)
} }
@@ -443,8 +443,8 @@ export class LocalStorageService implements S3Service {
return `${this.endpoint}${signLocalUrl(bucket, key)}` return `${this.endpoint}${signLocalUrl(bucket, key)}`
} }
let url = `${this.endpoint}/files/${bucket}/${key}` let url = `${this.endpoint}/files/${bucket}/${key}`
if (filename) { if (download) {
url += `?filename=${encodeURIComponent(filename)}` url += '?download=1'
} }
return url return url
} }
+2 -1
View File
@@ -21,6 +21,7 @@ import {
import { ImageTranscoder, VideoTranscoder } from '@shumai/transcode' import { ImageTranscoder, VideoTranscoder } from '@shumai/transcode'
import { generateKeyBetween } from 'jittered-fractional-indexing' import { generateKeyBetween } from 'jittered-fractional-indexing'
import { ulid } from 'ulid' import { ulid } from 'ulid'
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
export class UploadService { export class UploadService {
constructor(private readonly prismaClient: typeof prisma = prisma) {} constructor(private readonly prismaClient: typeof prisma = prisma) {}
@@ -130,7 +131,7 @@ export class UploadService {
const assetType = file.type === 'folder' ? AssetType.folder : AssetType.file const assetType = file.type === 'folder' ? AssetType.folder : AssetType.file
let key: string | null = null let key: string | null = null
if (assetType === AssetType.file) { if (assetType === AssetType.file) {
key = `files/${ulid()}/raw` key = `files/${ulid()}/${sanitizeFilename(file.name)}`
} }
const newAsset = await tx.asset.create({ const newAsset = await tx.asset.create({
+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
}
+1
View File
@@ -279,6 +279,7 @@ export interface ImageTranscode {
export interface VideoTranscode { export interface VideoTranscode {
id: string id: string
url: string url: string
key: string
width: number width: number
height: number height: number
size: number size: number
+11 -4
View File
@@ -2,6 +2,7 @@ import { prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3' import { s3Service } from '@shumai/core/src/s3/s3'
import { transcodeService } from '@shumai/core' import { transcodeService } from '@shumai/core'
import { metadataService } from '@shumai/core/src/metadata/metadata' import { metadataService } from '@shumai/core/src/metadata/metadata'
import { stemFromKey } from '@shumai/core/src/utils/filename'
import { ApplicationFailure } from '@temporalio/activity' import { ApplicationFailure } from '@temporalio/activity'
import * as path from 'path' import * as path from 'path'
import * as fs from 'fs' import * as fs from 'fs'
@@ -162,7 +163,9 @@ export async function transcodeVideoActivity(
params: VideoActivityParams, params: VideoActivityParams,
): Promise<PrismaJson.VideoTranscode> { ): Promise<PrismaJson.VideoTranscode> {
const bucket = process.env.S3_BUCKET || 'shumai' const bucket = process.env.S3_BUCKET || 'shumai'
const key = path.join(path.dirname(params.assetKey), `video-${params.videoSpec.height}p.mp4`) const stem = stemFromKey(params.assetKey)
const res = params.videoSpec.resolution || `${params.videoSpec.height}p`
const key = path.posix.join(path.posix.dirname(params.assetKey), `${stem}-${res}.mp4`)
try { try {
await s3Service.headObject(bucket, key) await s3Service.headObject(bucket, key)
@@ -172,7 +175,7 @@ export async function transcodeVideoActivity(
} }
const tmpDir = path.dirname(params.filePath) const tmpDir = path.dirname(params.filePath)
const outputFile = path.join(tmpDir, `video-${params.videoSpec.height}p.mp4`) const outputFile = path.join(tmpDir, `${stem}-${res}.mp4`)
try { try {
let targetFps: number | string = params.originalFps || 30 let targetFps: number | string = params.originalFps || 30
@@ -237,7 +240,11 @@ export async function transcodeImageActivity(
params: ImageActivityParams, params: ImageActivityParams,
): Promise<PrismaJson.ImageTranscode> { ): Promise<PrismaJson.ImageTranscode> {
const bucket = process.env.S3_BUCKET || 'shumai' const bucket = process.env.S3_BUCKET || 'shumai'
const key = path.join(path.dirname(params.assetKey), `image-${params.imageSpec.width}p.webp`) const stem = stemFromKey(params.assetKey)
const key = path.posix.join(
path.posix.dirname(params.assetKey),
`${stem}-${params.imageSpec.width}p.webp`,
)
try { try {
await s3Service.headObject(bucket, key) await s3Service.headObject(bucket, key)
@@ -247,7 +254,7 @@ export async function transcodeImageActivity(
} }
const tmpDir = path.dirname(params.filePath) const tmpDir = path.dirname(params.filePath)
const outputFile = path.join(tmpDir, `image-${params.imageSpec.width}p.webp`) const outputFile = path.join(tmpDir, `${stem}-${params.imageSpec.width}p.webp`)
try { try {
await transcodeService.transcodeImage( await transcodeService.transcodeImage(
+97 -3
View File
@@ -1,8 +1,10 @@
import { Badge } from '@/ui/components/ui/badge' import { Badge } from '@/ui/components/ui/badge'
import { client } from '@/ui/api/client'
import { import {
DropdownMenu, DropdownMenu,
DropdownMenuContent, DropdownMenuContent,
DropdownMenuItem, DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuPortal, DropdownMenuPortal,
DropdownMenuSeparator, DropdownMenuSeparator,
DropdownMenuSub, DropdownMenuSub,
@@ -44,6 +46,15 @@ interface BreadcrumbNavProps {
shareId?: string shareId?: string
onFolderClick?: (folderId: string) => void onFolderClick?: (folderId: string) => void
fileId?: string fileId?: string
downloadInfo?: {
originalKey?: string
videoTranscodes?: Array<{
key: string
width: number
height: number
isRaw?: boolean
}>
}
versions?: Array<{ versions?: Array<{
id: string id: string
version: number version: number
@@ -69,6 +80,8 @@ export function BreadcrumbNav({
isPublic = false, isPublic = false,
onFolderClick, onFolderClick,
fileId, fileId,
shareId,
downloadInfo,
versions, versions,
}: BreadcrumbNavProps) { }: BreadcrumbNavProps) {
const navigate = useNavigate() const navigate = useNavigate()
@@ -83,6 +96,33 @@ export function BreadcrumbNav({
}) })
} }
const handleDownload = async (key: string) => {
if (!fileId) return
try {
const res = shareId
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
param: { shareId, fileId },
json: { key },
})
: await client.api.files['download-url'].$post({
json: { key, assetId: fileId },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
}
const hasVideoTranscodes =
downloadInfo?.videoTranscodes && downloadInfo.videoTranscodes.filter((t) => !t.isRaw).length > 0
const breadcrumbs: { name: string; path?: string; id?: string; isMuted?: boolean }[] = isPublic const breadcrumbs: { name: string; path?: string; id?: string; isMuted?: boolean }[] = isPublic
? [ ? [
{ {
@@ -155,9 +195,63 @@ export function BreadcrumbNav({
<ChevronDown className="h-4 w-4" /> <ChevronDown className="h-4 w-4" />
</DropdownMenuTrigger> </DropdownMenuTrigger>
<DropdownMenuContent className="w-56"> <DropdownMenuContent className="w-56">
<DropdownMenuItem onClick={() => console.log('Download')}> {hasVideoTranscodes ? (
Download <DropdownMenuSub>
</DropdownMenuItem> <DropdownMenuSubTrigger className="flex items-center gap-2">
<span>{m.download()}</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent className="w-48">
<DropdownMenuLabel>{m.download()}</DropdownMenuLabel>
{downloadInfo?.videoTranscodes
?.filter((t) => !t.isRaw)
.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
return (
<DropdownMenuItem
key={resolution}
onClick={() => handleDownload(t.key)}
className="flex items-center justify-between"
>
<span>{resolution}</span>
<span className="text-xs text-muted-foreground">MP4</span>
</DropdownMenuItem>
)
})}
{downloadInfo?.originalKey && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() => handleDownload(downloadInfo.originalKey!)}
className="flex items-center justify-between"
>
<span>Original</span>
<span className="text-xs text-muted-foreground">
{currentAsset.name?.split('.').pop()?.toUpperCase() || 'RAW'}
</span>
</DropdownMenuItem>
</>
)}
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
) : (
<DropdownMenuItem
onClick={() =>
downloadInfo?.originalKey && handleDownload(downloadInfo.originalKey)
}
disabled={!downloadInfo?.originalKey}
>
{m.download()}
</DropdownMenuItem>
)}
{canEdit && ( {canEdit && (
<DropdownMenuItem onClick={() => console.log('Rename')}> <DropdownMenuItem onClick={() => console.log('Rename')}>
Rename Rename
+29 -11
View File
@@ -1,4 +1,5 @@
import { useScreenSize } from '@/ui/hooks/useScreenSize' import { useScreenSize } from '@/ui/hooks/useScreenSize'
import { client } from '@/ui/api/client'
import { getBestTranscode } from '@/ui/lib/media' import { getBestTranscode } from '@/ui/lib/media'
import type { AssetInfo } from '@shumai/dtos' import type { AssetInfo } from '@shumai/dtos'
import { Check, Copy, Download, Minus, Plus } from 'lucide-react' import { Check, Copy, Download, Minus, Plus } from 'lucide-react'
@@ -19,6 +20,7 @@ type FileViewerProps = {
onTimeUpdate?: (time: number) => void onTimeUpdate?: (time: number) => void
annotations?: Annotation[] annotations?: Annotation[]
startTime?: number startTime?: number
shareId?: string
children?: ReactNode children?: ReactNode
} }
@@ -30,6 +32,7 @@ export function FileViewer({
onTimeUpdate, onTimeUpdate,
annotations, annotations,
startTime, startTime,
shareId,
children, children,
}: FileViewerProps) { }: FileViewerProps) {
const { width: screenWidth } = useScreenSize() const { width: screenWidth } = useScreenSize()
@@ -133,15 +136,29 @@ export function FileViewer({
} }
} }
const handleDownload = () => { const handleDownload = async () => {
const url = file.media?.original?.downloadUrl const key = file.media?.original?.key
if (!url) return if (!key || !file.id) return
const link = document.createElement('a') try {
link.href = url const res = shareId
link.download = file.name || 'image' ? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
document.body.appendChild(link) param: { shareId, fileId: file.id },
link.click() json: { key },
document.body.removeChild(link) })
: await client.api.files['download-url'].$post({
json: { key, assetId: file.id },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
} }
const handleCopy = async () => { const handleCopy = async () => {
@@ -210,7 +227,7 @@ export function FileViewer({
<div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200"> <div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
<button <button
onClick={handleDownload} onClick={handleDownload}
disabled={!file.media?.original?.downloadUrl} disabled={!file.media?.original?.key}
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200" className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
title="Download original file" title="Download original file"
> >
@@ -291,7 +308,7 @@ export function FileViewer({
</button> </button>
<button <button
onClick={handleDownload} onClick={handleDownload}
disabled={!file.media?.original?.downloadUrl} disabled={!file.media?.original?.key}
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200" className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
title="Download original image" title="Download original image"
> >
@@ -311,6 +328,7 @@ export function FileViewer({
onTimeUpdate={onTimeUpdate} onTimeUpdate={onTimeUpdate}
annotations={displayAnnotations} annotations={displayAnnotations}
startTime={startTime} startTime={startTime}
shareId={shareId}
> >
{children} {children}
</VideoPlayer> </VideoPlayer>
@@ -350,6 +350,17 @@ export function PublicShareManager({
isPublic: true, isPublic: true,
shareId, shareId,
fileId: viewingFileId || undefined, fileId: viewingFileId || undefined,
downloadInfo: viewingFileData
? {
originalKey: viewingFileData.media?.original?.key,
videoTranscodes: viewingFileData.media?.videoTranscodes?.map((t) => ({
key: t.key,
width: t.width,
height: t.height,
isRaw: t.isRaw,
})),
}
: undefined,
onFolderClick: handleBreadcrumbClick, onFolderClick: handleBreadcrumbClick,
isRightSidebarCollapsed, isRightSidebarCollapsed,
onRightSidebarToggle: () => setIsRightSidebarCollapsed((prev) => !prev), onRightSidebarToggle: () => setIsRightSidebarCollapsed((prev) => !prev),
@@ -458,6 +469,7 @@ export function PublicShareManager({
onTimeUpdate={setCurrentTime} onTimeUpdate={setCurrentTime}
annotations={annotations} annotations={annotations}
startTime={startTime} startTime={startTime}
shareId={shareId}
/> />
)} )}
</div> </div>
+2
View File
@@ -19,6 +19,7 @@ export function TopNav() {
currentAsset, currentAsset,
isRootFolder, isRootFolder,
fileId, fileId,
downloadInfo,
versions, versions,
isPublic, isPublic,
shareId, shareId,
@@ -87,6 +88,7 @@ export function TopNav() {
isPublic={isPublic} isPublic={isPublic}
shareId={shareId} shareId={shareId}
fileId={fileId} fileId={fileId}
downloadInfo={downloadInfo}
versions={versions} versions={versions}
onFolderClick={onFolderClick} onFolderClick={onFolderClick}
/> />
@@ -1,4 +1,5 @@
import type { AssetInfo, VideoTranscode } from '@shumai/dtos' import type { AssetInfo, VideoTranscode } from '@shumai/dtos'
import { client } from '@/ui/api/client'
import { cn } from '@/ui/lib/utils' import { cn } from '@/ui/lib/utils'
import { import {
Check, Check,
@@ -58,6 +59,7 @@ interface VideoPlayerProps {
onTimeUpdate?: (time: number) => void onTimeUpdate?: (time: number) => void
annotations?: Annotation[] annotations?: Annotation[]
startTime?: number startTime?: number
shareId?: string
children?: React.ReactNode children?: React.ReactNode
} }
@@ -76,7 +78,7 @@ interface ControlBarProps {
handleVolumeChange: (newVolume: number) => void handleVolumeChange: (newVolume: number) => void
changePlaybackRate: (rate: number) => void changePlaybackRate: (rate: number) => void
changeResolution: (res: DisplayTranscode) => void changeResolution: (res: DisplayTranscode) => void
handleDownload: (url: string, resolution: string) => void handleDownload: (key: string) => void
toggleFullScreen: () => void toggleFullScreen: () => void
onZoomChange: (zoom: number) => void onZoomChange: (zoom: number) => void
onZoomReset: () => void onZoomReset: () => void
@@ -344,11 +346,13 @@ const ControlBar: React.FC<ControlBarProps> = ({
{resolutions.map((res) => ( {resolutions.map((res) => (
<DropdownMenuItem <DropdownMenuItem
key={res.resolution} key={res.resolution}
onClick={() => handleDownload(res.url ?? '', res.resolution)} onClick={() => handleDownload(res.key ?? '')}
className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground" className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground"
> >
<span>{res.resolution}</span> <span>{res.resolution}</span>
<span className="text-xs text-muted-foreground">MP4</span> <span className="text-xs text-muted-foreground">
{res.isRaw ? data.name?.split('.').pop()?.toUpperCase() || 'RAW' : 'MP4'}
</span>
</DropdownMenuItem> </DropdownMenuItem>
))} ))}
</DropdownMenuContent> </DropdownMenuContent>
@@ -376,6 +380,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
onTimeUpdate, onTimeUpdate,
annotations, annotations,
startTime, startTime,
shareId,
children, children,
}) => { }) => {
const localPlayerRef = useRef<Player | null>(null) const localPlayerRef = useRef<Player | null>(null)
@@ -456,12 +461,16 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
} }
// Initial resolution setup // Initial resolution setup
const originalRes = { const originalRes: DisplayTranscode = {
id: 'original',
url: data.media.original.downloadUrl, url: data.media.original.downloadUrl,
key: data.media.original.key ?? '',
width: data.media.metadata.originalWidth ?? 0, width: data.media.metadata.originalWidth ?? 0,
height: data.media.metadata.originalHeight ?? 0, height: data.media.metadata.originalHeight ?? 0,
size: 0,
isRaw: true,
resolution: 'Original', resolution: 'Original',
} as DisplayTranscode }
const resolutions: DisplayTranscode[] = (data.media.videoTranscodes ?? []).map((t) => { const resolutions: DisplayTranscode[] = (data.media.videoTranscodes ?? []).map((t) => {
const longSide = Math.max(t.width, t.height) const longSide = Math.max(t.width, t.height)
@@ -837,13 +846,27 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
} }
} }
const handleDownload = (url: string, resolution: string) => { const handleDownload = async (key: string) => {
const link = document.createElement('a') try {
link.href = url const res = shareId
link.download = `${data.name}-${resolution}.mp4` ? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
document.body.appendChild(link) param: { shareId, fileId: data.id! },
link.click() json: { key },
document.body.removeChild(link) })
: await client.api.files['download-url'].$post({
json: { key, assetId: data.id! },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
} }
// Listen for fullscreen change events (ESC key) // Listen for fullscreen change events (ESC key)
@@ -148,6 +148,15 @@ function FileViewPage() {
}, },
isRootFolder: false, isRootFolder: false,
fileId, fileId,
downloadInfo: {
originalKey: fileData.media?.original?.key,
videoTranscodes: fileData.media?.videoTranscodes?.map((t) => ({
key: t.key,
width: t.width,
height: t.height,
isRaw: t.isRaw,
})),
},
versions: versionsDataList, versions: versionsDataList,
onFolderClick: (id: string) => { onFolderClick: (id: string) => {
navigate({ navigate({
+9
View File
@@ -13,6 +13,15 @@ export interface TopNavProjectState {
} }
isRootFolder: boolean isRootFolder: boolean
fileId?: string fileId?: string
downloadInfo?: {
originalKey?: string
videoTranscodes?: Array<{
key: string
width: number
height: number
isRaw?: boolean
}>
}
versions?: Array<{ versions?: Array<{
id: string id: string
version: number version: number