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); } }