From 6608d8ca5b78057e5bb18c68b423fa244f5aadb7 Mon Sep 17 00:00:00 2001 From: Alan Grainger Date: Mon, 11 Nov 2024 19:34:49 +0100 Subject: [PATCH] Update docs --- app/src/encrypt.ts | 6 +++--- app/src/functions.ts | 5 ++++- app/src/immich.ts | 8 ++++---- app/src/render.ts | 6 +++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/src/encrypt.ts b/app/src/encrypt.ts index 275d1f9..3cc092c 100644 --- a/app/src/encrypt.ts +++ b/app/src/encrypt.ts @@ -1,6 +1,6 @@ import crypto from 'crypto' -interface Payload { +interface EncryptedPayload { iv: string; cr: string; // Encrypted data } @@ -9,7 +9,7 @@ interface Payload { const key = crypto.randomBytes(32) const algorithm = 'aes-256-cbc' -export function encrypt (text: string): Payload { +export function encrypt (text: string): EncryptedPayload { try { const ivBuf = crypto.randomBytes(16) const cipher = crypto.createCipheriv(algorithm, Buffer.from(key), ivBuf) @@ -27,7 +27,7 @@ export function encrypt (text: string): Payload { } } -export function decrypt (payload: Payload) { +export function decrypt (payload: EncryptedPayload) { try { const decipher = crypto.createDecipheriv(algorithm, Buffer.from(key), Buffer.from(payload.iv, 'hex')) let decrypted = decipher.update(payload.cr, 'hex', 'utf8') diff --git a/app/src/functions.ts b/app/src/functions.ts index 1b9ea65..2604010 100644 --- a/app/src/functions.ts +++ b/app/src/functions.ts @@ -9,7 +9,7 @@ try { } catch (e) { } /** - * Get a configuration option using dotted notation. + * Get a configuration option fron config.json using dotted notation. * * @param path * @param [defaultOption] - Specify a default option to return if no configuation value is found @@ -39,6 +39,9 @@ export function getSize (req: Request) { return req.query?.size === 'thumbnail' ? ImageSize.thumbnail : ImageSize.original } +/** + * Force a value to be a string + */ export function toString (value: unknown) { return typeof value === 'string' ? value : '' } diff --git a/app/src/immich.ts b/app/src/immich.ts index b1a33ab..b75a126 100644 --- a/app/src/immich.ts +++ b/app/src/immich.ts @@ -45,8 +45,8 @@ class Immich { */ async handleShareRequest (request: IncomingShareRequest, res: Response) { addResponseHeaders(res) + // Check that the key is a valid format if (!immich.isKey(request.key)) { - // This is not a valid key format log('Invalid share key ' + request.key) res.status(404).send() } else { @@ -61,13 +61,14 @@ class Immich { res.status(401).send() } else if (sharedLinkRes.passwordRequired) { // Password required - show the visitor the password page - // `req.params.key` should already be sanitised at this point, but it never hurts to be explicit + // `req.params.key` is already sanitised at this point, but it never hurts to be explicit const key = request.key.replace(/[^\w-]/g, '') res.render('password', { key, lgConfig: render.lgConfig }) } else if (sharedLinkRes.link) { // Valid shared link const link = sharedLinkRes.link if (!link.assets.length) { + // Immich didn't return any assets for this link (empty array) log('No assets for key ' + request.key) res.status(404).send() } else if (link.assets.length === 1) { @@ -105,8 +106,7 @@ class Immich { password }) const res = await fetch(url) - const contentType = res.headers.get('Content-Type') || '' - if (contentType.includes('application/json')) { + if ((res.headers.get('Content-Type') || '').includes('application/json')) { const jsonBody = await res.json() if (jsonBody) { if (res.status === 200) { diff --git a/app/src/render.ts b/app/src/render.ts index 3d0e4dc..8e83cc7 100644 --- a/app/src/render.ts +++ b/app/src/render.ts @@ -20,7 +20,7 @@ class Render { const subpath = asset.type === AssetType.video ? '/video/playback' : '/' + size const headers = { range: '' } - // Stream the video in 2.5MB chunks + // For videos, request them in 2.5MB chunks rather than the entire video if (asset.type === AssetType.video) { const range = (req.range || '').replace(/bytes=/, '').split('-') const start = parseInt(range[0], 10) || 0 @@ -39,12 +39,12 @@ class Render { // Return the response to the client if (data.status >= 200 && data.status < 300) { - // Populate the response headers + // Populate the whitelisted response headers headerList.forEach(header => { const value = data.headers.get(header) if (value) res.setHeader(header, value) }) - // Return the body + // Return the Immich asset binary data await data.body?.pipeTo( new WritableStream({ write (chunk) { res.write(chunk) }