From a815d0245b97efbb948b33d6fc9d49862ce06889 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Fri, 15 May 2026 00:25:28 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E7=AC=AC=E4=B8=89=E6=96=B9=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E8=87=AA=E5=8A=A8=E6=B3=A8=E5=86=8C=E7=9A=84=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/basic/file-controller.test.ts | 20 +++++++++++++++++++ .../src/controller/basic/file-controller.ts | 14 +++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 packages/ui/certd-server/src/controller/basic/file-controller.test.ts diff --git a/packages/ui/certd-server/src/controller/basic/file-controller.test.ts b/packages/ui/certd-server/src/controller/basic/file-controller.test.ts new file mode 100644 index 000000000..a09837292 --- /dev/null +++ b/packages/ui/certd-server/src/controller/basic/file-controller.test.ts @@ -0,0 +1,20 @@ +/// +/// + +import assert from "node:assert/strict"; + +import { isImageFile } from "./file-controller.js"; + +describe("FileController.isImageFile", () => { + it("detects uploaded logo image files", () => { + assert.equal(isImageFile("data/upload/public/user/logo.PNG"), true); + assert.equal(isImageFile("data/upload/public/user/logo.svg"), true); + assert.equal(isImageFile("data/upload/public/user/logo.webp"), true); + }); + + it("does not treat non-image downloads as logo images", () => { + assert.equal(isImageFile("data/upload/public/user/archive.zip"), false); + assert.equal(isImageFile("data/upload/public/user/cert.pem"), false); + assert.equal(isImageFile("data/upload/public/user/logo"), false); + }); +}); diff --git a/packages/ui/certd-server/src/controller/basic/file-controller.ts b/packages/ui/certd-server/src/controller/basic/file-controller.ts index d2dca2acd..e0340fd79 100644 --- a/packages/ui/certd-server/src/controller/basic/file-controller.ts +++ b/packages/ui/certd-server/src/controller/basic/file-controller.ts @@ -5,6 +5,13 @@ import { nanoid } from 'nanoid'; import { cache } from '@certd/basic'; import { UploadFileInfo } from '@midwayjs/upload'; +const imageExtSet = new Set(['.apng', '.avif', '.bmp', '.gif', '.ico', '.jpeg', '.jpg', '.png', '.svg', '.webp']); +const imageCacheSeconds = 3 * 24 * 60 * 60; + +export function isImageFile(filePath: string) { + return imageExtSet.has(filePath.substring(filePath.lastIndexOf('.')).toLowerCase()); +} + /** */ @Provide() @@ -40,8 +47,11 @@ export class FileController extends BaseController { userId = this.getUserId(); } const filePath = this.fileService.getFile(key, userId); - this.ctx.response.attachment(filePath); - this.ctx.response.set('Cache-Control', 'public,max-age=2592000'); + if (isImageFile(filePath)) { + this.ctx.response.set('Cache-Control', `public,max-age=${imageCacheSeconds}`); + } else { + this.ctx.response.attachment(filePath); + } await send(this.ctx, filePath); } }