From 6bd4723926e8a122102746a09c2023bac416a872 Mon Sep 17 00:00:00 2001
From: Alan Grainger <16197738+alangrainger@users.noreply.github.com>
Date: Tue, 28 Oct 2025 17:24:27 +0800
Subject: [PATCH] #149 Fix issue where the original filename was not always
present for downloaded file
---
README.md | 24 ++++++++++++------------
app/src/render.ts | 35 +++++++++++++++++++++--------------
2 files changed, 33 insertions(+), 26 deletions(-)
diff --git a/README.md b/README.md
index f18a3bb..cd53373 100644
--- a/README.md
+++ b/README.md
@@ -146,18 +146,18 @@ Alternatively, you can [pass the configuration inline](docs/inline-configuration
### Immich Public Proxy options
-| Option | Type | Description |
-|-------------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| `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. |
-| `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.
`0` for the current time
`1` for the original filename
`2` for the Immich asset ID number |
-| `showGalleryTitle` | `bool` | Show a title on the gallery page. |
-| `allowDownloadAll` | `int` | Allow visitors to download all files as a zip.
`0` disable downloads
`1` follow Immich setting per share ([example](https://github.com/user-attachments/assets/79ea8c08-71ce-42ab-b025-10aec384938a))
`2` always allowed |
-| `allowSlugLinks` | `bool` | Enable/disable the custom URL links. |
-| `showHomePage` | `bool` | Set to `false` to remove the IPP shield page at `/` and at `/share` |
-| `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. |
+| Option | Type | Description |
+|-------------------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| `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. |
+| `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.
`0` for the original filename if available, falling back to the Immich asset ID
`1` for the Immich asset ID number
`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. |
+| `allowDownloadAll` | `int` | Allow visitors to download all files as a zip.
`0` disable downloads
`1` follow Immich setting per share ([example](https://github.com/user-attachments/assets/79ea8c08-71ce-42ab-b025-10aec384938a))
`2` always allowed |
+| `allowSlugLinks` | `bool` | Enable/disable the custom URL links. |
+| `showHomePage` | `bool` | Set to `false` to remove the IPP shield page at `/` and at `/share` |
+| `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. |
For example, to disable the home page at `/` and at `/share` you need to change `showHomePage` to `false`:
diff --git a/app/src/render.ts b/app/src/render.ts
index f4fecdd..2ccef58 100644
--- a/app/src/render.ts
+++ b/app/src/render.ts
@@ -70,7 +70,7 @@ class Render {
// Add the filename for downloaded assets
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
@@ -111,9 +111,7 @@ class Render {
// 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 now = Date.now()
- for (let i = 0; i < share.assets.length; i++) {
- const asset = share.assets[i]
+ for (const asset of share.assets) {
let video, downloadUrl
if (asset.type === AssetType.video) {
// Populate the data-video property
@@ -139,20 +137,12 @@ class Render {
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, ''') : ''
- // 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
const itemHtml = [
video ? `
`,
+ ` data-download="${this.getFilename(asset)}">
`,
video ? '' : '',
''
].join('')
@@ -202,11 +192,28 @@ class Render {
console.warn(`Failed to fetch asset: ${asset.id}`)
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()
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()