This commit is contained in:
2026-06-17 10:47:21 +07:00
parent f8cdef0663
commit 8e49e1c85c
3 changed files with 76 additions and 0 deletions

View File

@ -95,6 +95,59 @@ export class FileController {
}
}
static async delete(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
/*
#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<void | Response> {
/*
#swagger.tags = ['File']

View File

@ -105,4 +105,26 @@ export default class Thirdparty {
};
}
}
static async DeleteFile(url: string, field: any, token?: string) {
try {
const headers: Record<string, string> = {};
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,
};
}
}
}

View File

@ -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);