Refactor the backend

This commit is contained in:
jiangrui
2025-03-11 17:42:59 +08:00
parent 615149c83f
commit 37c25a9307
23 changed files with 347 additions and 323 deletions

View File

@@ -0,0 +1,32 @@
import { Request, Response } from "express";
import { BaseController } from "./BaseController";
import { ICloudStorageService } from "@/types/services";
export abstract class BaseCloudController extends BaseController {
constructor(protected cloudService: ICloudStorageService) {
super();
}
async getShareInfo(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { shareCode, receiveCode } = req.query;
await this.cloudService.setCookie(req);
return await this.cloudService.getShareInfo(shareCode as string, receiveCode as string);
});
}
async getFolderList(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { parentCid } = req.query;
await this.cloudService.setCookie(req);
return await this.cloudService.getFolderList(parentCid as string);
});
}
async saveFile(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
await this.cloudService.setCookie(req);
return await this.cloudService.saveSharedFile(req.body);
});
}
}

View File

@@ -9,11 +9,13 @@ export abstract class BaseController {
protected async handleRequest<T>(
req: Request,
res: Response,
action: () => Promise<ApiResponseData<T>>
action: () => Promise<ApiResponseData<T> | void>
): Promise<void> {
try {
const result = await action();
res.json(ApiResponse.success(result.data, result.message));
if (result) {
res.json(ApiResponse.success(result.data, result.message));
}
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : "未知错误";
res.status(200).json(ApiResponse.error(errorMessage));

View File

@@ -1,41 +1,11 @@
import { Request, Response } from "express";
import { Cloud115Service } from "../services/Cloud115Service";
import { BaseController } from "./BaseController";
import { injectable, inject } from "inversify";
import { TYPES } from "../core/types";
import { BaseCloudController } from "./BaseCloudController";
@injectable()
export class Cloud115Controller extends BaseController {
constructor(@inject(TYPES.Cloud115Service) private cloud115Service: Cloud115Service) {
super();
}
async getShareInfo(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { shareCode, receiveCode } = req.query;
await this.cloud115Service.setCookie(req);
return await this.cloud115Service.getShareInfo(shareCode as string, receiveCode as string);
});
}
async getFolderList(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { parentCid } = req.query;
await this.cloud115Service.setCookie(req);
return await this.cloud115Service.getFolderList(parentCid as string);
});
}
async saveFile(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { shareCode, receiveCode, fileId, folderId } = req.body;
await this.cloud115Service.setCookie(req);
return await this.cloud115Service.saveSharedFile({
shareCode,
receiveCode,
fileId,
cid: folderId,
});
});
export class Cloud115Controller extends BaseCloudController {
constructor(@inject(TYPES.Cloud115Service) cloud115Service: Cloud115Service) {
super(cloud115Service);
}
}

View File

@@ -2,34 +2,11 @@ import { Request, Response } from "express";
import { injectable, inject } from "inversify";
import { TYPES } from "../core/types";
import { QuarkService } from "../services/QuarkService";
import { BaseController } from "./BaseController";
import { BaseCloudController } from "./BaseCloudController";
@injectable()
export class QuarkController extends BaseController {
constructor(@inject(TYPES.QuarkService) private quarkService: QuarkService) {
super();
}
async getShareInfo(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { shareCode, receiveCode } = req.query;
await this.quarkService.setCookie(req);
return await this.quarkService.getShareInfo(shareCode as string, receiveCode as string);
});
}
async getFolderList(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const { parentCid } = req.query;
await this.quarkService.setCookie(req);
return await this.quarkService.getFolderList(parentCid as string);
});
}
async saveFile(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
await this.quarkService.setCookie(req);
return await this.quarkService.saveSharedFile(req.body);
});
export class QuarkController extends BaseCloudController {
constructor(@inject(TYPES.QuarkService) quarkService: QuarkService) {
super(quarkService);
}
}

View File

@@ -12,8 +12,19 @@ export class ImageController extends BaseController {
async getImages(req: Request, res: Response): Promise<void> {
await this.handleRequest(req, res, async () => {
const url = req.query.url as string;
return await this.imageService.getImages(url);
const url = decodeURIComponent((req.query.url as string) || "");
const response = await this.imageService.getImages(url);
// 设置正确的响应头
res.setHeader("Content-Type", response.headers["content-type"]);
res.setHeader("Cache-Control", "no-cache");
// 确保清除任何可能导致304响应的头信息
res.removeHeader("etag");
res.removeHeader("last-modified");
// 直接传输图片数据
response.data.pipe(res);
});
}
}