Change default download filenames to anonymised hash
This commit is contained in:
@@ -112,7 +112,7 @@ describe('buildAssetMetadata', () => {
|
||||
})
|
||||
|
||||
it('always returns a download filename', () => {
|
||||
setConfig({})
|
||||
setConfig({ ipp: { downloadedFilename: 0 } })
|
||||
const meta = buildAssetMetadata(asset(), share())
|
||||
// HEIC original served as preview JPEG when downloadOriginalPhoto defaults on
|
||||
expect(meta.downloadFilename).toMatch(/IMG_1234/)
|
||||
|
||||
@@ -224,6 +224,7 @@ describe('assetBuffer streaming', () => {
|
||||
}
|
||||
|
||||
it('sends an attachment named from Immich\'s header, with nosniff', async () => {
|
||||
setConfig({ ipp: { downloadedFilename: 0 } })
|
||||
vi.stubGlobal('fetch', svgFetch({ 'content-disposition': 'attachment; filename="drawing.svg"' }))
|
||||
const res = new FakeRes()
|
||||
await assetBuffer(makeRequest(), asResponse(res), albumAsset, ImageSize.original)
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { Asset, AssetType, KeyType } from '@ipp/core'
|
||||
import { dateSortComparator } from '../src/gallery/builder'
|
||||
import { describe, it, expect, afterEach } from 'vitest'
|
||||
import { Asset, AssetType, KeyType, loadConfig } from '@ipp/core'
|
||||
import { dateSortComparator, displayOrder } from '../src/gallery/order'
|
||||
|
||||
/*
|
||||
The date-grouping sort must follow the album's own order while
|
||||
keeping undated assets last in BOTH directions.
|
||||
keeping undated assets last in BOTH directions. displayOrder applies it,
|
||||
after the album's own sort, when `gallery.groupByDate` is on.
|
||||
*/
|
||||
|
||||
afterEach(() => {
|
||||
delete process.env.CONFIG
|
||||
loadConfig()
|
||||
})
|
||||
|
||||
const asset = (localDateTime?: string): Asset => ({
|
||||
id: localDateTime || 'undated',
|
||||
key: 'k',
|
||||
@@ -57,3 +63,37 @@ describe('dateSortComparator', () => {
|
||||
expect(sorted[1]).toBe(a)
|
||||
})
|
||||
})
|
||||
|
||||
describe('displayOrder', () => {
|
||||
const dated = (id: string, fileCreatedAt: string, localDateTime?: string): Asset =>
|
||||
({ ...asset(localDateTime), id, fileCreatedAt })
|
||||
// Capture order c, a, b; local time disagrees for b (shot late at night)
|
||||
const album = () => [
|
||||
dated('a', '2024-02-01T10:00:00.000Z'),
|
||||
dated('b', '2024-03-01T01:00:00.000Z', '2024-01-31T23:00:00.000Z'),
|
||||
dated('c', '2024-01-01T10:00:00.000Z')
|
||||
]
|
||||
const ids = (assets: Asset[]) => assets.map(a => a.id)
|
||||
|
||||
it('leaves an orderless share as Immich returned it', () => {
|
||||
expect(ids(displayOrder(album()))).toEqual(['a', 'b', 'c'])
|
||||
})
|
||||
|
||||
it('sorts an album by capture time in its own direction', () => {
|
||||
expect(ids(displayOrder(album(), 'asc'))).toEqual(['c', 'a', 'b'])
|
||||
expect(ids(displayOrder(album(), 'desc'))).toEqual(['b', 'a', 'c'])
|
||||
})
|
||||
|
||||
it('sorts by local time when date grouping is on, so buckets stay contiguous', () => {
|
||||
process.env.CONFIG = JSON.stringify({ ipp: { gallery: { groupByDate: 'day' } } })
|
||||
loadConfig()
|
||||
expect(ids(displayOrder(album(), 'asc'))).toEqual(['c', 'b', 'a'])
|
||||
// Newest-first without an album order
|
||||
expect(ids(displayOrder(album()))).toEqual(['a', 'b', 'c'])
|
||||
})
|
||||
|
||||
it('sorts in place and returns the same array', () => {
|
||||
const assets = album()
|
||||
expect(displayOrder(assets, 'asc')).toBe(assets)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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' }
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { AssetType, KeyType } from '@ipp/core'
|
||||
import type { Asset } from '@ipp/core'
|
||||
import type { Asset, SharedLink } from '@ipp/core'
|
||||
import { createHash } from 'crypto'
|
||||
import { ImageSize } from '../src/types'
|
||||
import { getFilename } from '../src/gallery/filename'
|
||||
import { findPositionInShare, getFilename, positionInShare, positionStem } from '../src/gallery/filename'
|
||||
|
||||
/*
|
||||
getFilename reads `ipp.downloadedFilename` via getConfigOption; mock it in
|
||||
@ipp/core so tests exercise the default (original filename) mode
|
||||
without loading a real config file.
|
||||
@ipp/core so tests pick the naming mode directly, without loading a real
|
||||
config file. Tests start in mode 0 (original filename); the position-name
|
||||
tests switch to 2.
|
||||
*/
|
||||
const mocked = vi.hoisted(() => ({ downloadedFilename: 0 as unknown }))
|
||||
vi.mock('@ipp/core', async (importOriginal) => ({
|
||||
...await importOriginal<typeof import('@ipp/core')>(),
|
||||
getConfigOption: (_path: string, fallback?: unknown) => fallback
|
||||
getConfigOption: (path: string, fallback?: unknown) =>
|
||||
path === 'ipp.downloadedFilename' ? mocked.downloadedFilename ?? fallback : fallback
|
||||
}))
|
||||
|
||||
afterEach(() => { mocked.downloadedFilename = 0 })
|
||||
|
||||
function videoAsset (originalFileName: string, originalMimeType = 'video/x-msvideo'): Asset {
|
||||
return {
|
||||
id: 'asset-id',
|
||||
@@ -75,3 +81,87 @@ describe('getFilename WebP previews', () => {
|
||||
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview)).toBe('holiday.jpg')
|
||||
})
|
||||
})
|
||||
|
||||
/** A share of `count` images whose ids are `asset-1` .. `asset-N`. */
|
||||
function shareOf (count: number, key = 'sharekey', keyType = KeyType.key): SharedLink {
|
||||
const assets: Asset[] = Array.from({ length: count }, (_, i) => ({ ...imageAsset(`IMG_${i + 1}.heic`), id: `asset-${i + 1}` }))
|
||||
return { key, keyType, slug: keyType === KeyType.slug ? 'my-album' : null, type: 'ALBUM', assets, expiresAt: null }
|
||||
}
|
||||
|
||||
const HASH_OF_SHAREKEY = createHash('sha256').update('sharekey').digest('hex').slice(0, 8)
|
||||
|
||||
describe('getFilename position names (downloadedFilename: 2)', () => {
|
||||
it('names by share hash and position, with the served extension', () => {
|
||||
mocked.downloadedFilename = 2
|
||||
const share = shareOf(5)
|
||||
expect(getFilename(share.assets[0], ImageSize.original, undefined, positionInShare(share, 0))).toBe(`${HASH_OF_SHAREKEY}_001.heic`)
|
||||
expect(getFilename(share.assets[4], ImageSize.original, undefined, positionInShare(share, 4))).toBe(`${HASH_OF_SHAREKEY}_005.heic`)
|
||||
})
|
||||
|
||||
it('never puts the share key itself in the name', () => {
|
||||
expect(HASH_OF_SHAREKEY).toMatch(/^[0-9a-f]{8}$/)
|
||||
expect(positionStem(positionInShare(shareOf(1), 0))).not.toContain('sharekey')
|
||||
})
|
||||
|
||||
it('pads to the digits of the asset count, minimum three', () => {
|
||||
const stem = (count: number, index: number) => positionStem(positionInShare(shareOf(count), index)).split('_')[1]
|
||||
expect(stem(1, 0)).toBe('001')
|
||||
expect(stem(9, 8)).toBe('009')
|
||||
expect(stem(10, 9)).toBe('010')
|
||||
expect(stem(999, 998)).toBe('999')
|
||||
expect(stem(1000, 0)).toBe('0001')
|
||||
expect(stem(1000, 999)).toBe('1000')
|
||||
})
|
||||
|
||||
it('gives the same prefix whether the share was opened by key or by slug', () => {
|
||||
const byKey = positionStem(positionInShare(shareOf(3, 'sharekey', KeyType.key), 1))
|
||||
const bySlug = positionStem(positionInShare(shareOf(3, 'sharekey', KeyType.slug), 1))
|
||||
expect(bySlug).toBe(byKey)
|
||||
expect(positionStem(positionInShare(shareOf(3, 'otherkey'), 1))).not.toBe(byKey)
|
||||
})
|
||||
|
||||
it('is deterministic for an unchanged share', () => {
|
||||
const a = positionStem(positionInShare(shareOf(20), 7))
|
||||
const b = positionStem(positionInShare(shareOf(20), 7))
|
||||
expect(a).toBe(b)
|
||||
})
|
||||
|
||||
it('finds an asset by id, and a motion photo clip by its still', () => {
|
||||
const share = shareOf(4)
|
||||
share.assets[2].livePhotoVideoId = 'clip-id'
|
||||
expect(findPositionInShare(share, 'asset-2')).toEqual({ shareKey: 'sharekey', position: 2, count: 4 })
|
||||
expect(findPositionInShare(share, 'clip-id')).toEqual({ shareKey: 'sharekey', position: 3, count: 4 })
|
||||
expect(findPositionInShare(share, 'not-shared')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('falls back to the asset id when there is no position', () => {
|
||||
mocked.downloadedFilename = 2
|
||||
expect(getFilename(imageAsset('holiday.heic', 'image/jpeg'))).toBe('asset-id.jpg')
|
||||
})
|
||||
|
||||
it('is the mode used when the config omits downloadedFilename', () => {
|
||||
mocked.downloadedFilename = undefined
|
||||
const share = shareOf(1)
|
||||
expect(getFilename(share.assets[0], ImageSize.original, undefined, positionInShare(share, 0))).toBe(`${HASH_OF_SHAREKEY}_001.heic`)
|
||||
})
|
||||
|
||||
it('follows the served bytes for the extension, as the other modes do', () => {
|
||||
mocked.downloadedFilename = 2
|
||||
const share = shareOf(2)
|
||||
const position = positionInShare(share, 1)
|
||||
expect(getFilename(share.assets[1], ImageSize.preview, undefined, position)).toBe(`${HASH_OF_SHAREKEY}_002.jpg`)
|
||||
expect(getFilename(share.assets[1], ImageSize.preview, 'image/webp', position)).toBe(`${HASH_OF_SHAREKEY}_002.webp`)
|
||||
expect(getFilename(share.assets[1], ImageSize.thumbnail, undefined, position)).toBe(`${HASH_OF_SHAREKEY}_002.webp`)
|
||||
expect(getFilename(videoAsset('clip.avi'), ImageSize.original, 'video/mp4', position)).toBe(`${HASH_OF_SHAREKEY}_002.mp4`)
|
||||
expect(getFilename(videoAsset('clip.avi'), ImageSize.original, undefined, position)).toBe(`${HASH_OF_SHAREKEY}_002.avi`)
|
||||
})
|
||||
|
||||
it('leaves modes 0 and 1 unchanged', () => {
|
||||
const share = shareOf(2)
|
||||
const position = positionInShare(share, 0)
|
||||
mocked.downloadedFilename = 0
|
||||
expect(getFilename(share.assets[0], ImageSize.original, undefined, position)).toBe('IMG_1.heic')
|
||||
mocked.downloadedFilename = 1
|
||||
expect(getFilename(share.assets[0], ImageSize.original, undefined, position)).toBe('asset-1.heic')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user