diff --git a/src/application.ts b/src/application.ts index 1599e80..2a64822 100644 --- a/src/application.ts +++ b/src/application.ts @@ -29,6 +29,7 @@ import { PackageController } from "@/modules/package/package.controller"; import { PackageMapper } from "@/modules/package/package.mapper"; import { PartnerController } from "@/modules/partner/partner.controller"; import { PartnerMapper } from "@/modules/partner/partner.mapper"; +import { StaticController } from "@/modules/static/static.controller"; import { TransportationController } from "@/modules/transportation/transportation.controller"; import { TransportationMapper } from "@/modules/transportation/transportation.mapper"; import compression from "compression"; @@ -139,6 +140,7 @@ export class Application { this._paymentService, this._jwtService, ).buildRouter(); + const staticRouter = new StaticController(this._fileStorage).buildRouter(); this._app.use("/countries", countryRouter); this._app.use("/cities", cityRouter); @@ -152,6 +154,7 @@ export class Application { this._app.use("/admins", adminRouter); this._app.use("/partners", partnerRouter); this._app.use("/orders", orderRouter); + this._app.use("/statics", staticRouter); } public initializeErrorHandlers() {} diff --git a/src/modules/static/static.controller.ts b/src/modules/static/static.controller.ts new file mode 100644 index 0000000..c3aa00f --- /dev/null +++ b/src/modules/static/static.controller.ts @@ -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); + } + + 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; + } +} diff --git a/src/modules/static/static.schemas.ts b/src/modules/static/static.schemas.ts new file mode 100644 index 0000000..8e99778 --- /dev/null +++ b/src/modules/static/static.schemas.ts @@ -0,0 +1,5 @@ +import z from "zod"; + +export const staticParamsSchema = z.object({ + name: z.string("Must be string.").nonempty("Must not empty."), +}); diff --git a/src/modules/static/static.types.ts b/src/modules/static/static.types.ts new file mode 100644 index 0000000..41b6cca --- /dev/null +++ b/src/modules/static/static.types.ts @@ -0,0 +1,10 @@ +import type { countryParamsSchema } from "@/modules/country/country.schemas"; +import z from "zod"; + +export type StaticParams = z.infer; + +export type StaticMetaResponse = { + name: string; + mime_type: string; + extension: string; +};