Skip to content

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

fixing issue copy sidecars

kristiyandda31997ff36b2
diff --git a/data/electron/electron.api.js b/data/electron/electron.api.js
index d25f5ebe6..888603e0d 100644
--- a/data/electron/electron.api.js
+++ b/data/electron/electron.api.js
@@ -677,7 +677,7 @@ define(function(require, exports, module) {
* @returns {Promise.<Success, Error>}
*/
function copyFilePromise(sourceFilePath, targetFilePath) {
- return new Promise(function(resolve, reject) {
+ /*return new Promise(function(resolve, reject) {
getPropertiesPromise(sourceFilePath).then(function(entry) {
if (!entry.isFile) {
reject($.i18n.t("ns.common:fileIsDirectory", {fileName: sourceFilePath}));
@@ -686,7 +686,6 @@ define(function(require, exports, module) {
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}));
@@ -698,8 +697,8 @@ define(function(require, exports, module) {
wr.on("close", function(ex) {
resolve();
});
+ wr.on('finish', resolve([sourceFilePath, targetFilePath]));
rd.pipe(wr);
-
}
}, function(err) {
reject(err);
@@ -708,6 +707,19 @@ define(function(require, exports, module) {
}, function(err) {
reject(err);
});
+ });*/
+ 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])
+ });
+ });
});
}
diff --git a/data/js/core.ui.js b/data/js/core.ui.js
index 6b6d009d5..474d48bfd 100644
--- a/data/js/core.ui.js
+++ b/data/js/core.ui.js
@@ -2,7 +2,7 @@
* Use of this source code is governed by a AGPL3 license that
* can be found in the LICENSE file. */
-/* global define, Handlebars, isNode, isFirefox */
+/* global define, Handlebars, isNode, isFirefox, Mousetrap */
define(function(require, exports) {
'use strict';
@@ -64,6 +64,29 @@ define(function(require, exports) {
}
}
});
+
+ //Mousetrap.unbind(TSCORE.Config.getPrevDocumentKeyBinding());
+ Mousetrap.bind(TSCORE.Config.getPrevDocumentKeyBinding(), function() {
+ if (TSCORE.selectedFiles[0]) {
+ TSCORE.PerspectiveManager.selectFile(TSCORE.PerspectiveManager.getPrevFile(TSCORE.selectedFiles[0]));
+ }
+ if (TSCORE.FileOpener.getOpenedFilePath() !== undefined) {
+ TSCORE.FileOpener.openFile(TSCORE.PerspectiveManager.getPrevFile(TSCORE.FileOpener.getOpenedFilePath()));
+ TSCORE.PerspectiveManager.selectFile(TSCORE.FileOpener.getOpenedFilePath());
+ }
+ return false;
+ });
+ //Mousetrap.unbind(TSCORE.Config.getNextDocumentKeyBinding());
+ Mousetrap.bind(TSCORE.Config.getNextDocumentKeyBinding(), function() {
+ if (TSCORE.selectedFiles[0]) {
+ TSCORE.PerspectiveManager.selectFile(TSCORE.PerspectiveManager.getNextFile(TSCORE.selectedFiles[0]));
+ }
+ if (TSCORE.FileOpener.getOpenedFilePath() !== undefined) {
+ TSCORE.FileOpener.openFile(TSCORE.PerspectiveManager.getNextFile(TSCORE.FileOpener.getOpenedFilePath()));
+ TSCORE.PerspectiveManager.selectFile(TSCORE.FileOpener.getOpenedFilePath());
+ }
+ return false;
+ });
}
$('#addFileInput').on('change', function(selection) {
@@ -843,28 +866,22 @@ define(function(require, exports) {
for (var i = 0; i < TSCORE.selectedFiles.length; i++) {
newFilePath = $('#moveCopyDirectoryPath').val() + TSCORE.dirSeparator + TSCORE.TagUtils.extractFileName(TSCORE.selectedFiles[i]);
filePath = TSCORE.selectedFiles[i];
- TSCORE.IO.renameFilePromise(filePath, newFilePath).then(function(success) {
- TSCORE.hideWaitingDialog();
- TSPOSTIO.renameFile(filePath, newFilePath);
- }, function(err) {
- TSCORE.hideWaitingDialog();
- TSCORE.showAlertDialog(err);
- });
- //fileOperations.push(TSCORE.IO.renameFilePromise(filePath, newFilePath));
+ fileOperations.push(TSCORE.IO.renameFilePromise(filePath, newFilePath));
}
-
if (TSCORE.IO.stopWatchingDirectories) {
TSCORE.IO.stopWatchingDirectories();
}
- //Promise.all(fileOperations).then(function(success) {
- // // TODO handle moving sidecar files
- // TSCORE.hideWaitingDialog();
- // TSCORE.navigateToDirectory(TSCORE.currentPath);
- // TSCORE.showSuccessDialog("Files successfully moved");
- //}, function(err) {
- // TSCORE.hideWaitingDialog();
- // TSCORE.showAlertDialog("Renaming files failed");
- //});
+ Promise.all(fileOperations).then(function(success) {
+ success.forEach(function(operation) {
+ TSCORE.Meta.updateMetaData(operation[0], operation[1]);
+ });
+ TSCORE.hideWaitingDialog();
+ TSCORE.navigateToDirectory(TSCORE.currentPath);
+ TSCORE.showSuccessDialog("Files successfully moved");
+ }, function(err) {
+ TSCORE.hideWaitingDialog();
+ TSCORE.showAlertDialog("Renaming files failed");
+ });
});
$('#copyFilesButton').click(function(e) {
@@ -882,6 +899,9 @@ define(function(require, exports) {
}
Promise.all(fileOperations).then(function(success) {
// TODO handle copying sidecar files
+ success.forEach(function(operation) {
+ TSCORE.Meta.copyMetaData(operation[0], operation[1]);
+ });
TSCORE.hideWaitingDialog();
TSCORE.navigateToDirectory(TSCORE.currentPath);
TSCORE.showSuccessDialog("Files successfully copied");
diff --git a/data/js/meta.js b/data/js/meta.js
index 2e457cfad..06604256a 100644
--- a/data/js/meta.js
+++ b/data/js/meta.js
@@ -67,12 +67,12 @@ define(function(require, exports, module) {
TSCORE.IO.saveTextFilePromise(metaFilePath, content, true);
}
- function updateTsMetaData(oldFileName, newFileName) {
+ function updateTsMetaData(oldFileName, newFileName) {
var name = TSCORE.Utils.baseName(oldFileName);
TSCORE.metaFileList.forEach(function(element, index) {
if (element.name.indexOf(name) >= 0) {
if (newFileName) {
- var pathOld = TSCORE.Utils.dirName(oldFileName);
+ var pathOld = TSCORE.Utils.dirName(oldFileName);
var pathNew = TSCORE.Utils.dirName(newFileName);
var path = TSCORE.currentPath;
@@ -89,11 +89,47 @@ define(function(require, exports, module) {
if (pathOld == TSCORE.currentPath) {
element.name = newName;
- element.path = newFilePath;
+ element.path = newFilePath;
} else {
TSCORE.metaFileList.splice(index, 1);
}
-
+
+ } else {
+ TSCORE.IO.deleteFilePromise(element.path).then(function() {
+ TSCORE.metaFileList.splice(index, 1);
+ });
+ }
+ }
+ });
+ }
+
+ function copyTsMetaData(oldFileName, newFileName) {
+ var name = TSCORE.Utils.baseName(oldFileName);
+ TSCORE.metaFileList.forEach(function(element, index) {
+ if (element.name.indexOf(name) >= 0) {
+ if (newFileName) {
+ var pathOld = TSCORE.Utils.dirName(oldFileName);
+ var pathNew = TSCORE.Utils.dirName(newFileName);
+ var path = TSCORE.currentPath;
+
+ if (pathNew.lastIndexOf(TSCORE.dirSeparator) === 0) {
+ pathOld += TSCORE.dirSeparator;
+ }
+
+ if (pathOld != pathNew) {
+ path = pathNew;
+ }
+ var newName = TSCORE.Utils.baseName(newFileName) + "." + element.name.split('.').pop();
+ var newFilePath = path + TSCORE.dirSeparator + TSCORE.metaFolder + TSCORE.dirSeparator + newName;
+ TSCORE.IO.copyFilePromise(element.path, newFilePath);
+
+ if (pathOld == TSCORE.currentPath) {
+ element.name = newName;
+ element.path = newFilePath;
+ } else {
+ TSCORE.metaFileList.splice(index, 1);
+ }
+
} else {
TSCORE.IO.deleteFilePromise(element.path).then(function() {
TSCORE.metaFileList.splice(index, 1);
@@ -210,7 +246,7 @@ define(function(require, exports, module) {
metaObj = {
thumbnailPath: "",
metaData: null,
- };
+ };
}
if (!metaObj.metaData) {
@@ -226,7 +262,7 @@ define(function(require, exports, module) {
tags.forEach(function(element) {
var newTag = {
"title": element,
- "type":"sidecar",
+ "type": "sidecar",
"style": TSCORE.generateTagStyle(TSCORE.Config.findTag(element))
};
var isNewTag = true;
@@ -236,7 +272,7 @@ define(function(require, exports, module) {
}
});
if (isNewTag) {
- metaObj.metaData.tags.push(newTag);
+ metaObj.metaData.tags.push(newTag);
}
});
@@ -246,7 +282,7 @@ define(function(require, exports, module) {
function renameMetaTag(filePath, oldTag, newTag) {
var metaObj = findMetaObjectFromFileList(filePath);
if (metaObj.metaData) {
- metaObj.metaData.tags.forEach(function(tag , index) {
+ metaObj.metaData.tags.forEach(function(tag, index) {
if (tag.title === oldTag) {
tag.title = newTag;
}
@@ -258,9 +294,9 @@ define(function(require, exports, module) {
function removeMetaTag(filePath, tagName) {
var metaObj = findMetaObjectFromFileList(filePath);
if (metaObj.metaData) {
- metaObj.metaData.tags.forEach(function(tag , index) {
+ metaObj.metaData.tags.forEach(function(tag, index) {
if (tag.title === tagName) {
- metaObj.metaData.tags.splice(index , 1);
+ metaObj.metaData.tags.splice(index, 1);
}
});
var metaFileJson = findMetaFilebyPath(filePath, TSCORE.metaFileExt);
@@ -287,7 +323,7 @@ define(function(require, exports, module) {
});
});
}
-
+
function createMetaFolder(dirPath) {
if (dirPath.lastIndexOf(TSCORE.metaFolder) >= dirPath.length - TSCORE.metaFolder.length) {
console.log("Can not create meta folder in a meta folder");
@@ -302,10 +338,11 @@ define(function(require, exports, module) {
}
exports.getDirectoryMetaInformation = getDirectoryMetaInformation;
- exports.findMetaFilebyPath = findMetaFilebyPath;
+ exports.findMetaFilebyPath = findMetaFilebyPath;
exports.findMetaObjectFromFileList = findMetaObjectFromFileList;
exports.saveMetaData = saveMetaData;
exports.updateMetaData = updateTsMetaData;
+ exports.copyMetaData = copyTsMetaData;
exports.loadMetaFileJsonPromise = loadMetaFileJsonPromise;
exports.loadThumbnailPromise = loadThumbnailPromise;
exports.getTagsFromMetaFile = getTagsFromMetaFile;