From b961f7a3ea27db4e099efe2c14bed640d5df10e7 Mon Sep 17 00:00:00 2001 From: Ming Di Leom Date: Thu, 23 Dec 2021 10:59:17 +0000 Subject: [PATCH] fix(utils): check response is not empty before fs.writeFile() - use utf8 encoding for svg --- src/utils.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/utils.js b/src/utils.js index e611fa7..23336bd 100644 --- a/src/utils.js +++ b/src/utils.js @@ -283,13 +283,18 @@ module.exports = function(redis) { return { success: false, reason: 'SERVER_ERROR' } } - try { - await fs.writeFile(path_with_filename, data, 'binary') - return { success: true, path: path_with_filename } - } catch(err) { - console.log('Writing media file to disk failed for unknown reason. Details:', err) - return { success: false, reason: 'WRITEFILE_FAILED' } + const encoding = url.includes('render/svg/') ? 'utf8' : 'binary' + + if(body) { + try { + await fs.writeFile(path_with_filename, body, encoding) + return { success: true, path: path_with_filename } + } catch(err) { + console.log('Writing media file to disk failed for unknown reason. Details:', err) + return { success: false, reason: 'WRITEFILE_FAILED' } + } } + return { success: false, reason: 'EMPTY_BODY' } } return { success: true, path: path_with_filename }