add statics api
This commit is contained in:
@@ -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() {}
|
||||
|
||||
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