diff --git a/src/controllers/file/file.ts b/src/controllers/file/file.ts index 0d7a196..ea07b31 100644 --- a/src/controllers/file/file.ts +++ b/src/controllers/file/file.ts @@ -95,6 +95,59 @@ export class FileController { } } + static async delete(req: Request, res: Response, next: NextFunction): Promise { + /* + #swagger.tags = ['File'] + #swagger.security = [{ + "bearerAuth": [] + }] + #swagger.requestBody = { + required: true, + content: { + "application/json": { + schema: { + type: "object", + properties: { + file: { + type: "string", + description: "File name returned from upload" + } + }, + required: ["file"] + } + } + } + } + } + */ + try { + const schema = Joi.object({ + file: Joi.string().required().label("File"), + }); + + const param = await schema.validateAsync(req.body); + + if (param.file.includes("..") || param.file.includes("/") || param.file.includes("\\")) { + return ReturnHelper.errorResponse(res, 400, 400, Language.lang.failed_delete, "Invalid file name"); + } + + const field = { + file: param.file, + application: "saude", + }; + + let url = config.get("service.file") + "delete"; + await Thirdparty.DeleteFile(url, field, JwtHelper.token(req.auth.data)); + + return ReturnHelper.successResponseAny(res, 200, Language.lang.success_delete, {}); + } catch (e: unknown) { + log.error(e); + const err = e as Error; + + return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_delete, err.message); + } + } + static async download(req: Request, res: Response, next: NextFunction): Promise { /* #swagger.tags = ['File'] diff --git a/src/helpers/thirdparty.ts b/src/helpers/thirdparty.ts index 7ad4731..7b725cb 100644 --- a/src/helpers/thirdparty.ts +++ b/src/helpers/thirdparty.ts @@ -105,4 +105,26 @@ export default class Thirdparty { }; } } + + static async DeleteFile(url: string, field: any, token?: string) { + try { + const headers: Record = {}; + if (token) { + headers.Authorization = `Bearer ${token}`; + } + + const response = await axios.delete(url, { data: field, headers }); + const result = response.data; + + if (!result.status) throw response; + return result; + } catch (error: any) { + const { response } = error; + throw { + status: false, + message: response?.data?.error ?? response?.data?.message ?? "Unknown error", + data: null, + }; + } + } } diff --git a/src/routes/private.ts b/src/routes/private.ts index 75da764..961145b 100644 --- a/src/routes/private.ts +++ b/src/routes/private.ts @@ -103,6 +103,7 @@ export class RoutePrivate { app.put("/api/menu/restore/:id", MenuController.restore); app.post("/api/upload", FileController.upload); + app.delete("/api/delete", FileController.delete); app.get("/api/download", FileController.download); app.get("/api/administrativu/list", AdministrativuController.list);