perf: 第三方登录自动注册的用户支持设置初始化密码

This commit is contained in:
xiaojunnuo
2026-05-15 00:25:28 +08:00
parent 229f22d5a9
commit a815d0245b
2 changed files with 32 additions and 2 deletions
@@ -0,0 +1,20 @@
/// <reference types="mocha" />
/// <reference types="node" />
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);
});
});
@@ -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);
}
}