Skip to content
Browse files

Copy the output file when EXDEV is encountered

  • Loading branch information...
1 parent d563608 commit 061fa5da5cfa9901032630b92d6849a808dfbff5 @chriso chriso committed Mar 27, 2013
Showing with 26 additions and 0 deletions.
  1. +26 −0 index.js
View
26 index.js
@@ -44,6 +44,12 @@ jpegmini.optimise = function (path, options, callback) {
}
fs.rename(options.output, path, function (err) {
if (err) {
+ if (err.code === 'EXDEV') {
+ return copyFile(options.output, path, function (err) {
+ fs.unlink(options.output, function () {});
+ callback(err);
+ });
+ }
return callback(err);
}
callback(null, true);
@@ -215,3 +221,23 @@ jpegmini.exec = function () {
jpegmini_exec.apply(this, args);
};
+/**
+ * Copy a file.
+ */
+
+function copyFile(source, target, callback) {
+ var reader = fs.createReadStream(source)
+ , writer = fs.createWriteStream(target)
+ , done = false;
+ reader.on('error', oncomplete);
+ writer.on('error', oncomplete);
+ writer.on('close', oncomplete);
+ reader.pipe(writer);
+ function oncomplete(err) {
+ if (!done) {
+ callback(err);
+ done = true;
+ }
+ }
+}
+

0 comments on commit 061fa5d

Please sign in to comment.
Something went wrong with that request. Please try again.