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
@@ -3,7 +3,7 @@
import assert from "node:assert/strict";
import { isImageFile } from "./file-controller.js";
import { getImageDownloadOptions, isImageFile } from "./file-controller.js";
describe("FileController.isImageFile", () => {
it("detects uploaded logo image files", () => {
@@ -17,4 +17,23 @@ describe("FileController.isImageFile", () => {
assert.equal(isImageFile("data/upload/public/user/cert.pem"), false);
assert.equal(isImageFile("data/upload/public/user/logo"), false);
});
it("builds koa-send options that keep image cache headers at 3 days", () => {
const options = getImageDownloadOptions("data/upload/public/user/logo.png");
assert.equal(options?.maxage, 259200000);
const headers: Record<string, string> = {};
options?.setHeaders({
setHeader(key: string, value: string) {
headers[key] = value;
},
});
assert.equal(headers["Cache-Control"], "public,max-age=259200");
});
it("does not build cache options for non-image files", () => {
assert.equal(getImageDownloadOptions("data/upload/private/user/cert.pem"), undefined);
});
});
@@ -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);
}
}