Skip to content

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

adding walkdirectory and creating index methods

uggrock95049aa68fcc
diff --git a/data/chromium/chrome.api.js b/data/chromium/chrome.api.js
index 20fe59e8e..ff62b991a 100644
--- a/data/chromium/chrome.api.js
+++ b/data/chromium/chrome.api.js
@@ -49,61 +49,6 @@ define(function(require, exports, module) {
return (false);
}
- function scanDirectory(dirPath, index) {
- /* $.ajax({
- url: "file://"+dirPath,
- type: 'GET'
- })
- .done(function(data) {
- //console.log("Dir List "+data);
- var folders = data.substring(data.indexOf(dataBegin)+dataBegin.length,data.lastIndexOf(dataEnd));
- folders = folders.split(dataBegin).join("");
- folders = folders.split(dataEnd);
-
- var name,
- path,
- isFile,
- fileSize,
- lastDateModified,
- fileProp;
-
- // sciping the first entry pointing to the parent directory
- for (var i=1; i < folders.length; i++) {
- console.log("Dir Req "+folders[i]);
- name = folders[i].substring(2,folders[i].indexOf('","'));
- path = dirPath+TSCORE.dirSeparator+name;
- isFile = (folders[i].indexOf(dataFile) > 1);
- fileSize = 0;
- lastDateModified = 0;
- if(isFile) {
- fileProp = folders[i].substring(folders[i].indexOf(dataFile)+dataFile.length+1,folders[i].length-1);
- fileProp = fileProp.split('","');
- fileSize = fileProp[0];
- lastDateModified = fileProp[1];
- }
- index.push({
- "name": name,
- "isFile": isFile,
- "size": fileSize,
- "lmdt": lastDateModified,
- "path": path
- });
- if (!isFile) {
- scanDirectory(path, index);
- }
- }
- return index;
- })
- .fail(function(data) {
- TSPOSTIO.errorOpeningPath(dirPath);
- console.log("Error opening path "+data);
- });*/
- }
-
- function generateDirectoryTree(dirPath) {
-
- }
-
var checkNewVersion = function() {
console.log("Checking for new version...");
var cVer = TSCORE.Config.DefaultSettings.appVersion + "." + TSCORE.Config.DefaultSettings.appBuild;
@@ -130,6 +75,37 @@ define(function(require, exports, module) {
);
};
+ function walkDirectory(path, options, fileCallback, dirCallback) {
+ return listDirectoryPromise(path).then(function(entries) {
+ return Promise.all(entries.map(function(entry) {
+ if(!options) {
+ options = {};
+ options.recursive = false;
+ }
+
+ if (entry.isFile) {
+ if(fileCallback) {
+ return fileCallback(entry);
+ } else {
+ return entry;
+ }
+ } else {
+ if(dirCallback) {
+ return dirCallback(entry);
+ }
+ if(options.recursive) {
+ return walkDirectory(entry.path, options, fileCallback, dirCallback);
+ } else {
+ return entry;
+ }
+ }
+ }), function(err) {
+ console.error("Error list dir prom " + err);
+ return null;
+ });
+ });
+ }
+
var listDirectory = function(dirPath, resultCallback) {
TSCORE.showLoadingAnimation();
listDirectoryPromise(dirPath).then(function(anotatedDirList) {
@@ -212,20 +188,30 @@ define(function(require, exports, module) {
};
var createDirectoryIndex = function(dirPath) {
- console.log("Creating index for directory: " + dirPath);
- TSCORE.showLoadingAnimation();
+ TSCORE.showWaitingDialog($.i18n.t("ns.common:waitDialogDiectoryIndexing"));
+ //TSCORE.showLoadingAnimation();
+
var directoryIndex = [];
- directoryIndex = scanDirectory(dirPath, directoryIndex);
- console.log(JSON.stringify(directoryIndex));
- TSPOSTIO.createDirectoryIndex(directoryIndex);
+ walkDirectory(dirPath, {recursive: true}, function(fileEntry) {
+ directoryIndex.push(fileEntry);
+ }).then(
+ function(entries) {
+ TSPOSTIO.createDirectoryIndex(directoryIndex);
+ },
+ function(err) {
+ console.warn("Error creating index: " + err);
+ }
+ ).catch(function() {
+ TSCORE.hideWaitingDialog();
+ });
};
var createDirectoryTree = function(dirPath) {
TSCORE.showLoadingAnimation();
- console.log("Creating directory index for: " + dirPath);
- var directoyTree = generateDirectoryTree(dirPath);
+ console.log("Creating directory not supported: " + dirPath);
+ //var directoyTree = generateDirectoryTree(dirPath);
//console.log(JSON.stringify(directoyTree));
- TSPOSTIO.createDirectoryTree(directoyTree);
+ //TSPOSTIO.createDirectoryTree(directoyTree);
};
var saveTextFile = function(filePath, content) {
@@ -437,4 +423,5 @@ define(function(require, exports, module) {
exports.getDirectoryMetaInformation = getDirectoryMetaInformation;
exports.getFileContentPromise = getFileContentPromise;
exports.listDirectoryPromise = listDirectoryPromise;
+ exports.walkDirectory = walkDirectory;
});