add statics api
This commit is contained in:
56
src/modules/static/static.controller.ts
Normal file
56
src/modules/static/static.controller.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { Controller } from "@/common/controller";
|
||||
import { createOrmContextMiddleware } from "@/common/middlewares/create-orm-context.middleware";
|
||||
import type { AbstractFileStorage } from "@/common/services/file-storage/abstract.file-storage";
|
||||
import type { SingleResponse } from "@/common/types";
|
||||
import { staticParamsSchema } from "@/modules/static/static.schemas";
|
||||
import type { StaticMetaResponse } from "@/modules/static/static.types";
|
||||
import { Router, type Request, type Response } from "express";
|
||||
|
||||
export class StaticController extends Controller {
|
||||
public constructor(private readonly fileStorage: AbstractFileStorage) {
|
||||
super();
|
||||
}
|
||||
|
||||
async download(req: Request, res: Response) {
|
||||
const parseParamsResult = staticParamsSchema.safeParse(req.params);
|
||||
if (!parseParamsResult.success) {
|
||||
return this.handleZodError(parseParamsResult.error, res, "params");
|
||||
}
|
||||
const params = parseParamsResult.data;
|
||||
|
||||
const file = await this.fileStorage.retrieveFile(params.name);
|
||||
|
||||
return res.status(200).send(file.buffer);
|
||||
}
|
||||
|
||||
async viewMeta(req: Request, res: Response) {
|
||||
const parseParamsResult = staticParamsSchema.safeParse(req.params);
|
||||
if (!parseParamsResult.success) {
|
||||
return this.handleZodError(parseParamsResult.error, res, "params");
|
||||
}
|
||||
const params = parseParamsResult.data;
|
||||
|
||||
const file = await this.fileStorage.retrieveFile(params.name);
|
||||
|
||||
return res.status(200).send({
|
||||
data: {
|
||||
name: file.name,
|
||||
mime_type: file.mimeType,
|
||||
extension: file.extension,
|
||||
},
|
||||
errors: null,
|
||||
} satisfies SingleResponse<StaticMetaResponse>);
|
||||
}
|
||||
|
||||
public buildRouter(): Router {
|
||||
const router = Router();
|
||||
router.get("/:name", createOrmContextMiddleware, this.download.bind(this));
|
||||
router.get(
|
||||
"/:name/meta",
|
||||
createOrmContextMiddleware,
|
||||
this.download.bind(this),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
}
|
||||
5
src/modules/static/static.schemas.ts
Normal file
5
src/modules/static/static.schemas.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import z from "zod";
|
||||
|
||||
export const staticParamsSchema = z.object({
|
||||
name: z.string("Must be string.").nonempty("Must not empty."),
|
||||
});
|
||||
10
src/modules/static/static.types.ts
Normal file
10
src/modules/static/static.types.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { countryParamsSchema } from "@/modules/country/country.schemas";
|
||||
import z from "zod";
|
||||
|
||||
export type StaticParams = z.infer<typeof countryParamsSchema>;
|
||||
|
||||
export type StaticMetaResponse = {
|
||||
name: string;
|
||||
mime_type: string;
|
||||
extension: string;
|
||||
};
|
||||
Reference in New Issue
Block a user