#149 Fix issue where the original filename was not always present for downloaded file

This commit is contained in:
Alan Grainger
2025-10-28 17:24:27 +08:00
parent 4a97b2e715
commit 6bd4723926
2 changed files with 33 additions and 26 deletions
+12 -12
View File
@@ -146,18 +146,18 @@ Alternatively, you can [pass the configuration inline](docs/inline-configuration
### Immich Public Proxy options ### Immich Public Proxy options
| Option | Type | Description | | Option | Type | Description |
|-------------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |-------------------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `responseHeaders` | `object` | Change the headers sent with your web responses. By default there is `cache-control` and CORS added. | | `responseHeaders` | `object` | Change the headers sent with your web responses. By default there is `cache-control` and CORS added. |
| `singleImageGallery` | `bool` | By default a link to a single image will directly open the image file. Set to `true` if you want to show a gallery page instead for a single item. | | `singleImageGallery` | `bool` | By default a link to a single image will directly open the image file. Set to `true` if you want to show a gallery page instead for a single item. |
| `downloadOriginalPhoto` | `bool` | Set to `false` if you only want people to be able to download the 'preview' quality photo, rather than your original photo. | | `downloadOriginalPhoto` | `bool` | Set to `false` if you only want people to be able to download the 'preview' quality photo, rather than your original photo. |
| `downloadedFilename` | `int` | The filename of the downloaded image.<br>`0` for the current time<br>`1` for the original filename<br>`2` for the Immich asset ID number | | `downloadedFilename` | `int` | The filename of the downloaded image.<br>`0` for the original filename if available, falling back to the Immich asset ID<br>`1` for the Immich asset ID number<br>`2` for a sanitised version of the asset ID: 'img_' plus the first 8 characters of the asset ID |
| `showGalleryTitle` | `bool` | Show a title on the gallery page. | | `showGalleryTitle` | `bool` | Show a title on the gallery page. |
| `allowDownloadAll` | `int` | Allow visitors to download all files as a zip.<br>`0` disable downloads<br>`1` follow Immich setting per share ([example](https://github.com/user-attachments/assets/79ea8c08-71ce-42ab-b025-10aec384938a))<br>`2` always allowed | | `allowDownloadAll` | `int` | Allow visitors to download all files as a zip.<br>`0` disable downloads<br>`1` follow Immich setting per share ([example](https://github.com/user-attachments/assets/79ea8c08-71ce-42ab-b025-10aec384938a))<br>`2` always allowed |
| `allowSlugLinks` | `bool` | Enable/disable the custom URL links. | | `allowSlugLinks` | `bool` | Enable/disable the custom URL links. |
| `showHomePage` | `bool` | Set to `false` to remove the IPP shield page at `/` and at `/share` | | `showHomePage` | `bool` | Set to `false` to remove the IPP shield page at `/` and at `/share` |
| `showMetadata` | `object` | See the [Metadata](#metadata) section below. | | `showMetadata` | `object` | See the [Metadata](#metadata) section below. |
| `customInvalidResponse` | various | Send a custom response instead of the default 404 - see [Custom responses](docs/custom-responses.md) for more details. | | `customInvalidResponse` | various | Send a custom response instead of the default 404 - see [Custom responses](docs/custom-responses.md) for more details. |
For example, to disable the home page at `/` and at `/share` you need to change `showHomePage` to `false`: For example, to disable the home page at `/` and at `/share` you need to change `showHomePage` to `false`:
+21 -14
View File
@@ -70,7 +70,7 @@ class Render {
// Add the filename for downloaded assets // Add the filename for downloaded assets
if (size === ImageSize.original && asset.originalFileName && getConfigOption('ipp.downloadOriginalPhoto', true)) { if (size === ImageSize.original && asset.originalFileName && getConfigOption('ipp.downloadOriginalPhoto', true)) {
res.setHeader('Content-Disposition', `attachment; filename="${asset.originalFileName}"`) res.setHeader('Content-Disposition', `attachment; filename="${this.getFilename(asset)}"`)
} }
// Return the response to the client // Return the response to the client
@@ -111,9 +111,7 @@ class Render {
// You can specify this in your docker-compose file, or send it dynamically as a `publicBaseUrl` header // You can specify this in your docker-compose file, or send it dynamically as a `publicBaseUrl` header
const publicBaseUrl = process.env.PUBLIC_BASE_URL || res.req.headers.publicBaseUrl || (res.req.protocol + '://' + res.req.headers.host) const publicBaseUrl = process.env.PUBLIC_BASE_URL || res.req.headers.publicBaseUrl || (res.req.protocol + '://' + res.req.headers.host)
const now = Date.now() for (const asset of share.assets) {
for (let i = 0; i < share.assets.length; i++) {
const asset = share.assets[i]
let video, downloadUrl let video, downloadUrl
if (asset.type === AssetType.video) { if (asset.type === AssetType.video) {
// Populate the data-video property // Populate the data-video property
@@ -139,20 +137,12 @@ class Render {
const previewUrl = immich.photoUrl(share.key, asset.id, immich.getPreviewImageSize(asset)) const previewUrl = immich.photoUrl(share.key, asset.id, immich.getPreviewImageSize(asset))
const description = getConfigOption('ipp.showMetadata.description', false) && typeof asset?.exifInfo?.description === 'string' ? asset.exifInfo.description.replace(/'/g, '&apos;') : '' const description = getConfigOption('ipp.showMetadata.description', false) && typeof asset?.exifInfo?.description === 'string' ? asset.exifInfo.description.replace(/'/g, '&apos;') : ''
// Filename for the downloaded image
let filename = 'img_' + (now + i)
if (getConfigOption('ipp.downloadedFilename') === 1) {
filename = asset.originalFileName || filename
} else if (getConfigOption('ipp.downloadedFilename') === 2) {
filename = asset.id
}
// Create the full HTML element source to pass to the gallery view // Create the full HTML element source to pass to the gallery view
const itemHtml = [ const itemHtml = [
video ? `<a data-video='${video}'` : `<a href="${previewUrl}"`, video ? `<a data-video='${video}'` : `<a href="${previewUrl}"`,
downloadUrl ? ` data-download-url="${downloadUrl}"` : '', downloadUrl ? ` data-download-url="${downloadUrl}"` : '',
description ? ` data-sub-html='<p>${description}</p>'` : '', description ? ` data-sub-html='<p>${description}</p>'` : '',
` data-download="${filename}"><img alt="" src="${thumbnailUrl}"/>`, ` data-download="${this.getFilename(asset)}"><img alt="" src="${thumbnailUrl}"/>`,
video ? '<div class="play-icon"></div>' : '', video ? '<div class="play-icon"></div>' : '',
'</a>' '</a>'
].join('') ].join('')
@@ -202,11 +192,28 @@ class Render {
console.warn(`Failed to fetch asset: ${asset.id}`) console.warn(`Failed to fetch asset: ${asset.id}`)
continue continue
} }
archive.append(Buffer.from(await data.arrayBuffer()), { name: asset.originalFileName || asset.id }) archive.append(Buffer.from(await data.arrayBuffer()), { name: this.getFilename(asset) })
} }
await archive.finalize() await archive.finalize()
archive.on('end', () => res.end()) archive.on('end', () => res.end())
} }
/**
* Generate a filename for the downloaded asset based on the configuration option chosen
*/
getFilename (asset: Asset) {
switch (getConfigOption('ipp.downloadedFilename')) {
case 1:
// Immich's ID number for this asset
return asset.id
case 2:
// A sanitised version of the ID number
return 'img_' + asset.id.slice(0, 8)
default:
// By default, it will choose the asset's original filename
return asset.originalFileName || asset.id
}
}
} }
const render = new Render() const render = new Render()