mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 05:08:22 +00:00
fix(share): resolve symlinks for download URLs and enable card download action (#274)
This commit is contained in:
@@ -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<ReturnType<typeof shareService.verifyPublicAccess>>,
|
||||
)
|
||||
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')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -1985,19 +1985,40 @@ export class AssetService {
|
||||
* that belong to an asset they have access to.
|
||||
*/
|
||||
async getDownloadUrl(assetId: string, key: string): Promise<string> {
|
||||
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')
|
||||
}
|
||||
|
||||
|
||||
@@ -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<AssetInfo | null>(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
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ interface UseFileActionsProps {
|
||||
folders: AssetInfo[]
|
||||
files: AssetInfo[]
|
||||
selectedIds: Set<string>
|
||||
isPublic?: boolean
|
||||
shareId?: string
|
||||
}
|
||||
|
||||
export function useFileActions({
|
||||
@@ -25,6 +27,8 @@ export function useFileActions({
|
||||
folders,
|
||||
files,
|
||||
selectedIds,
|
||||
isPublic,
|
||||
shareId,
|
||||
}: UseFileActionsProps) {
|
||||
const [editingItemId, setEditingItemId] = useState<string | null>(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)
|
||||
|
||||
@@ -618,6 +618,7 @@ export function PublicShareManager({
|
||||
isFetchingNextFilesPage={isFetchingNextFilesPage}
|
||||
isShareView={true}
|
||||
isPublic={true}
|
||||
shareId={shareId}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user