Skip to content

TagSpaces is an offline, open source, document manager with tagging support

updating error handling and file copy function

uggrock0725af4e4644
diff --git a/data/electron/electron.api.js b/data/electron/electron.api.js
index 755a67e91..bb16a387e 100644
--- a/data/electron/electron.api.js
+++ b/data/electron/electron.api.js
@@ -628,6 +628,7 @@ define(function(require, exports, module) {
fs.lstat(path, function(err, stats) {
if (err) {
resolve(false);
+ return;
}
if (stats) {
@@ -656,6 +657,7 @@ define(function(require, exports, module) {
fs.mkdir(dirPath, function(error) {
if (error) {
reject("Error creating folder: " + dirPath);
+ return;
}
resolve();
});
@@ -672,49 +674,26 @@ define(function(require, exports, module) {
* @returns {Promise.<Success, Error>}
*/
function copyFilePromise(sourceFilePath, targetFilePath) {
- /*return new Promise(function(resolve, reject) {
- getPropertiesPromise(sourceFilePath).then(function(entry) {
- if (!entry.isFile) {
- reject($.i18n.t("ns.common:fileIsDirectory", {fileName: sourceFilePath}));
- } else {
- getPropertiesPromise(targetFilePath).then(function(entry2) {
- if (entry2) {
- reject($.i18n.t("ns.common:fileExists", {fileName: targetFilePath}));
- } else {
- var rd = fs.createReadStream(sourceFilePath);
- rd.on("error", function(err) {
- reject($.i18n.t("ns.common:fileCopyFailed", {fileName: sourceFilePath}));
- });
- var wr = fs.createWriteStream(targetFilePath);
- wr.on("error", function(err) {
- reject($.i18n.t("ns.common:fileCopyFailed", {fileName: sourceFilePath}));
- });
- wr.on("close", function(ex) {
- resolve();
- });
- wr.on('finish', resolve([sourceFilePath, targetFilePath]));
- rd.pipe(wr);
- }
- }, function(err) {
- reject(err);
- });
- }
- }, function(err) {
- reject(err);
- });
- });*/
+ console.log("Copying file: " + sourceFilePath + " to " + targetFilePath);
return new Promise(function(resolve, reject) {
- fs.readFile(sourceFilePath, 'utf8', function (err, data) {
- if (err){
- reject($.i18n.t("ns.common:fileCopyFailed", {fileName: sourceFilePath}));
- throw err;
- }
- fs.writeFile (targetFilePath, data, function(err) {
- if (err) throw err;
- console.log('complete');
- resolve([sourceFilePath, targetFilePath])
+ if (sourceFilePath === targetFilePath) {
+ reject($.i18n.t("ns.common:fileTheSame"), "File copying failed");
+ return;
+ } else if (fs.lstatSync(sourceFilePath).isDirectory()) {
+ reject($.i18n.t("ns.common:fileIsDirectory", {fileName: sourceFilePath}));
+ return;
+ } else if (fs.existsSync(targetFilePath)) {
+ reject($.i18n.t("ns.common:fileExists", {fileName: targetFilePath}), "File copying failed");
+ return;
+ } else {
+ fs.copy(sourceFilePath, targetFilePath, function(error) {
+ if (error) {
+ reject("Copying: " + sourceFilePath + " failed.");
+ return;
+ }
+ resolve([sourceFilePath, targetFilePath]);
});
- });
+ }
});
}
@@ -732,14 +711,18 @@ define(function(require, exports, module) {
return new Promise(function(resolve, reject) {
if (filePath === newFilePath) {
reject($.i18n.t("ns.common:fileTheSame"), $.i18n.t("ns.common:fileNotMoved"));
+ return;
} else if (fs.lstatSync(filePath).isDirectory()) {
reject($.i18n.t("ns.common:fileIsDirectory", {fileName: filePath}));
+ return;
} else if (fs.existsSync(newFilePath)) {
reject($.i18n.t("ns.common:fileExists", {fileName: newFilePath}), $.i18n.t("ns.common:fileRenameFailed"));
+ return;
} else {
fs.move(filePath, newFilePath, {clobber: true}, function(error) {
if (error) {
- reject("Renaming: " + filePath + " failed.");
+ reject("Renaming: " + filePath + " failed.", error);
+ return;
}
resolve([filePath, newFilePath]);
});
@@ -771,6 +754,7 @@ define(function(require, exports, module) {
if (error) {
console.error("Renaming directory failed " + error);
reject("Renaming " + dirPath + " failed.");
+ return;
}
resolve(newDirPath);
});
@@ -867,6 +851,7 @@ define(function(require, exports, module) {
fs.writeFile(filePath, content, 'utf8', function(error) {
if (error) {
reject(error);
+ return;
}
resolve(isNewFile);
});
@@ -948,6 +933,7 @@ define(function(require, exports, module) {
fs.unlink(path, function(error) {
if (error) {
reject(error);
+ return;
} else {
resolve(path);
}
@@ -972,6 +958,7 @@ define(function(require, exports, module) {
fs.rmdir(path, function(error) {
if (error) {
reject(error);
+ return;
} else {
resolve(path);
}