diff --git a/server/src/utils/file.ts b/server/src/utils/file.ts index 915c60de2a..716e0b1957 100644 --- a/server/src/utils/file.ts +++ b/server/src/utils/file.ts @@ -33,27 +33,28 @@ export class ImmichFileResponse { type SendFile = Parameters; type SendFileOptions = SendFile[1]; +const cacheControlHeaders: Record = { + [CacheControl.PRIVATE_WITH_CACHE]: 'private, max-age=86400, no-transform', + [CacheControl.PRIVATE_WITHOUT_CACHE]: 'private, no-cache, no-transform', + [CacheControl.NONE]: null, // falsy value to prevent adding Cache-Control header +}; + export const sendFile = async ( res: Response, next: NextFunction, handler: () => Promise, logger: LoggingRepository, ): Promise => { + // promisified version of 'res.sendFile' for cleaner async handling const _sendFile = (path: string, options: SendFileOptions) => promisify(res.sendFile).bind(res)(path, options); try { const file = await handler(); - switch (file.cacheControl) { - case CacheControl.PRIVATE_WITH_CACHE: { - res.set('Cache-Control', 'private, max-age=86400, no-transform'); - break; - } - - case CacheControl.PRIVATE_WITHOUT_CACHE: { - res.set('Cache-Control', 'private, no-cache, no-transform'); - break; - } + const cacheControlHeader = cacheControlHeaders[file.cacheControl]; + if (cacheControlHeader) { + // set the header to Cache-Control + res.set('Cache-Control', cacheControlHeader); } res.header('Content-Type', file.contentType); @@ -61,6 +62,7 @@ export const sendFile = async ( res.header('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(file.fileName)}`); } + // configure options for serving const options: SendFileOptions = { dotfiles: 'allow' }; if (!isAbsolute(file.path)) { options.root = process.cwd();