diff --git a/packages/api/src/share.test.ts b/packages/api/src/share.test.ts index f11d817b..0ca6f3e2 100644 --- a/packages/api/src/share.test.ts +++ b/packages/api/src/share.test.ts @@ -736,4 +736,25 @@ describe('Share API', () => { expect(body.watermarkConfig.hash).toBe('h1') }) }) + + describe('POST /shares/:shareId/files/:fileId/download-url', () => { + test('Success for symlink asset in share', async () => { + vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue( + {} as unknown as Awaited>, + ) + vi.spyOn(assetService, 'getDownloadUrl').mockResolvedValue('https://s3.example.com/download') + + const res = await app.request('/shares/share1/files/symlink1/download-url', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ key: 'files/target1/fly.png' }), + }) + + expect(res.status).toBe(200) + const body = await res.json() + expect(body.url).toBe('https://s3.example.com/download') + expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('symlink1', undefined) + expect(assetService.getDownloadUrl).toHaveBeenCalledWith('symlink1', 'files/target1/fly.png') + }) + }) }) diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index b7714f3f..72e90f89 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -2617,6 +2617,35 @@ describe('AssetService — natural sort by name', () => { assetService.getDownloadUrl(asset.id, 'files/01TESTULID000000000000000/test.mp4'), ).rejects.toThrow('Asset not found or has no storage key') }) + + it('should return a presigned download URL when given a symlink asset ID', async () => { + const realAsset = await prisma.asset.create({ + data: { + name: 'fly.png', + type: AssetType.file, + status: AssetStatus.uploaded, + storageKey: { + create: { key: 'files/01KZ8VH3QHKQCSQDC5JPXCYT0R/fly.png' }, + }, + }, + }) + + const symlinkAsset = await prisma.asset.create({ + data: { + name: 'fly.png', + type: AssetType.symlink, + status: AssetStatus.uploaded, + targetId: realAsset.id, + }, + }) + + const url = await assetService.getDownloadUrl( + symlinkAsset.id, + 'files/01KZ8VH3QHKQCSQDC5JPXCYT0R/fly.png', + ) + + expect(url).toBe('http://mock-s3-url') + }) }) describe('resolveLatestVersionId', () => { diff --git a/packages/core/src/asset/asset.ts b/packages/core/src/asset/asset.ts index 67ba9615..a0ba4762 100644 --- a/packages/core/src/asset/asset.ts +++ b/packages/core/src/asset/asset.ts @@ -1985,19 +1985,40 @@ export class AssetService { * that belong to an asset they have access to. */ async getDownloadUrl(assetId: string, key: string): Promise { + const targetAssetId = await this.resolveLatestVersionId(assetId) const asset = await this.prismaClient.asset.findUnique({ - where: { id: assetId }, - select: { storageKey: { select: { key: true } } }, + where: { id: targetAssetId }, + select: { id: true, parentId: true, 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)) { + if (key.includes('..')) { + throw new Error('Key does not belong to this asset') + } + + let storageKey = asset.storageKey.key + let assetDir = storageKey.substring(0, storageKey.lastIndexOf('/') + 1) + + if (!key.startsWith(assetDir) && asset.parentId) { + // Check if requested key belongs to another version in the same version stack + const sibling = await this.prismaClient.asset.findFirst({ + where: { + parentId: asset.parentId, + isDeleted: false, + storageKey: { key: { startsWith: key.substring(0, key.lastIndexOf('/') + 1) } }, + }, + select: { storageKey: { select: { key: true } } }, + }) + if (sibling?.storageKey?.key) { + storageKey = sibling.storageKey.key + assetDir = storageKey.substring(0, storageKey.lastIndexOf('/') + 1) + } + } + + if (!key.startsWith(assetDir)) { throw new Error('Key does not belong to this asset') } diff --git a/packages/webui/components/file-browser/file-browser.tsx b/packages/webui/components/file-browser/file-browser.tsx index 07d4a3ca..ef4595e2 100644 --- a/packages/webui/components/file-browser/file-browser.tsx +++ b/packages/webui/components/file-browser/file-browser.tsx @@ -91,6 +91,7 @@ interface FileBrowserProps { // eslint-disable-next-line @typescript-eslint/no-explicit-any onUpdateCollection?: (updates: { name?: string; filter?: any }) => void rootFolderId?: string + shareId?: string } type FileWithId = { @@ -136,6 +137,7 @@ export function FileBrowser({ collection, onUpdateCollection, rootFolderId, + shareId, }: FileBrowserProps) { const [contextMenuItem, setContextMenuItem] = useState(null) const [moveCopyMode, setMoveCopyMode] = useState<'move' | 'copy' | null>(null) @@ -418,6 +420,8 @@ export function FileBrowser({ folders, files: displayedFiles, selectedIds, + isPublic, + shareId, }) const [isEmptyTrashDialogOpen, setIsEmptyTrashDialogOpen] = useState(false) @@ -666,6 +670,8 @@ export function FileBrowser({ ? [...folders, ...files].filter((i) => selectedIds.has(i.id!)) : [item] onRemoveFromShare?.(targetItems) + } else if (action === 'download') { + handleAction('download', item) } return } diff --git a/packages/webui/components/file-browser/use-file-actions.ts b/packages/webui/components/file-browser/use-file-actions.ts index 5ffedb69..a84c99f2 100644 --- a/packages/webui/components/file-browser/use-file-actions.ts +++ b/packages/webui/components/file-browser/use-file-actions.ts @@ -16,6 +16,8 @@ interface UseFileActionsProps { folders: AssetInfo[] files: AssetInfo[] selectedIds: Set + isPublic?: boolean + shareId?: string } export function useFileActions({ @@ -25,6 +27,8 @@ export function useFileActions({ folders, files, selectedIds, + isPublic, + shareId, }: UseFileActionsProps) { const [editingItemId, setEditingItemId] = useState(null) const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false) @@ -215,10 +219,35 @@ export function useFileActions({ setIsLoadingLinks(true) setResolvedFiles([]) try { - const res = await getDownloadLinks({ - json: { ids: items.map((i) => i.id!) }, - }) - setResolvedFiles(res.files) + if (isPublic && shareId) { + const password = localStorage.getItem(`share_pwd_${shareId}`) || '' + const links: Array<{ id: string; name: string; url: string }> = [] + for (const item of items) { + const key = item.media?.original?.key + if (!key || !item.id) continue + const res = await client.api.shares[':shareId'].files[':fileId']['download-url'].$post( + { + param: { shareId, fileId: item.id }, + json: { key }, + }, + { + headers: { + 'x-share-password': password, + }, + }, + ) + if (res.ok) { + const { url } = await res.json() + links.push({ id: item.id, name: item.name || 'download', url }) + } + } + setResolvedFiles(links) + } else { + const res = await getDownloadLinks({ + json: { ids: items.map((i) => i.id!) }, + }) + setResolvedFiles(res.files) + } } catch (error) { toast.error('Failed to prepare download links') setIsDownloadDialogOpen(false) diff --git a/packages/webui/components/public-share-manager.tsx b/packages/webui/components/public-share-manager.tsx index 236c71be..f87b3d51 100644 --- a/packages/webui/components/public-share-manager.tsx +++ b/packages/webui/components/public-share-manager.tsx @@ -618,6 +618,7 @@ export function PublicShareManager({ isFetchingNextFilesPage={isFetchingNextFilesPage} isShareView={true} isPublic={true} + shareId={shareId} /> ) )}