-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for mv: works accross filesystems
Now mv use copy + unlink instead of rename for moving temporary files.
- Loading branch information
1 parent
02eea18
commit a4f13ff
Showing
3 changed files
with
107 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const streamifier = require('streamifier'); | ||
|
||
|
||
/** | ||
* Returns true if argument is function. | ||
*/ | ||
const isFunc = func => func && func.constructor && func.call && func.apply ? true: false; | ||
|
||
/** | ||
* Creates a folder for file specified in the path variable | ||
* @param {Object} fileUploadOptions | ||
* @param {String} filePath | ||
*/ | ||
const checkAndMakeDir = function(fileUploadOptions, filePath){ | ||
//Check upload options were set. | ||
if (!fileUploadOptions) return false; | ||
if (!fileUploadOptions.createParentPath) return false; | ||
//Check whether folder for the file exists. | ||
if (!filePath) return false; | ||
const parentPath = path.dirname(filePath); | ||
//Create folder if it is not exists. | ||
if(!fs.existsSync(parentPath)) fs.mkdirSync(parentPath); | ||
return true; | ||
}; | ||
|
||
/** | ||
* Copy file via streams | ||
* @param {String} src - Path to the source file | ||
* @param {String} dst - Path to the destination file. | ||
*/ | ||
const copyFile = function(src, dst, callback){ | ||
//cbCalled flag and runCb helps to run cb only once. | ||
let cbCalled = false; | ||
let runCb = (err) => { | ||
if(cbCalled) return; | ||
cbCalled = true; | ||
callback(err); | ||
}; | ||
//Create read stream | ||
let readable = fs.createReadStream(src); | ||
readable.on('error', runCb); | ||
//Create write stream | ||
let writable = fs.createWriteStream(dst); | ||
writable.on('error', (err)=>{ | ||
readable.destroy(); | ||
runCb(err); | ||
}); | ||
writable.on('close', () => runCb()); | ||
//Copieng file via piping streams. | ||
readable.pipe(writable); | ||
}; | ||
|
||
/** | ||
* Save buffer data to a file. | ||
* @param {Buffer} buffer - buffer to save to a file. | ||
* @param {String} filePath - path to a file. | ||
*/ | ||
const saveBufferToFile = function(buffer, filePath, callback){ | ||
if(!Buffer.isBuffer(buffer)){ | ||
callback(new Error('buffer variable should be a Buffer!')); | ||
return; | ||
} | ||
const fstream = fs.createWriteStream(filePath); | ||
streamifier.createReadStream(buffer).pipe(fstream); | ||
fstream.on('error', error => callback(error)); | ||
fstream.on('close', () => callback()); | ||
}; | ||
|
||
module.exports = { | ||
isFunc, | ||
checkAndMakeDir, | ||
copyFile, | ||
saveBufferToFile | ||
}; |