Change default download filenames to anonymised hash

This commit is contained in:
Alan Grainger
2026-10-08 10:31:35 +13:00
parent e7836b85f7
commit 5cddddd9f7
16 changed files with 328 additions and 83 deletions
+17 -1
View File
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { Asset, AssetType, KeyType, loadConfig, SharedLink } from '@ipp/core'
import { createHash } from 'crypto'
import { Writable } from 'stream'
import { once } from 'events'
import type { Response } from 'express-serve-static-core'
@@ -164,6 +165,7 @@ function countOf (zip: Buffer, signature: string): number {
describe('downloadAssets', () => {
it('streams a valid zip with one entry per asset', async () => {
setConfig({ ipp: { downloadedFilename: 0 } })
vi.stubGlobal('fetch', instantFetch(2048))
const res = new FakeRes()
await downloadAssets(asResponse(res), share, [makeAsset('a1'), makeAsset('a2')])
@@ -286,8 +288,22 @@ describe('downloadAssets', () => {
expect(signals.every(s => s.aborted)).toBe(true)
}, 10_000)
it('numbers multi-select entries by their position in the whole share (downloadedFilename: 2)', async () => {
setConfig({ ipp: { downloadedFilename: 2 } })
vi.stubGlobal('fetch', instantFetch(2048))
const album: SharedLink = { ...share, assets: ['a1', 'a2', 'a3', 'a4', 'a5'].map(makeAsset) }
const prefix = createHash('sha256').update(album.key).digest('hex').slice(0, 8)
const res = new FakeRes()
// Selected out of order: entries follow the request order, names the album order
await downloadAssets(asResponse(res), album, [album.assets[3], album.assets[1]])
expect(centralDirectory(res.output)).toEqual([
{ name: `${prefix}_004.jpg`, size: 2048 },
{ name: `${prefix}_002.jpg`, size: 2048 }
])
})
it('names a WebP preview zip entry .webp', async () => {
setConfig({ ipp: { maxDownloadQuality: 'preview' } })
setConfig({ ipp: { maxDownloadQuality: 'preview', downloadedFilename: 0 } })
const fetchMock = vi.fn(async () => new globalThis.Response(new Uint8Array(2048), {
status: 200,
headers: { 'content-type': 'image/webp' }