Skip to content

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

Introduce new module for meta data

Borislav Sapundzhiev759526a39220
diff --git a/data/ext/perspectiveGrid/perspectiveUI.js b/data/ext/perspectiveGrid/perspectiveUI.js
index 5bd61081c..bd2df7d0b 100644
--- a/data/ext/perspectiveGrid/perspectiveUI.js
+++ b/data/ext/perspectiveGrid/perspectiveUI.js
@@ -796,7 +796,7 @@ define(function(require, exports, module) {
$fileTile = $("#" + this.extensionID + "Container li[filepath='" + oldFilePath + "']");
}
- var metaObj = TSCORE.findMetaObjectFromFileList(oldFilePath);
+ var metaObj = TSCORE.Meta.findMetaObjectFromFileList(oldFilePath);
$fileTile.replaceWith(this.createFileTile(title, newFilePath, fileExt, fileTags, true, metaObj));
this.refreshThumbnails();
diff --git a/data/ext/perspectiveImageSwiper/perspectiveUI.js b/data/ext/perspectiveImageSwiper/perspectiveUI.js
index 525bc6115..74628c04b 100644
--- a/data/ext/perspectiveImageSwiper/perspectiveUI.js
+++ b/data/ext/perspectiveImageSwiper/perspectiveUI.js
@@ -39,7 +39,7 @@ define(function(require, exports, module) {
title: fileInfo[TSCORE.fileListTITLE]
}
- var metaFilePath = TSCORE.findMetaFilebyPath(filePath, "png");
+ var metaFilePath = TSCORE.Meta.findMetaFilebyPath(filePath, "png");
if(metaFilePath) {
doc.thumbnail = encodeURI("file://" + metaFilePath);
}
diff --git a/data/js/core.api.js b/data/js/core.api.js
index 52ce5ec65..4fe9e5475 100644
--- a/data/js/core.api.js
+++ b/data/js/core.api.js
@@ -17,6 +17,8 @@ define(function(require, exports, module) {
var tsSearch = require('tssearch');
var tsPro = require('tspro');
var tsUtils = require('tsutils');
+ var tsMeta = require('tsmeta');
+
var currentPath;
var currentLocationObject;
var currentPerspectiveID;
@@ -550,76 +552,6 @@ define(function(require, exports, module) {
}, 0);
}
- function findMetaFilebyPath(filePath, type) {
- var metaFilePath = null;
- filePath = filePath + "." + type;
- exports.metaFileList.every(function(element) {
- if (filePath.indexOf(element.name) > 0) {
- metaFilePath = exports.currentPath + exports.dirSeparator +
- exports.metaFolder + exports.dirSeparator + element.name;
- return false;
- }
- return true;
- });
- return metaFilePath;
- }
-
- function findMetaObjectFromFileList(filePath) {
- var metaObj = null;
- exports.fileList.every(function(element) {
- if(element[exports.fileListFILEPATH] === filePath) {
- metaObj = element[exports.fileListMETA];
- return false;
- }
- return true;
- });
- return metaObj;
- }
-
- function saveMetaData(filePath, metaData) {
-
- var metaFilePath = findMetaFilebyPath(filePath, exports.metsFileExt);
-
- if(!metaFilePath) {
- var name = exports.Utils.baseName(filePath) + "." + exports.metsFileExt;
- metaFilePath = exports.currentPath + exports.dirSeparator +
- exports.metaFolder + exports.dirSeparator + name;
-
- var entry = {
- "name": name,
- "isFile": true,
- "path": metaFilePath,
- };
- exports.metaFileList.push(entry);
- }
- var content = JSON.stringify(metaData);
- exports.IO.saveTextFile(metaFilePath, content, true, true);
- }
-
- function updateTsMetaData(oldFileName, newFileName) {
- var name = exports.Utils.baseName(oldFileName);
- exports.metaFileList.forEach(function(element, index) {
-
- if(element.name.indexOf(name) >= 0) {
-
- if(newFileName) {
- var newName = exports.Utils.baseName(newFileName) + "." + element.name.split('.').pop();
- var newFilePath = exports.currentPath + exports.dirSeparator +
- exports.metaFolder + exports.dirSeparator + newName;
-
- exports.IO.renameFile(element.path, newFilePath);
- element.name = newName;
- element.path = newFilePath;
-
- } else {
-
- exports.IO.deleteElement(element.path);
- exports.metaFileList.splice(index, 1);
- }
- }
- });
- }
-
// Proxying applications parts
exports.Config = tsSettings;
exports.IO = tsIOApi;
@@ -632,7 +564,7 @@ define(function(require, exports, module) {
if (tsPro.available) {
exports.PRO = tsPro;
}
-
+ exports.Meta = tsMeta;
// Public API definition
exports.dirSeparator = isWin && !isWeb ? '\\' : '/';
exports.locationDesktop;
@@ -721,15 +653,8 @@ define(function(require, exports, module) {
exports.fileListFILEPATH = 5;
exports.fileListFILENAME = 6;
exports.fileListMETA = 7;
- exports.metsFileExt = "json";
- exports.metaFolder = ".ts";
- exports.thumbFileExt = "png";
//document events
exports.createDocumentEvent = createDocumentEvent;
exports.fireDocumentEvent = fireDocumentEvent;
exports.metaFileList = metaFileList;
- exports.findMetaFilebyPath = findMetaFilebyPath;
- exports.findMetaObjectFromFileList = findMetaObjectFromFileList;
- exports.saveMetaData = saveMetaData;
- exports.updateMetaData = updateTsMetaData;
});
diff --git a/data/js/directories.ui.js b/data/js/directories.ui.js
index 8e3463e73..f773a82f1 100644
--- a/data/js/directories.ui.js
+++ b/data/js/directories.ui.js
@@ -356,15 +356,9 @@ define(function(require, exports, module) {
}
console.log('Dir History: ' + JSON.stringify(directoryHistory));
TSCORE.currentPath = directoryPath;
- if (TSCORE.IO.getDirectoryMetaInformation) {
- var metaFolderPath = TSCORE.currentPath + TSCORE.dirSeparator + TSCORE.metaFolder;
- TSCORE.IO.getDirectoryMetaInformation(metaFolderPath, function() {
- TSCORE.IO.listDirectory(directoryPath);
- });
- } else {
- TSCORE.IO.listDirectory(directoryPath);
- }
-
+ TSCORE.Meta.getDirectoryMetaInformation(function() {
+ TSCORE.IO.listDirectory(directoryPath);
+ });
}
function initUI() {
diff --git a/data/js/meta.js b/data/js/meta.js
new file mode 100644
index 000000000..7dcee8411
--- /dev/null
+++ b/data/js/meta.js
@@ -0,0 +1,242 @@
+define(function(require, exports, module) {
+ 'use strict';
+
+ var TSCORE = require("tscore");
+ var TSPRO = require("tspro");
+
+ var metaFileExt = "json";
+ var metaFolder = ".ts";
+ var thumbFileExt = "png";
+
+ function makeMetaPathByName(name) {
+ return TSCORE.currentPath + TSCORE.dirSeparator + metaFolder + name;
+ }
+
+ function getDirectoryMetaInformation(readyCallback) {
+
+ if (TSCORE.IO.getDirectoryMetaInformation) {
+ var metaFolderPath = TSCORE.currentPath + TSCORE.dirSeparator + metaFolder;
+ TSCORE.IO.getDirectoryMetaInformation(metaFolderPath, function() {
+ readyCallback()
+ });
+ } else {
+ readyCallback();
+ }
+ }
+
+ function findMetaFilebyPath(filePath, type) {
+ var metaFilePath = null;
+ filePath = filePath + "." + type;
+ TSCORE.metaFileList.every(function(element) {
+ if (filePath.indexOf(element.name) > 0) {
+ metaFilePath = TSCORE.currentPath + TSCORE.dirSeparator +
+ metaFolder + TSCORE.dirSeparator + element.name;
+ return false;
+ }
+ return true;
+ });
+ return metaFilePath;
+ }
+
+ function findMetaObjectFromFileList(filePath) {
+ var metaObj = null;
+ TSCORE.fileList.every(function(element) {
+ if(element[TSCORE.fileListFILEPATH] === filePath) {
+ metaObj = element[TSCORE.fileListMETA];
+ return false;
+ }
+ return true;
+ });
+ return metaObj;
+ }
+
+ function saveMetaData(filePath, metaData) {
+
+ var metaFilePath = findMetaFilebyPath(filePath, metaFileExt);
+
+ if(!metaFilePath) {
+ var name = TSCORE.Utils.baseName(filePath) + "." + metaFileExt;
+ metaFilePath = TSCORE.currentPath + TSCORE.dirSeparator +
+ metaFolder + TSCORE.dirSeparator + name;
+
+ var entry = {
+ "name": name,
+ "isFile": true,
+ "path": metaFilePath,
+ };
+ TSCORE.metaFileList.push(entry);
+ }
+
+ var content = JSON.stringify(metaData);
+ TSCORE.IO.saveTextFile(metaFilePath, content, true, true);
+ }
+
+ 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 newName = TSCORE.Utils.baseName(newFileName) + "." + element.name.split('.').pop();
+ var newFilePath = TSCORE.currentPath + TSCORE.dirSeparator +
+ metaFolder + TSCORE.dirSeparator + newName;
+
+ TSCORE.IO.renameFile(element.path, newFilePath);
+ element.name = newName;
+ element.path = newFilePath;
+
+ } else {
+ TSCORE.IO.deleteElement(element.path);
+ TSCORE.metaFileList.splice(index, 1);
+ }
+ }
+ });
+ }
+
+ function loadThumbnail(filePath) {
+ var promise = new Promise(function(resolve, reject) {
+ if (TSPRO.available) {
+ TSPRO.getThumbnailURL(filePath, function(dataURL) {
+ resolve(dataURL);
+ });
+ } else {
+ var metaFilePath = findMetaFilebyPath(filePath, thumbFileExt);
+ if (metaFilePath && isChrome) {
+ metaFilePath = "file://" + metaFilePath;
+ }
+ resolve(metaFilePath);
+ }
+ });
+ return promise;
+ }
+
+ function loadMetaFileJson(filePath) {
+ var promise = new Promise(function(resolve, reject) {
+ var metaFileJson = findMetaFilebyPath(filePath, metaFileExt);
+ if (metaFileJson) {
+ TSCORE.IO.getFileContent(metaFileJson, function(result) {
+ try {
+ var str = String.fromCharCode.apply(null, new Uint8Array(result));
+ str = (str.charCodeAt(0) != 0x7B) ? str.substring(3, str.length) : str;
+ var metaData = JSON.parse(str);
+ resolve(metaData);
+ } catch (e) {
+ console.log("loadMetaFileJson: error: " + e.message);
+ resolve(null);
+ }
+ });
+ } else {
+ resolve(null);
+ }
+ });
+ return promise;
+ }
+
+ function loadMetaDataFromFile(entry) {
+ var filePath = entry[TSCORE.fileListFILEPATH];
+ var promise = new Promise(function(resolve, reject) {
+ loadMetaFileJson(filePath).then(function(result) {
+ entry[TSCORE.fileListMETA].metaData = result;
+ loadThumbnail(filePath).then(function(result) {
+ entry[TSCORE.fileListMETA].thumbnailPath = result;
+ resolve(entry);
+ });
+ });
+ });
+ return promise;
+ }
+
+ function getTagsFromMetaFile(filePath) {
+ var tags = [];
+ var metaObj = findMetaObjectFromFileList(filePath);
+ if (metaObj.metaData && metaObj.metaData.tags) {
+ metaObj.metaData.tags.forEach(function(elem) {
+ tags.push({
+ tag: elem.title,
+ filepath: filePath,
+ style: elem.style
+ });
+ });
+ }
+ return tags;
+ }
+ //meta tag utils
+ function addMetaTags(filePath, tags) {
+
+ var metaObj = findMetaObjectFromFileList(filePath);
+ if (!metaObj) {
+ metaObj = {
+ thumbnailPath: "",
+ metaData: null,
+ };
+ }
+
+ if (!metaObj.metaData) {
+ metaObj.metaData = {
+ tags: []
+ };
+ }
+
+ tags.forEach(function(element) {
+ var newTag = {
+ "title": element,
+ "type":"plain",
+ "titleUI": element,
+ "icon":"",
+ "style":""
+ };
+ var isNewTag = true;
+ metaObj.metaData.tags.forEach(function(oldTag) {
+ if (oldTag.title === element) {
+ isNewTag = false;
+ }
+ });
+ if (isNewTag) {
+ metaObj.metaData.tags.push(newTag);
+ }
+ });
+
+ saveMetaData(filePath, metaObj.metaData);
+ }
+
+ function reanmeMetaTag(filePath, oldTag, newTag) {
+ var metaObj = findMetaObjectFromFileList(filePath);
+ if(metaObj.metaData) {
+ metaObj.metaData.tags.forEach(function(tag , index) {
+ if(tag.title === oldTag) {
+ tag.title = newTag;
+ }
+ });
+ saveMetaData(filePath, metaObj.metaData);
+ }
+ }
+
+ function removeMetaTag(filePath, tagName) {
+ var metaObj = findMetaObjectFromFileList(filePath);
+ if(metaObj.metaData) {
+ metaObj.metaData.tags.forEach(function(tag , index) {
+ if(tag.title === tagName) {
+ metaObj.metaData.tags.splice(index , 1);
+ }
+ });
+ var metaFileJson = findMetaFilebyPath(filePath, metaFileExt);
+ if (metaFileJson) {
+ var content = JSON.stringify(metaObj.metaData);
+ TSCORE.IO.saveTextFile(metaFileJson, content, true, true);
+ }
+ }
+ }
+
+ exports.getDirectoryMetaInformation = getDirectoryMetaInformation;
+ exports.findMetaFilebyPath = findMetaFilebyPath;
+ exports.findMetaObjectFromFileList = findMetaObjectFromFileList;
+ exports.saveMetaData = saveMetaData;
+ exports.updateMetaData = updateTsMetaData;
+ exports.loadMetaDataFromFile = loadMetaDataFromFile;
+ exports.getTagsFromMetaFile = getTagsFromMetaFile;
+ //tag utils
+ exports.addMetaTags = addMetaTags;
+ exports.reanmeMetaTag = reanmeMetaTag;
+ exports.removeMetaTag = removeMetaTag;
+});
\ No newline at end of file
diff --git a/data/js/perspective.manager.js b/data/js/perspective.manager.js
index b8393890b..0c5fe1bad 100644
--- a/data/js/perspective.manager.js
+++ b/data/js/perspective.manager.js
@@ -275,7 +275,7 @@ define(function(require, exports, module) {
metaObj
];
TSCORE.fileList.push(entry);
- workers.push(loadMetaDataFromFile(entry));
+ workers.push(TSCORE.Meta.loadMetaDataFromFile(entry));
} else {
entry = [
path,
@@ -292,57 +292,7 @@ define(function(require, exports, module) {
alert("MetaData Error: " + error);
});
};
-
- function loadThumbnail(filePath) {
- var promise = new Promise(function(resolve, reject) {
- if (TSPRO.available) {
- TSPRO.getThumbnailURL(filePath, function(dataURL) {
- resolve(dataURL);
- });
- } else {
- var metaFilePath = TSCORE.findMetaFilebyPath(filePath, TSCORE.thumbFileExt);
- if (metaFilePath && isChrome) {
- metaFilePath = "file://" + metaFilePath;
- }
- resolve(metaFilePath);
- }
- });
- return promise;
- }
-
- function loadMetaFileJson(filePath) {
- var promise = new Promise(function(resolve, reject) {
- var metaFileJson = TSCORE.findMetaFilebyPath(filePath, TSCORE.metsFileExt);
- if(metaFileJson) {
- TSCORE.IO.getFileContent(metaFileJson, function(result) {
- var str = String.fromCharCode.apply(null, new Uint8Array(result));
- if (str.charCodeAt(0) != 0x7B) {
- str = str.substring(3, str.length);
- }
- var metaData = JSON.parse(str);
- resolve(metaData);
- });
- } else {
- resolve(null);
- }
- });
- return promise;
- }
-
- function loadMetaDataFromFile(entry) {
- var filePath = entry[TSCORE.fileListFILEPATH];
- var promise = new Promise(function(resolve, reject) {
- loadMetaFileJson(filePath).then(function(result) {
- entry[TSCORE.fileListMETA].metaData = result;
- loadThumbnail(filePath).then(function(result) {
- entry[TSCORE.fileListMETA].thumbnailPath = result;
- resolve(entry);
- });
- });
- });
- return promise;
- }
-
+
var refreshFileListContainer = function() {
// TODO consider search view
TSCORE.IO.listDirectory(TSCORE.currentPath);
diff --git a/data/js/postioapi.js b/data/js/postioapi.js
index 710ae7a74..ff2c28cd2 100644
--- a/data/js/postioapi.js
+++ b/data/js/postioapi.js
@@ -54,7 +54,7 @@ define(function(require, exports, module) {
TSCORE.updateFileModel(TSCORE.fileList, oldFilePath, newFilePath);
TSCORE.PerspectiveManager.updateFileUI(oldFilePath, newFilePath);
}
- TSCORE.updateMetaData(oldFilePath, newFilePath);
+ TSCORE.Meta.updateMetaData(oldFilePath, newFilePath);
TSCORE.hideWaitingDialog(); //TSCORE.PerspectiveManager.clearSelectedFiles();
};
diff --git a/data/js/tags.ui.js b/data/js/tags.ui.js
index 978909b4e..256fbda98 100644
--- a/data/js/tags.ui.js
+++ b/data/js/tags.ui.js
@@ -391,18 +391,10 @@ define(function(require, exports, module) {
});
}
}
-
- var metaObj = TSCORE.findMetaObjectFromFileList(filePath);
- if (metaObj.metaData && metaObj.metaData.tags) {
- metaObj.metaData.tags.forEach(function(elem) {
- context.tags.push({
- tag: elem.title,
- filepath: filePath,
- style: elem.style
- });
- });
+ var metaTags = TSCORE.Meta.getTagsFromMetaFile(filePath);
+ if(metaTags.length > 0) {
+ context.tags = context.tags.concat(metaTags);
}
-
return tagButtonTmpl(context);
}
// Get the color for a tag
diff --git a/data/js/tagutils.js b/data/js/tagutils.js
index 83ae5ea12..3511b0d5f 100644
--- a/data/js/tagutils.js
+++ b/data/js/tagutils.js
@@ -332,50 +332,12 @@ define(function(require, exports, module) {
return newFileName;
}
- function addMetaTags(filePath, tags) {
-
- var metaObj = TSCORE.findMetaObjectFromFileList(filePath);
- if (!metaObj) {
- metaObj = {
- thumbnailPath: "",
- metaData: null,
- };
- }
-
- if (!metaObj.metaData) {
- metaObj.metaData = {
- tags: []
- };
- }
-
- tags.forEach(function(element) {
- var newTag = {
- "title": element,
- "type":"plain",
- "titleUI": element,
- "icon":"",
- "style":""
- };
- var isNewTag = true;
- metaObj.metaData.tags.forEach(function(oldTag) {
- if (oldTag.title === element) {
- isNewTag = false;
- }
- });
- if (isNewTag) {
- metaObj.metaData.tags.push(newTag);
- }
- });
-
- TSCORE.saveMetaData(filePath, metaObj.metaData);
- TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
- }
-
function writeTagsToFile(filePath, tags) {
console.log('Add the tags to: ' + filePath);
if (TSCORE.Config.DefaultSettings.writeTagsToFile !== true) {
- addMetaTags(filePath, tags);
+ TSCORE.Meta.addMetaTags(filePath, tags);
+ TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
return;
}
@@ -475,24 +437,13 @@ define(function(require, exports, module) {
TSCORE.IO.renameFile(filePath, containingDirectoryPath + TSCORE.dirSeparator + newFileName);
}
- function reanmeMetaTag(filePath, oldTag, newTag) {
- var metaObj = TSCORE.findMetaObjectFromFileList(filePath);
- if(metaObj.metaData) {
- metaObj.metaData.tags.forEach(function(tag , index) {
- if(tag.title === oldTag) {
- tag.title = newTag;
- }
- });
- TSCORE.saveMetaData(filePath, metaObj.metaData);
- TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
- }
- }
// Replaces a tag with a new one
function renameTag(filePath, oldTag, newTag) {
console.log('Rename tag for file: ' + filePath);
if (TSCORE.Config.DefaultSettings.writeTagsToFile !== true) {
- reanmeMetaTag(filePath, oldTag, newTag);
+ TSCORE.Meta.reanmeMetaTag(filePath, oldTag, newTag);
+ TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
}
var fileName = extractFileName(filePath);
@@ -524,29 +475,13 @@ define(function(require, exports, module) {
return true;
}
- function removeMetaTag(filePath, tagName) {
- var metaObj = TSCORE.findMetaObjectFromFileList(filePath);
- if(metaObj.metaData) {
- metaObj.metaData.tags.forEach(function(tag , index) {
- if(tag.title === tagName) {
- metaObj.metaData.tags.splice(index , 1);
- }
- });
- var metaFileJson = TSCORE.findMetaFilebyPath(filePath, TSCORE.metsFileExt);
- if (metaFileJson) {
- var content = JSON.stringify(metaObj.metaData);
- TSCORE.IO.saveTextFile(metaFileJson, content, true, true);
- }
- TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
- }
- }
-
// Removing a tag from a filename
function removeTag(filePath, tagName) {
console.log('Removing tag: ' + tagName + ' from ' + filePath);
if (TSCORE.Config.DefaultSettings.writeTagsToFile !== true) {
- removeMetaTag(filePath, tagName);
+ TSCORE.Meta.removeMetaTag(filePath, tagName);
+ TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
}
var fileName = extractFileName(filePath);