UNPKG

@opengis/fastify-table

Version:

core-plugins

93 lines (92 loc) 3.21 kB
import { config, dataUpdate, logger, pgClients } from "../../../../utils.js"; /** * Апі використовується для видалення файлів за допомогою fs або s3 * * @method GET * @summary Видалення файлів і оновлення логів в БД * @priority 5 * @alias deleteFileAPI * @type api * @tag file * @requires deleteFile * @param {Object} params Параметри URL * @param {String} params[0] Шлях до файла * @errors 403,500 * @returns {Number} status Номер помилки * @returns {String|Object} error Опис помилки * @returns {Object} headers Заголовки HTTP * @returns {String} message Повідомлення про успішне виконання або об'єкт з параметрами */ export default async function deleteFileAPI(req, reply) { const { pg = pgClients.client, params = {}, user = {} } = req; const { uid, user_rnokpp: rnokpp } = user; if (!params["*"]) { return reply.status(404).send({ error: "File not found", code: 404 }); } const filename = params["*"].startsWith("/") || /^[0-9]+$/.test(params["*"]) ? params["*"] : `/${params["*"]}`; if (!filename) { logger.file("file", { level: "INFO", type: "delete", message: 'No required param "filename"', file: params["*"], uid, rnokpp, }); return reply.status(404).send({ error: "File not found", code: 404 }); } if (filename.includes("..")) { logger.file("file", { level: "WARN", type: "delete", message: "injection attempt", file: params["*"], uid, rnokpp, }); return reply.status(404).send({ error: "File not found", code: 404 }); } try { const result = await pg .query("select file_id, file_path as fpath, uid from crm.files where $1 in (file_path, file_id)", [filename]) .then((el) => el.rows?.[0] || {}); const res = await dataUpdate({ pg, table: "crm.files", id: result?.file_id, data: { file_status: 3 }, uid, }); if (!res) { logger.file("file", { level: "INFO", type: "delete", message: "file not found", file: params["*"], uid, rnokpp, }); return reply.status(404).send({ error: "File not found", code: 404 }); } const message = { id: res.file_id, filepath: res.file_path }; logger.file("file/delete", { message, file: params["*"], uid, rnokpp, }); return message; } catch (err) { logger.file("file/delete", { error: err.toString(), file: params["*"], uid, rnokpp, }); const error = config.local ? err.toString() : "Помилка видалення файлу"; return reply.status(500).send({ error, code: 500 }); } }