High performance self-hosted photo and video management solution.
TypeScript backup-toolfluttergoogle-photosgoogle-photos-alternativejavascriptmobile-appnestjsnodejsphoto-galleryphotosphotos-managementself-hostedsveltesveltekittypescriptvideos
refactor(server): replace switch statement in sendFile with Record lookup (#16630)
rrrockeyd84009648e94
diff --git a/server/src/utils/file.ts b/server/src/utils/file.tsindex 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<Response['sendFile']>;type SendFileOptions = SendFile[1];+const cacheControlHeaders: Record<CacheControl, string | null> = {+ [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<ImmichFileResponse>,logger: LoggingRepository,): Promise<void> => {+ // promisified version of 'res.sendFile' for cleaner async handlingconst _sendFile = (path: string, options: SendFileOptions) =>promisify<string, SendFileOptions>(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 servingconst options: SendFileOptions = { dotfiles: 'allow' };if (!isAbsolute(file.path)) {options.root = process.cwd();