From 8766b5d487dfc52c8f6c830c836d6ba2f81ae1bf Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 24 May 2018 11:30:29 +0100 Subject: [PATCH] DRY up writing to dump-folder in FileWriter --- .../app/coffee/infrastructure/FileWriter.coffee | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/services/web/app/coffee/infrastructure/FileWriter.coffee b/services/web/app/coffee/infrastructure/FileWriter.coffee index 21353e7a36..27b1f16921 100644 --- a/services/web/app/coffee/infrastructure/FileWriter.coffee +++ b/services/web/app/coffee/infrastructure/FileWriter.coffee @@ -7,13 +7,18 @@ request = require 'request' module.exports = FileWriter = - writeLinesToDisk: (identifier, lines, callback = (error, fsPath)->) -> - callback = _.once(callback) - fsPath = "#{Settings.path.dumpFolder}/#{identifier}_#{uuid.v4()}" + _ensureDumpFolderExists: (callback=(error)->) -> fs.mkdir Settings.path.dumpFolder, (error) -> if error? and error.code != 'EEXIST' # Ignore error about already existing return callback(error) + callback(null) + + writeLinesToDisk: (identifier, lines, callback = (error, fsPath)->) -> + callback = _.once(callback) + fsPath = "#{Settings.path.dumpFolder}/#{identifier}_#{uuid.v4()}" + FileWriter._ensureDumpFolderExists (error) -> + return callback(error) if error? fs.writeFile fsPath, lines.join('\n'), (error) -> return callback(error) if error? callback(null, fsPath) @@ -23,11 +28,9 @@ module.exports = FileWriter = fsPath = "#{Settings.path.dumpFolder}/#{identifier}_#{uuid.v4()}" stream.pause() - fs.mkdir Settings.path.dumpFolder, (error) -> + FileWriter._ensureDumpFolderExists (error) -> + return callback(error) if error? stream.resume() - if error? and error.code != 'EEXIST' - # Ignore error about already existing - return callback(error) writeStream = fs.createWriteStream(fsPath) stream.pipe(writeStream)