mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 21:44:27 +00:00
fix(share): resolve symlinks for download URLs and enable card download action (#274)
This commit is contained in:
@@ -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')
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user