mirror of
https://github.com/immich-app/immich.git
synced 2025-03-11 02:23:09 -05:00
refactor(server): replace switch statement in sendFile with Record lookup (#16630)
* refactor cache control handling in server/utils/file.ts * add ability to null CacheControl.NONE * Cache control handling comment * Added comment to file.ts This comment provides a better understanding of what the cacheControlHeader is doing. * Update file.ts Added comments * Update server/src/utils/file.ts * fix comments in file.ts * run prettier with --write to fix formatting --------- Co-authored-by: pnleguizamo <pnleguizamo@gmail.com> Co-authored-by: drew-kearns <dkearns@iastate.edu> Co-authored-by: Sierra (Izumi) Brown <119357873+SierraIBrown@users.noreply.github.com> Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
parent
fc2df05190
commit
d84009648e
1 changed files with 12 additions and 10 deletions
|
@ -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 handling
|
||||
const _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 serving
|
||||
const options: SendFileOptions = { dotfiles: 'allow' };
|
||||
if (!isAbsolute(file.path)) {
|
||||
options.root = process.cwd();
|
||||
|
|
Loading…
Add table
Reference in a new issue