mirror of
https://github.com/certd/certd.git
synced 2026-06-27 22:17:35 +08:00
chore: format
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import { Controller, Fields, Files, Get, Inject, Post, Provide, Query } from '@midwayjs/core';
|
||||
import { BaseController, Constants, FileService, PermissionException, UploadFileItem, uploadTmpFileCacheKey } from '@certd/lib-server';
|
||||
import send from 'koa-send';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { cache } from '@certd/basic';
|
||||
import { UploadFileInfo } from '@midwayjs/upload';
|
||||
import { AuthService } from '../../modules/sys/authority/service/auth-service.js';
|
||||
import { Controller, Fields, Files, Get, Inject, Post, Provide, Query } from "@midwayjs/core";
|
||||
import { BaseController, Constants, FileService, PermissionException, UploadFileItem, uploadTmpFileCacheKey } from "@certd/lib-server";
|
||||
import send from "koa-send";
|
||||
import { nanoid } from "nanoid";
|
||||
import { cache } from "@certd/basic";
|
||||
import { UploadFileInfo } from "@midwayjs/upload";
|
||||
import { AuthService } from "../../modules/sys/authority/service/auth-service.js";
|
||||
|
||||
const imageExtSet = new Set(['.apng', '.avif', '.bmp', '.gif', '.ico', '.jpeg', '.jpg', '.png', '.svg', '.webp']);
|
||||
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());
|
||||
return imageExtSet.has(filePath.substring(filePath.lastIndexOf(".")).toLowerCase());
|
||||
}
|
||||
|
||||
export function getImageDownloadOptions(filePath: string) {
|
||||
@@ -20,7 +20,7 @@ export function getImageDownloadOptions(filePath: string) {
|
||||
return {
|
||||
maxage: imageCacheSeconds * 1000,
|
||||
setHeaders(res: any) {
|
||||
res.setHeader('Cache-Control', `public,max-age=${imageCacheSeconds}`);
|
||||
res.setHeader("Cache-Control", `public,max-age=${imageCacheSeconds}`);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -28,7 +28,7 @@ export function getImageDownloadOptions(filePath: string) {
|
||||
/**
|
||||
*/
|
||||
@Provide()
|
||||
@Controller('/api/basic/file')
|
||||
@Controller("/api/basic/file")
|
||||
export class FileController extends BaseController {
|
||||
@Inject()
|
||||
fileService: FileService;
|
||||
@@ -36,9 +36,9 @@ export class FileController extends BaseController {
|
||||
@Inject()
|
||||
authService: AuthService;
|
||||
|
||||
@Post('/upload', { description: Constants.per.authOnly })
|
||||
async upload(@Files() files: UploadFileInfo<string>[], @Fields() fields: any, @Query('autoSave') autoSave: string) {
|
||||
console.log('files', files, fields);
|
||||
@Post("/upload", { description: Constants.per.authOnly })
|
||||
async upload(@Files() files: UploadFileInfo<string>[], @Fields() fields: any, @Query("autoSave") autoSave: string) {
|
||||
console.log("files", files, fields);
|
||||
const cacheKey = uploadTmpFileCacheKey + nanoid();
|
||||
const file = files[0];
|
||||
cache.set(
|
||||
@@ -51,8 +51,8 @@ export class FileController extends BaseController {
|
||||
ttl: 1000 * 60 * 60,
|
||||
}
|
||||
);
|
||||
if (autoSave === 'true') {
|
||||
const key = await this.fileService.saveFile(this.getUserId(), cacheKey, 'public');
|
||||
if (autoSave === "true") {
|
||||
const key = await this.fileService.saveFile(this.getUserId(), cacheKey, "public");
|
||||
return this.ok({
|
||||
key,
|
||||
url: `/api/basic/file/download?key=${encodeURIComponent(key)}`,
|
||||
@@ -63,8 +63,8 @@ export class FileController extends BaseController {
|
||||
});
|
||||
}
|
||||
|
||||
@Get('/download', { description: Constants.per.guestOptionalAuth })
|
||||
async download(@Query('key') key: string) {
|
||||
@Get("/download", { description: Constants.per.guestOptionalAuth })
|
||||
async download(@Query("key") key: string) {
|
||||
const filePath = this.getDownloadFilePath(key);
|
||||
const sendOptions = getImageDownloadOptions(filePath);
|
||||
if (!sendOptions) {
|
||||
@@ -74,7 +74,7 @@ export class FileController extends BaseController {
|
||||
}
|
||||
|
||||
private getDownloadFilePath(key: string) {
|
||||
const isPrivateFile = !key.startsWith('/public');
|
||||
const isPrivateFile = !key.startsWith("/public");
|
||||
const userId = isPrivateFile ? this.getUserId() : null;
|
||||
try {
|
||||
return this.fileService.getFile(key, userId);
|
||||
|
||||
Reference in New Issue
Block a user