Move attachmentDisposition to filename.ts
This commit is contained in:
@@ -103,6 +103,13 @@ export function downloadFilename (asset: Asset): string {
|
|||||||
return getFilename(asset, resolveImageEndpoint(ImageSize.original, asset).servedSize)
|
return getFilename(asset, resolveImageEndpoint(ImageSize.original, asset).servedSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Download Content-Disposition, RFC 5987-encoded so `,` and `;` can't split the header. */
|
||||||
|
export function attachmentDisposition (filename: string): string {
|
||||||
|
const encoded = encodeURIComponent(filename)
|
||||||
|
.replace(/['()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
|
||||||
|
return `attachment; filename*=UTF-8''${encoded}`
|
||||||
|
}
|
||||||
|
|
||||||
/** Content-type without its parameters, or undefined if absent. */
|
/** Content-type without its parameters, or undefined if absent. */
|
||||||
export function responseMime (response: globalThis.Response): string | undefined {
|
export function responseMime (response: globalThis.Response): string | undefined {
|
||||||
return (response.headers.get('content-type') || '').split(';')[0].trim() || undefined
|
return (response.headers.get('content-type') || '').split(';')[0].trim() || undefined
|
||||||
|
|||||||
@@ -7,12 +7,11 @@ import {
|
|||||||
import { Response } from 'express-serve-static-core'
|
import { Response } from 'express-serve-static-core'
|
||||||
import { Asset, ImageSize, IncomingShareRequest, SharedLink } from '../types'
|
import { Asset, ImageSize, IncomingShareRequest, SharedLink } from '../types'
|
||||||
import { respondToInvalidRequest } from '../invalidRequestHandler'
|
import { respondToInvalidRequest } from '../invalidRequestHandler'
|
||||||
import { getFilename, servedMimeFrom } from '../gallery/filename'
|
import { attachmentDisposition, getFilename, servedMimeFrom } from '../gallery/filename'
|
||||||
import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint } from '../gallery/sizing'
|
import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint } from '../gallery/sizing'
|
||||||
import { pipeline } from 'stream/promises'
|
import { pipeline } from 'stream/promises'
|
||||||
import { readableFromWeb } from '../utils/webStream'
|
import { readableFromWeb } from '../utils/webStream'
|
||||||
import { log } from '../utils/log'
|
import { log } from '../utils/log'
|
||||||
import { attachmentDisposition } from './download'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stream an asset from Immich back to the client.
|
* Stream an asset from Immich back to the client.
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import archiver, { Archiver } from 'archiver'
|
|||||||
import { sanitize } from '../utils/sanitize'
|
import { sanitize } from '../utils/sanitize'
|
||||||
import { resolveDownloadEndpoint, ImageEndpoint } from '../gallery/sizing'
|
import { resolveDownloadEndpoint, ImageEndpoint } from '../gallery/sizing'
|
||||||
import { title } from '../share'
|
import { title } from '../share'
|
||||||
import { getFilename, responseMime, servedMimeFrom } from '../gallery/filename'
|
import { attachmentDisposition, getFilename, responseMime, servedMimeFrom } from '../gallery/filename'
|
||||||
import { readableFromWeb } from '../utils/webStream'
|
import { readableFromWeb } from '../utils/webStream'
|
||||||
import { respondToInvalidRequest } from '../invalidRequestHandler'
|
import { respondToInvalidRequest } from '../invalidRequestHandler'
|
||||||
|
|
||||||
@@ -238,14 +238,6 @@ function enrichFromHeaders (asset: Asset, response: globalThis.Response): Asset
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function attachmentDisposition (filename: string): string {
|
|
||||||
// encodeURI leaves `,` and `;` raw, which split the header: Chrome reads a
|
|
||||||
// comma as a second Content-Disposition and refuses the download.
|
|
||||||
const encoded = encodeURIComponent(filename)
|
|
||||||
.replace(/['()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
|
|
||||||
return `attachment; filename*=UTF-8''${encoded}`
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract a filename from a `Content-Disposition` header. Prefers the RFC 5987
|
* Extract a filename from a `Content-Disposition` header. Prefers the RFC 5987
|
||||||
* `filename*=UTF-8''...` form (percent-decoded) over the plain `filename=`.
|
* `filename*=UTF-8''...` form (percent-decoded) over the plain `filename=`.
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { describe, it, expect } from 'vitest'
|
import { describe, it, expect } from 'vitest'
|
||||||
import { attachmentDisposition, filenameFromContentDisposition } from '../src/stream/download'
|
import { filenameFromContentDisposition } from '../src/stream/download'
|
||||||
|
import { attachmentDisposition } from '../src/gallery/filename'
|
||||||
|
|
||||||
// Used by the zip download path to recover the real filename for album grid
|
// Used by the zip download path to recover the real filename for album grid
|
||||||
// assets (which arrive without originalFileName) from the /original response.
|
// assets (which arrive without originalFileName) from the /original response.
|
||||||
|
|||||||
Reference in New Issue
Block a user