mirror of
https://github.com/immich-app/immich.git
synced 2025-05-30 19:54:52 -04: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
@ -33,27 +33,28 @@ export class ImmichFileResponse {
|
|||||||
type SendFile = Parameters<Response['sendFile']>;
|
type SendFile = Parameters<Response['sendFile']>;
|
||||||
type SendFileOptions = SendFile[1];
|
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 (
|
export const sendFile = async (
|
||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction,
|
next: NextFunction,
|
||||||
handler: () => Promise<ImmichFileResponse>,
|
handler: () => Promise<ImmichFileResponse>,
|
||||||
logger: LoggingRepository,
|
logger: LoggingRepository,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
// promisified version of 'res.sendFile' for cleaner async handling
|
||||||
const _sendFile = (path: string, options: SendFileOptions) =>
|
const _sendFile = (path: string, options: SendFileOptions) =>
|
||||||
promisify<string, SendFileOptions>(res.sendFile).bind(res)(path, options);
|
promisify<string, SendFileOptions>(res.sendFile).bind(res)(path, options);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const file = await handler();
|
const file = await handler();
|
||||||
switch (file.cacheControl) {
|
const cacheControlHeader = cacheControlHeaders[file.cacheControl];
|
||||||
case CacheControl.PRIVATE_WITH_CACHE: {
|
if (cacheControlHeader) {
|
||||||
res.set('Cache-Control', 'private, max-age=86400, no-transform');
|
// set the header to Cache-Control
|
||||||
break;
|
res.set('Cache-Control', cacheControlHeader);
|
||||||
}
|
|
||||||
|
|
||||||
case CacheControl.PRIVATE_WITHOUT_CACHE: {
|
|
||||||
res.set('Cache-Control', 'private, no-cache, no-transform');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.header('Content-Type', file.contentType);
|
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)}`);
|
res.header('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(file.fileName)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// configure options for serving
|
||||||
const options: SendFileOptions = { dotfiles: 'allow' };
|
const options: SendFileOptions = { dotfiles: 'allow' };
|
||||||
if (!isAbsolute(file.path)) {
|
if (!isAbsolute(file.path)) {
|
||||||
options.root = process.cwd();
|
options.root = process.cwd();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user