fix(utils): check response is not empty before fs.writeFile()

- use utf8 encoding for svg
This commit is contained in:
Ming Di Leom
2021-12-23 10:59:17 +00:00
parent 443bca6b43
commit b961f7a3ea
+11 -6
View File
@@ -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 }