fix(share): resolve symlinks for download URLs and enable card download action (#274)

This commit is contained in:
Yiling-J
2026-08-07 23:24:24 +08:00
committed by GitHub
parent 624a3a6a54
commit 70b6c33204
6 changed files with 117 additions and 10 deletions
+29
View File
@@ -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', () => {
+27 -6
View File
@@ -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')
}