mirror of
https://github.com/certd/certd.git
synced 2026-05-16 05:07:32 +08:00
perf: 第三方登录自动注册的用户支持设置初始化密码
This commit is contained in:
@@ -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 { cache } from '@certd/basic';
|
||||||
import { UploadFileInfo } from '@midwayjs/upload';
|
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()
|
@Provide()
|
||||||
@@ -40,8 +47,11 @@ export class FileController extends BaseController {
|
|||||||
userId = this.getUserId();
|
userId = this.getUserId();
|
||||||
}
|
}
|
||||||
const filePath = this.fileService.getFile(key, userId);
|
const filePath = this.fileService.getFile(key, userId);
|
||||||
this.ctx.response.attachment(filePath);
|
if (isImageFile(filePath)) {
|
||||||
this.ctx.response.set('Cache-Control', 'public,max-age=2592000');
|
this.ctx.response.set('Cache-Control', `public,max-age=${imageCacheSeconds}`);
|
||||||
|
} else {
|
||||||
|
this.ctx.response.attachment(filePath);
|
||||||
|
}
|
||||||
await send(this.ctx, filePath);
|
await send(this.ctx, filePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user