-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfileSystem.js
More file actions
31 lines (25 loc) · 821 Bytes
/
fileSystem.js
File metadata and controls
31 lines (25 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// from http://stackoverflow.com/questions/8579055/how-i-move-files-on-node-js/29105404#29105404
var fs = require('fs');
module.exports = function move (oldPath, newPath, callback) {
fs.rename(oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
copy();
} else {
callback(err);
}
return;
}
callback(null, oldPath, newPath);
});
function copy () {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', callback);
writeStream.on('error', callback);
readStream.on('close', function () {
fs.unlink(oldPath, callback);
});
readStream.pipe(writeStream);
}
};