From b7367bb129dc9e68de7cfacb1de43c510fd562ba Mon Sep 17 00:00:00 2001 From: yiling Date: Thu, 2 Jul 2026 15:26:10 +0800 Subject: [PATCH] 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 --- packages/agent/src/tools/create-file.ts | 3 +- packages/agent/src/tools/create-version.ts | 3 +- packages/api/src/file.ts | 27 +++++ packages/api/src/public-share.ts | 22 ++++ packages/api/src/s3.test.ts | 30 ++++-- packages/api/src/s3.ts | 6 +- packages/core/src/asset/asset.test.ts | 99 +++++++++++++++++ packages/core/src/asset/asset.ts | 57 ++++++---- packages/core/src/s3/s3.test.ts | 39 +++++-- packages/core/src/s3/s3.ts | 20 ++-- packages/core/src/upload/upload.ts | 3 +- packages/core/src/utils/filename.test.ts | 66 ++++++++++++ packages/core/src/utils/filename.ts | 36 +++++++ packages/dtos/src/asset.ts | 1 + .../transcode/src/activities/transcode.ts | 15 ++- packages/webui/components/breadcrumb-nav.tsx | 100 +++++++++++++++++- packages/webui/components/file-viewer.tsx | 40 +++++-- .../webui/components/public-share-manager.tsx | 12 +++ packages/webui/components/top-nav.tsx | 2 + .../webui/components/viewers/video-player.tsx | 47 +++++--- .../projects/$projectId/files/$fileId.tsx | 9 ++ packages/webui/stores/top-nav.ts | 9 ++ 22 files changed, 562 insertions(+), 84 deletions(-) create mode 100644 packages/core/src/utils/filename.test.ts create mode 100644 packages/core/src/utils/filename.ts diff --git a/packages/agent/src/tools/create-file.ts b/packages/agent/src/tools/create-file.ts index 6ccd84c5..bb1c7e2e 100644 --- a/packages/agent/src/tools/create-file.ts +++ b/packages/agent/src/tools/create-file.ts @@ -6,6 +6,7 @@ import * as path from 'path' import { s3Service } from '@shumai/core/src/s3/s3' import { detectSupportedMimeType } from '@shumai/core/src/utils/mime' import { ulid } from 'ulid' +import { sanitizeFilename } from '@shumai/core/src/utils/filename' function getMimeType(filePath: string): string { try { @@ -64,7 +65,7 @@ export function createCreateFileTool(userId: string): AgentTool() const files = await assetService.getDownloadLinks(req.ids) 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 diff --git a/packages/api/src/public-share.ts b/packages/api/src/public-share.ts index 157e60a8..9687c039 100644 --- a/packages/api/src/public-share.ts +++ b/packages/api/src/public-share.ts @@ -147,6 +147,28 @@ const route = app 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( '/shares/:shareId/files/:fileId/comments', zValidator('query', paginationParamsSchema), diff --git a/packages/api/src/s3.test.ts b/packages/api/src/s3.test.ts index 9a89d76f..59bb4675 100644 --- a/packages/api/src/s3.test.ts +++ b/packages/api/src/s3.test.ts @@ -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 // eslint-disable-next-line @typescript-eslint/no-explicit-any let onFoundCallback: ((path: string, c: any) => void) | undefined @@ -54,26 +54,34 @@ describe('S3 API', () => { const { default: route } = await import('./s3') const app = new Hono().route('/files', route) - // First, verify onFound is called through the mock - await app.request('/files/b1/test.webp?filename=my-file.txt') + await app.request('/files/b1/test.webp?download=1') expect(onFoundCallback).toBeDefined() - // Test the callback directly - const mockContext = { - req: { - query: (key: string) => (key === 'filename' ? 'my-file.txt' : undefined), - }, + // Test with download=1 + const mockContextWithDownload = { + req: { query: (key: string) => (key === 'download' ? '1' : undefined) }, header: vi.fn(), } if (onFoundCallback) { - onFoundCallback('/files/b1/test.webp', mockContext) - expect(mockContext.header).toHaveBeenCalledWith( + onFoundCallback('/files/b1/test.webp', mockContextWithDownload) + expect(mockContextWithDownload.header).toHaveBeenCalledWith( '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 () => { diff --git a/packages/api/src/s3.ts b/packages/api/src/s3.ts index 80055908..38c2bd13 100644 --- a/packages/api/src/s3.ts +++ b/packages/api/src/s3.ts @@ -9,10 +9,8 @@ const route = new Hono() root: './data', rewriteRequestPath: (path) => path.replace(/^\/files\//, ''), onFound: (_path, c) => { - const filename = c.req.query('filename') - if (filename) { - const safeFilename = filename.replace(/["\r\n]/g, '_') - c.header('Content-Disposition', `attachment; filename="${safeFilename}"`) + if (c.req.query('download') === '1') { + c.header('Content-Disposition', 'attachment') } }, }), diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index 840c3d15..a8901163 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -2222,4 +2222,103 @@ describe('AssetService — natural sort by name', () => { 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') + }) + }) }) diff --git a/packages/core/src/asset/asset.ts b/packages/core/src/asset/asset.ts index 9651faf5..82d80344 100644 --- a/packages/core/src/asset/asset.ts +++ b/packages/core/src/asset/asset.ts @@ -842,7 +842,6 @@ export class AssetService { process.env.S3_BUCKET || 'shumai', info.media.original.key, '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) { media.videoPreview.url = await s3Service.presign( process.env.S3_BUCKET || 'shumai', @@ -1593,21 +1592,19 @@ export class AssetService { } const key = latestVersion.storageKey?.key - if (media && key) { - const fileName = - a.type === AssetType.version_stack && (a.name === '' || !a.name) - ? latestVersion.name - : a.name + if (key) { + if (!media) { + media = { + original: null, + videoTranscodes: [], + imageTranscodes: [], + } as unknown as PrismaJson.MediaInfo + } media.original = { key, - downloadUrl: await s3Service.presign( - process.env.S3_BUCKET || 'shumai', - key, - 'GET', - fileName, - ), + downloadUrl: await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET'), 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 if (!key) return null - const url = await s3Service.presign( - process.env.S3_BUCKET || 'shumai', - key, - 'GET', - file.name, - ) + const url = await s3Service.presign(process.env.S3_BUCKET || 'shumai', key, 'GET', true) return { id: file.id, @@ -1863,6 +1855,31 @@ export class AssetService { 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 { + 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 { if (!asset.media) return null diff --git a/packages/core/src/s3/s3.test.ts b/packages/core/src/s3/s3.test.ts index cd6404ff..89e89dd3 100644 --- a/packages/core/src/s3/s3.test.ts +++ b/packages/core/src/s3/s3.test.ts @@ -117,17 +117,44 @@ describe('S3Service implementations', () => { 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) - 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( 'key', 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', () => { @@ -182,9 +209,9 @@ describe('S3Service implementations', () => { expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt') }) - it('should generate a local presign URL with filename', async () => { - const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', 'original.txt') - expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?filename=original.txt') + it('should generate a local presign URL with download param', async () => { + const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET', true) + expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt?download=1') }) it('should throw an error for unsupported presign methods', async () => { diff --git a/packages/core/src/s3/s3.ts b/packages/core/src/s3/s3.ts index 669d4ff7..46145dd0 100644 --- a/packages/core/src/s3/s3.ts +++ b/packages/core/src/s3/s3.ts @@ -54,7 +54,7 @@ export interface S3Service { listObjects(bucket: string, prefix: string): Promise uploadFile(filePath: string, contentType: string): Promise uploadFileToKey(filePath: string, key: string, contentType: string): Promise - presign(bucket: string, key: string, method: string, filename?: string): Promise + presign(bucket: string, key: string, method: string, download?: boolean): Promise } 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 { + async presign(bucket: string, key: string, method: string, download?: boolean): Promise { const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10) const expiresInSeconds = expireHours * 3600 // Cache time is 2/3 of expire time, rounded to minute const cacheMinutes = Math.round((expireHours * 60 * 2) / 3) 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) if (cached) return cached } @@ -225,13 +225,13 @@ export class S3StorageService implements S3Service { method: method as any, } - if (filename) { - options.contentDisposition = `attachment; filename="${filename}"` + if (download) { + options.contentDisposition = 'attachment' } const url = this.client.presign(key, options) - if (method === 'GET') { + if (method === 'GET' && !download) { this.presignCache.set(cacheKey, url, cacheTtlMs) } @@ -435,7 +435,7 @@ export class LocalStorageService implements S3Service { await fs.promises.copyFile(filePath, destPath) } - async presign(bucket: string, key: string, method: string, filename?: string): Promise { + async presign(bucket: string, key: string, method: string, download?: boolean): Promise { if (method !== 'GET' && method !== 'PUT') { throw new Error(`Invalid method: ${method}`) } @@ -443,8 +443,8 @@ export class LocalStorageService implements S3Service { return `${this.endpoint}${signLocalUrl(bucket, key)}` } let url = `${this.endpoint}/files/${bucket}/${key}` - if (filename) { - url += `?filename=${encodeURIComponent(filename)}` + if (download) { + url += '?download=1' } return url } diff --git a/packages/core/src/upload/upload.ts b/packages/core/src/upload/upload.ts index 5bddab71..896493cf 100644 --- a/packages/core/src/upload/upload.ts +++ b/packages/core/src/upload/upload.ts @@ -21,6 +21,7 @@ import { import { ImageTranscoder, VideoTranscoder } from '@shumai/transcode' import { generateKeyBetween } from 'jittered-fractional-indexing' import { ulid } from 'ulid' +import { sanitizeFilename } from '@shumai/core/src/utils/filename' export class UploadService { constructor(private readonly prismaClient: typeof prisma = prisma) {} @@ -130,7 +131,7 @@ export class UploadService { const assetType = file.type === 'folder' ? AssetType.folder : AssetType.file let key: string | null = null if (assetType === AssetType.file) { - key = `files/${ulid()}/raw` + key = `files/${ulid()}/${sanitizeFilename(file.name)}` } const newAsset = await tx.asset.create({ diff --git a/packages/core/src/utils/filename.test.ts b/packages/core/src/utils/filename.test.ts new file mode 100644 index 00000000..42c32882 --- /dev/null +++ b/packages/core/src/utils/filename.test.ts @@ -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') + }) +}) diff --git a/packages/core/src/utils/filename.ts b/packages/core/src/utils/filename.ts new file mode 100644 index 00000000..f13db39b --- /dev/null +++ b/packages/core/src/utils/filename.ts @@ -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 +} diff --git a/packages/dtos/src/asset.ts b/packages/dtos/src/asset.ts index 57503246..54eaae8e 100644 --- a/packages/dtos/src/asset.ts +++ b/packages/dtos/src/asset.ts @@ -279,6 +279,7 @@ export interface ImageTranscode { export interface VideoTranscode { id: string url: string + key: string width: number height: number size: number diff --git a/packages/transcode/src/activities/transcode.ts b/packages/transcode/src/activities/transcode.ts index 707ea171..dc6e6477 100644 --- a/packages/transcode/src/activities/transcode.ts +++ b/packages/transcode/src/activities/transcode.ts @@ -2,6 +2,7 @@ import { prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db' import { s3Service } from '@shumai/core/src/s3/s3' import { transcodeService } from '@shumai/core' import { metadataService } from '@shumai/core/src/metadata/metadata' +import { stemFromKey } from '@shumai/core/src/utils/filename' import { ApplicationFailure } from '@temporalio/activity' import * as path from 'path' import * as fs from 'fs' @@ -162,7 +163,9 @@ export async function transcodeVideoActivity( params: VideoActivityParams, ): Promise { 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 { await s3Service.headObject(bucket, key) @@ -172,7 +175,7 @@ export async function transcodeVideoActivity( } 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 { let targetFps: number | string = params.originalFps || 30 @@ -237,7 +240,11 @@ export async function transcodeImageActivity( params: ImageActivityParams, ): Promise { 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 { await s3Service.headObject(bucket, key) @@ -247,7 +254,7 @@ export async function transcodeImageActivity( } 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 { await transcodeService.transcodeImage( diff --git a/packages/webui/components/breadcrumb-nav.tsx b/packages/webui/components/breadcrumb-nav.tsx index fb47055b..1acf444a 100644 --- a/packages/webui/components/breadcrumb-nav.tsx +++ b/packages/webui/components/breadcrumb-nav.tsx @@ -1,8 +1,10 @@ import { Badge } from '@/ui/components/ui/badge' +import { client } from '@/ui/api/client' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, + DropdownMenuLabel, DropdownMenuPortal, DropdownMenuSeparator, DropdownMenuSub, @@ -44,6 +46,15 @@ interface BreadcrumbNavProps { shareId?: string onFolderClick?: (folderId: string) => void fileId?: string + downloadInfo?: { + originalKey?: string + videoTranscodes?: Array<{ + key: string + width: number + height: number + isRaw?: boolean + }> + } versions?: Array<{ id: string version: number @@ -69,6 +80,8 @@ export function BreadcrumbNav({ isPublic = false, onFolderClick, fileId, + shareId, + downloadInfo, versions, }: BreadcrumbNavProps) { 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 ? [ { @@ -155,9 +195,63 @@ export function BreadcrumbNav({ - console.log('Download')}> - Download - + {hasVideoTranscodes ? ( + + + {m.download()} + + + + {m.download()} + {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 ( + handleDownload(t.key)} + className="flex items-center justify-between" + > + {resolution} + MP4 + + ) + })} + {downloadInfo?.originalKey && ( + <> + + handleDownload(downloadInfo.originalKey!)} + className="flex items-center justify-between" + > + Original + + {currentAsset.name?.split('.').pop()?.toUpperCase() || 'RAW'} + + + + )} + + + + ) : ( + + downloadInfo?.originalKey && handleDownload(downloadInfo.originalKey) + } + disabled={!downloadInfo?.originalKey} + > + {m.download()} + + )} {canEdit && ( console.log('Rename')}> Rename diff --git a/packages/webui/components/file-viewer.tsx b/packages/webui/components/file-viewer.tsx index 5c38184a..d1015288 100644 --- a/packages/webui/components/file-viewer.tsx +++ b/packages/webui/components/file-viewer.tsx @@ -1,4 +1,5 @@ import { useScreenSize } from '@/ui/hooks/useScreenSize' +import { client } from '@/ui/api/client' import { getBestTranscode } from '@/ui/lib/media' import type { AssetInfo } from '@shumai/dtos' import { Check, Copy, Download, Minus, Plus } from 'lucide-react' @@ -19,6 +20,7 @@ type FileViewerProps = { onTimeUpdate?: (time: number) => void annotations?: Annotation[] startTime?: number + shareId?: string children?: ReactNode } @@ -30,6 +32,7 @@ export function FileViewer({ onTimeUpdate, annotations, startTime, + shareId, children, }: FileViewerProps) { const { width: screenWidth } = useScreenSize() @@ -133,15 +136,29 @@ export function FileViewer({ } } - const handleDownload = () => { - const url = file.media?.original?.downloadUrl - if (!url) return - const link = document.createElement('a') - link.href = url - link.download = file.name || 'image' - document.body.appendChild(link) - link.click() - document.body.removeChild(link) + const handleDownload = async () => { + const key = file.media?.original?.key + if (!key || !file.id) return + try { + const res = shareId + ? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({ + param: { shareId, fileId: file.id }, + json: { key }, + }) + : 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 () => { @@ -210,7 +227,7 @@ export function FileViewer({
diff --git a/packages/webui/components/top-nav.tsx b/packages/webui/components/top-nav.tsx index 041ff2e7..55909a04 100644 --- a/packages/webui/components/top-nav.tsx +++ b/packages/webui/components/top-nav.tsx @@ -19,6 +19,7 @@ export function TopNav() { currentAsset, isRootFolder, fileId, + downloadInfo, versions, isPublic, shareId, @@ -87,6 +88,7 @@ export function TopNav() { isPublic={isPublic} shareId={shareId} fileId={fileId} + downloadInfo={downloadInfo} versions={versions} onFolderClick={onFolderClick} /> diff --git a/packages/webui/components/viewers/video-player.tsx b/packages/webui/components/viewers/video-player.tsx index 394222ab..732ce951 100644 --- a/packages/webui/components/viewers/video-player.tsx +++ b/packages/webui/components/viewers/video-player.tsx @@ -1,4 +1,5 @@ import type { AssetInfo, VideoTranscode } from '@shumai/dtos' +import { client } from '@/ui/api/client' import { cn } from '@/ui/lib/utils' import { Check, @@ -58,6 +59,7 @@ interface VideoPlayerProps { onTimeUpdate?: (time: number) => void annotations?: Annotation[] startTime?: number + shareId?: string children?: React.ReactNode } @@ -76,7 +78,7 @@ interface ControlBarProps { handleVolumeChange: (newVolume: number) => void changePlaybackRate: (rate: number) => void changeResolution: (res: DisplayTranscode) => void - handleDownload: (url: string, resolution: string) => void + handleDownload: (key: string) => void toggleFullScreen: () => void onZoomChange: (zoom: number) => void onZoomReset: () => void @@ -344,11 +346,13 @@ const ControlBar: React.FC = ({ {resolutions.map((res) => ( 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" > {res.resolution} - MP4 + + {res.isRaw ? data.name?.split('.').pop()?.toUpperCase() || 'RAW' : 'MP4'} + ))}
@@ -376,6 +380,7 @@ const VideoPlayer: React.FC = ({ onTimeUpdate, annotations, startTime, + shareId, children, }) => { const localPlayerRef = useRef(null) @@ -456,12 +461,16 @@ const VideoPlayer: React.FC = ({ } // Initial resolution setup - const originalRes = { + const originalRes: DisplayTranscode = { + id: 'original', url: data.media.original.downloadUrl, + key: data.media.original.key ?? '', width: data.media.metadata.originalWidth ?? 0, height: data.media.metadata.originalHeight ?? 0, + size: 0, + isRaw: true, resolution: 'Original', - } as DisplayTranscode + } const resolutions: DisplayTranscode[] = (data.media.videoTranscodes ?? []).map((t) => { const longSide = Math.max(t.width, t.height) @@ -837,13 +846,27 @@ const VideoPlayer: React.FC = ({ } } - const handleDownload = (url: string, resolution: string) => { - const link = document.createElement('a') - link.href = url - link.download = `${data.name}-${resolution}.mp4` - document.body.appendChild(link) - link.click() - document.body.removeChild(link) + const handleDownload = async (key: string) => { + try { + const res = shareId + ? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({ + param: { shareId, fileId: data.id! }, + json: { key }, + }) + : 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) diff --git a/packages/webui/routes/projects/$projectId/files/$fileId.tsx b/packages/webui/routes/projects/$projectId/files/$fileId.tsx index 811891bb..7c269d03 100644 --- a/packages/webui/routes/projects/$projectId/files/$fileId.tsx +++ b/packages/webui/routes/projects/$projectId/files/$fileId.tsx @@ -148,6 +148,15 @@ function FileViewPage() { }, isRootFolder: false, 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, onFolderClick: (id: string) => { navigate({ diff --git a/packages/webui/stores/top-nav.ts b/packages/webui/stores/top-nav.ts index 89e23329..1e70f27d 100644 --- a/packages/webui/stores/top-nav.ts +++ b/packages/webui/stores/top-nav.ts @@ -13,6 +13,15 @@ export interface TopNavProjectState { } isRootFolder: boolean fileId?: string + downloadInfo?: { + originalKey?: string + videoTranscodes?: Array<{ + key: string + width: number + height: number + isRaw?: boolean + }> + } versions?: Array<{ id: string version: number