From 86df2166b5424a0f8dfaac8df0e8dbfd41fb8db6 Mon Sep 17 00:00:00 2001 From: Etienne Laurent Date: Thu, 11 Apr 2024 17:26:10 +0200 Subject: [PATCH] refactor remove functions --- lib/methods/export.js | 9 --------- lib/methods/import.js | 24 ++---------------------- lib/methods/index.js | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/lib/methods/export.js b/lib/methods/export.js index dc8bdc6d..d4e683b9 100644 --- a/lib/methods/export.js +++ b/lib/methods/export.js @@ -367,15 +367,6 @@ module.exports = self => { } }, - async remove(filepath) { - console.info(`[export] removing ${filepath}`); - try { - await fsp.unlink(filepath); - } catch (error) { - self.apos.util.error(error); - } - }, - // Report is available for 10 minutes by default removeFromUploadFs(downloadPath, expiration) { const ms = expiration || 1000 * 60 * 10; diff --git a/lib/methods/import.js b/lib/methods/import.js index ec987956..b7b8f458 100644 --- a/lib/methods/import.js +++ b/lib/methods/import.js @@ -132,7 +132,7 @@ module.exports = self => { .dismiss(req, notificationId, 2000) .catch(self.apos.util.error); - await self.removeExport(exportPath); + await self.remove(exportPath); return; } @@ -531,27 +531,7 @@ module.exports = self => { self.apos.notification.dismiss(req, notificationId, 2000).catch(self.apos.util.error); } - await self.removeExport(exportPath); - }, - - async removeExport(filepath) { - try { - const stat = await fsp.lstat(filepath); - if (stat.isDirectory()) { - await fsp.rm(filepath, { - recursive: true, - force: true - }); - } else { - await fsp.unlink(filepath); - } - console.info(`[import] export path ${filepath} has been removed`); - } catch (err) { - console.trace(); - self.apos.util.error( - `Error while trying to remove the file or folder: ${filepath}. You might want to remove it yourself.` - ); - } + await self.remove(exportPath); } }; }; diff --git a/lib/methods/index.js b/lib/methods/index.js index f8d2e159..5c7840f4 100644 --- a/lib/methods/index.js +++ b/lib/methods/index.js @@ -1,3 +1,4 @@ +const fsp = require('node:fs/promises'); const importMethods = require('./import'); const exportMethods = require('./export'); @@ -34,6 +35,27 @@ module.exports = self => { return true; }, + + async remove(filepath) { + try { + const stat = await fsp.lstat(filepath); + if (stat.isDirectory()) { + await fsp.rm(filepath, { + recursive: true, + force: true + }); + } else { + await fsp.unlink(filepath); + } + console.info(`removed: ${filepath}`); + } catch (err) { + console.trace(); + self.apos.util.error( + `Error while trying to remove the file or folder: ${filepath}. You might want to remove it yourself.` + ); + } + }, + ...importMethods(self), ...exportMethods(self) };