perf: 头像增加缓存时间

This commit is contained in:
xiaojunnuo
2026-05-15 00:39:35 +08:00
parent 3b72ca09c6
commit 7015b1b232
6 changed files with 66 additions and 7 deletions
@@ -12,6 +12,18 @@ export function isImageFile(filePath: string) {
return imageExtSet.has(filePath.substring(filePath.lastIndexOf('.')).toLowerCase());
}
export function getImageDownloadOptions(filePath: string) {
if (!isImageFile(filePath)) {
return undefined;
}
return {
maxage: imageCacheSeconds * 1000,
setHeaders(res: any) {
res.setHeader('Cache-Control', `public,max-age=${imageCacheSeconds}`);
},
};
}
/**
*/
@Provide()
@@ -47,11 +59,10 @@ export class FileController extends BaseController {
userId = this.getUserId();
}
const filePath = this.fileService.getFile(key, userId);
if (isImageFile(filePath)) {
this.ctx.response.set('Cache-Control', `public,max-age=${imageCacheSeconds}`);
} else {
const sendOptions = getImageDownloadOptions(filePath);
if (!sendOptions) {
this.ctx.response.attachment(filePath);
}
await send(this.ctx, filePath);
await send(this.ctx, filePath, sendOptions);
}
}