Add #16 Download all files as zip

This commit is contained in:
Alan Grainger
2024-11-15 15:48:12 +01:00
parent 4a546419d7
commit 9e1970af4f
9 changed files with 85 additions and 16 deletions
+6 -2
View File
@@ -94,8 +94,12 @@ class Immich {
return
}
// Everything is ok - output the link page
if (link.assets.length === 1) {
// Everything is ok - output the shared link data
if (request.mode === 'download' && getConfigOption('ipp.allowDownloadAll', false)) {
// Download all assets as a zip file
await render.downloadAll(res, link)
} else if (link.assets.length === 1) {
// This is an individual item (not a gallery)
log('Serving link ' + request.key)
const asset = link.assets[0]
+3 -2
View File
@@ -19,9 +19,10 @@ app.use(express.static('public', { setHeaders: addResponseHeaders }))
/*
* [ROUTE] This is the main URL that someone would visit if they are opening a shared link
*/
app.get('/share/:key', async (req, res) => {
app.get('/share/:key/:mode?', async (req, res) => {
await immich.handleShareRequest({
key: req.params.key
key: req.params.key,
mode: req.params.mode
}, res)
})
+25
View File
@@ -2,6 +2,7 @@ import immich from './immich'
import { Response } from 'express-serve-static-core'
import { Asset, AssetType, ImageSize, IncomingShareRequest, SharedLink } from './types'
import { getConfigOption } from './functions'
import archiver from 'archiver'
class Render {
lgConfig
@@ -111,6 +112,9 @@ class Render {
items,
openItem,
title: this.title(share),
path: '/share/' + share.key,
showDownload: getConfigOption('ipp.allowDownloadAll', true),
showTitle: getConfigOption('ipp.showGalleryTitle', true),
lgConfig: getConfigOption('lightGallery', {})
})
}
@@ -121,6 +125,27 @@ class Render {
title (share: SharedLink) {
return share.description || share?.album?.albumName || ''
}
/**
* Download all assets as a zip file
*/
async downloadAll (res: Response, share: SharedLink) {
res.setHeader('Content-Type', 'application/zip')
const title = this.title(share).replace(/[^\w .-]/g, '') + '.zip'
res.setHeader('Content-Disposition', `attachment; filename="${title}"`)
const archive = archiver('zip', { zlib: { level: 9 } })
archive.pipe(res)
for (const asset of share.assets) {
const url = immich.buildUrl(immich.apiUrl() + '/assets/' + encodeURIComponent(asset.id) + '/original', {
key: asset.key,
password: asset.password
})
const data = await fetch(url)
archive.append(Buffer.from(await data.arrayBuffer()), { name: asset.originalFileName || asset.id })
}
await archive.finalize()
res.end()
}
}
const render = new Render()
+2
View File
@@ -6,6 +6,7 @@ export enum AssetType {
export interface Asset {
id: string;
key: string;
originalFileName?: string;
password?: string;
type: AssetType;
isTrashed: boolean;
@@ -39,6 +40,7 @@ export enum ImageSize {
export interface IncomingShareRequest {
key: string;
password?: string;
mode?: string;
size?: ImageSize;
range?: string;
}