From c7b1abebe4eefa7cb21f7d9699126294ec606d92 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 7 Feb 2021 18:35:00 -0500 Subject: [PATCH] ImportHandler: Avoid deprecated `fs.exists()` function --- src/node/handler/ImportHandler.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/node/handler/ImportHandler.js b/src/node/handler/ImportHandler.js index 82a611398..f5956edcb 100644 --- a/src/node/handler/ImportHandler.js +++ b/src/node/handler/ImportHandler.js @@ -34,11 +34,18 @@ const log4js = require('log4js'); const hooks = require('../../static/js/pluginfw/hooks.js'); const util = require('util'); -const fsp_exists = util.promisify(fs.exists); const fsp_rename = util.promisify(fs.rename); const fsp_readFile = util.promisify(fs.readFile); const fsp_unlink = util.promisify(fs.unlink); +const rm = async (path) => { + try { + await fsp_unlink(path); + } catch (err) { + if (err.code !== 'ENOENT') throw err; + } +}; + let convertor = null; let exportExtension = 'htm'; @@ -239,19 +246,8 @@ const doImport = async (req, res, padId) => { await padMessageHandler.updatePadClients(pad); // clean up temporary files - - /* - * TODO: directly delete the file and handle the eventual error. Checking - * before for existence is prone to race conditions, and does not handle any - * errors anyway. - */ - if (await fsp_exists(srcFile)) { - fsp_unlink(srcFile); - } - - if (await fsp_exists(destFile)) { - fsp_unlink(destFile); - } + rm(srcFile); + rm(destFile); }; exports.doImport = (req, res, padId) => {