Drive refactoring
This commit is contained in:
@@ -8,7 +8,6 @@ define([
|
||||
var UNSORTED = module.UNSORTED = "unsorted";
|
||||
var TRASH = module.TRASH = "trash";
|
||||
var TEMPLATE = module.TEMPLATE = "template";
|
||||
var NEW_FILES_DATA = 'filesData';
|
||||
|
||||
module.init = function (files, config) {
|
||||
var exp = {};
|
||||
@@ -16,6 +15,7 @@ define([
|
||||
var Messages = Cryptpad.Messages;
|
||||
|
||||
var FILES_DATA = module.FILES_DATA = exp.FILES_DATA = Cryptpad.storageKey;
|
||||
var OLD_FILES_DATA = module.OLD_FILES_DATA = exp.OLD_FILES_DATA = Cryptpad.oldStorageKey;
|
||||
var NEW_FOLDER_NAME = Messages.fm_newFolder;
|
||||
var NEW_FILE_NAME = Messages.fm_newFile;
|
||||
|
||||
@@ -43,7 +43,7 @@ define([
|
||||
var a = {};
|
||||
a[ROOT] = {};
|
||||
a[TRASH] = {};
|
||||
a[FILES_DATA] = [];
|
||||
a[FILES_DATA] = {};
|
||||
a[TEMPLATE] = [];
|
||||
return a;
|
||||
};
|
||||
@@ -54,15 +54,19 @@ define([
|
||||
|
||||
var compareFiles = function (fileA, fileB) { return fileA === fileB; };
|
||||
|
||||
var isFile = exp.isFile = function (element) {
|
||||
return typeof(element) === "number" || typeof(element) === "string";
|
||||
var isFile = exp.isFile = function (element, allowStr) {
|
||||
return typeof(element) === "number" ||
|
||||
((typeof(files[OLD_FILES_DATA]) !== "undefined" || allowStr)
|
||||
&& typeof(element) === "string");
|
||||
};
|
||||
|
||||
exp.isReadOnlyFile = function (element) {
|
||||
if (!isFile(element)) { return false; }
|
||||
var parsed = Cryptpad.parsePadUrl(element);
|
||||
var data = exp.getFileData(element);
|
||||
var parsed = Cryptpad.parsePadUrl(data.href);
|
||||
if (!parsed) { return false; }
|
||||
var pHash = parsed.hashData;
|
||||
if (!pHash || pHash.type !== "pad") { return; }
|
||||
return pHash && pHash.mode === 'view';
|
||||
};
|
||||
|
||||
@@ -70,12 +74,12 @@ define([
|
||||
return typeof(element) === "object";
|
||||
};
|
||||
exp.isFolderEmpty = function (element) {
|
||||
if (typeof(element) !== "object") { return false; }
|
||||
if (!isFolder(element)) { return false; }
|
||||
return Object.keys(element).length === 0;
|
||||
};
|
||||
|
||||
exp.hasSubfolder = function (element, trashRoot) {
|
||||
if (typeof(element) !== "object") { return false; }
|
||||
if (!isFolder(element)) { return false; }
|
||||
var subfolder = 0;
|
||||
var addSubfolder = function (el) {
|
||||
subfolder += isFolder(el.element) ? 1 : 0;
|
||||
@@ -93,7 +97,7 @@ define([
|
||||
};
|
||||
|
||||
exp.hasFile = function (element, trashRoot) {
|
||||
if (typeof(element) !== "object") { return false; }
|
||||
if (!isFolder(element)) { return false; }
|
||||
var file = 0;
|
||||
var addFile = function (el) {
|
||||
file += isFile(el.element) ? 1 : 0;
|
||||
@@ -113,29 +117,33 @@ define([
|
||||
// Get data from AllFiles (Cryptpad_RECENTPADS)
|
||||
var getFileData = exp.getFileData = function (file) {
|
||||
if (!file) { return; }
|
||||
var res;
|
||||
files[FILES_DATA].some(function(arr) {
|
||||
var href = arr.href;
|
||||
if (href === file) {
|
||||
res = arr;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return res;
|
||||
return files[FILES_DATA][file] || {};
|
||||
};
|
||||
|
||||
// Data from filesData
|
||||
var getTitle = exp.getTitle = function (href) {
|
||||
var getTitle = exp.getTitle = function (file, type) {
|
||||
if (workgroup) { debug("No titles in workgroups"); return; }
|
||||
var data = getFileData(href);
|
||||
if (!href || !data) {
|
||||
error("getTitle called with a non-existing href: ", href);
|
||||
var data = getFileData(file);
|
||||
if (!file || !data || !data.href) {
|
||||
error("getTitle called with a non-existing file id: ", file, data);
|
||||
return;
|
||||
}
|
||||
return data.title;
|
||||
if (type === 'title') { return data.title; }
|
||||
if (type === 'name') { return data.filename; }
|
||||
return data.filename || data.title || NEW_FILE_NAME;
|
||||
};
|
||||
|
||||
var getIdFromHref = exp.getIdFromHref = function (href) {
|
||||
var result;
|
||||
getFiles([FILES_DATA]).some(function (id) {
|
||||
if (files[FILES_DATA][id].href === href) {
|
||||
result = id;
|
||||
return true;
|
||||
}
|
||||
return;
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
// PATHS
|
||||
|
||||
@@ -246,31 +254,25 @@ define([
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
_getFiles[FILES_DATA] = function () {
|
||||
_getFiles[OLD_FILES_DATA] = function () {
|
||||
var ret = [];
|
||||
if (!files[FILES_DATA]) { return ret; }
|
||||
files[FILES_DATA].forEach(function (el) {
|
||||
if (!files[OLD_FILES_DATA]) { return ret; }
|
||||
files[OLD_FILES_DATA].forEach(function (el) {
|
||||
if (el.href && ret.indexOf(el.href) === -1) {
|
||||
ret.push(el.href);
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
};
|
||||
_getFiles[NEW_FILES_DATA] = function () {
|
||||
_getFiles[FILES_DATA] = function () {
|
||||
var ret = [];
|
||||
if (!files[NEW_FILES_DATA]) { return ret; }
|
||||
for (var k in files[NEW_FILES_DATA]) {
|
||||
var el = files[NEW_FILES_DATA][k];
|
||||
if (el.href && ret.indexOf(el.href) === -1) {
|
||||
ret.push(el.href);
|
||||
}
|
||||
};
|
||||
return ret;
|
||||
if (!files[FILES_DATA]) { return ret; }
|
||||
return Object.keys(files[FILES_DATA]).map(Number);
|
||||
};
|
||||
var getFiles = exp.getFiles = function (categories) {
|
||||
var ret = [];
|
||||
if (!categories || !categories.length) {
|
||||
categories = [ROOT, 'hrefArray', TRASH, FILES_DATA, NEW_FILES_DATA];
|
||||
categories = [ROOT, 'hrefArray', TRASH, OLD_FILES_DATA, FILES_DATA];
|
||||
}
|
||||
categories.forEach(function (c) {
|
||||
if (typeof _getFiles[c] === "function") {
|
||||
@@ -281,7 +283,7 @@ define([
|
||||
};
|
||||
|
||||
// SEARCH
|
||||
var _findFileInRoot = function (path, href) {
|
||||
var _findFileInRoot = function (path, file) {
|
||||
if (!isPathIn(path, [ROOT, TRASH])) { return []; }
|
||||
var paths = [];
|
||||
var root = find(path);
|
||||
@@ -292,7 +294,7 @@ define([
|
||||
};
|
||||
|
||||
if (isFile(root)) {
|
||||
if (compareFiles(href, root)) {
|
||||
if (compareFiles(file, root)) {
|
||||
if (paths.indexOf(path) === -1) {
|
||||
paths.push(path);
|
||||
}
|
||||
@@ -302,25 +304,25 @@ define([
|
||||
for (var e in root) {
|
||||
var nPath = path.slice();
|
||||
nPath.push(e);
|
||||
_findFileInRoot(nPath, href).forEach(addPaths);
|
||||
_findFileInRoot(nPath, file).forEach(addPaths);
|
||||
}
|
||||
|
||||
return paths;
|
||||
};
|
||||
exp.findFileInRoot = function (href) {
|
||||
return _findFileInRoot([ROOT], href);
|
||||
exp.findFileInRoot = function (file) {
|
||||
return _findFileInRoot([ROOT], file);
|
||||
};
|
||||
var _findFileInHrefArray = function (rootName, href) {
|
||||
var _findFileInHrefArray = function (rootName, file) {
|
||||
if (!files[rootName]) { return []; }
|
||||
var unsorted = files[rootName].slice();
|
||||
var ret = [];
|
||||
var i = -1;
|
||||
while ((i = unsorted.indexOf(href, i+1)) !== -1){
|
||||
while ((i = unsorted.indexOf(file, i+1)) !== -1){
|
||||
ret.push([rootName, i]);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
var _findFileInTrash = function (path, href) {
|
||||
var _findFileInTrash = function (path, file) {
|
||||
var root = find(path);
|
||||
var paths = [];
|
||||
var addPaths = function (p) {
|
||||
@@ -334,7 +336,7 @@ define([
|
||||
if (!Array.isArray(arr)) { return; }
|
||||
var nPath = path.slice();
|
||||
nPath.push(key);
|
||||
_findFileInTrash(nPath, href).forEach(addPaths);
|
||||
_findFileInTrash(nPath, file).forEach(addPaths);
|
||||
});
|
||||
}
|
||||
if (path.length === 2) {
|
||||
@@ -344,70 +346,44 @@ define([
|
||||
nPath.push(i);
|
||||
nPath.push('element');
|
||||
if (isFile(el.element)) {
|
||||
if (compareFiles(href, el.element)) {
|
||||
if (compareFiles(file, el.element)) {
|
||||
addPaths(nPath);
|
||||
}
|
||||
return;
|
||||
}
|
||||
_findFileInTrash(nPath, href).forEach(addPaths);
|
||||
_findFileInTrash(nPath, file).forEach(addPaths);
|
||||
});
|
||||
}
|
||||
if (path.length >= 4) {
|
||||
_findFileInRoot(path, href).forEach(addPaths);
|
||||
_findFileInRoot(path, file).forEach(addPaths);
|
||||
}
|
||||
return paths;
|
||||
};
|
||||
var findFile = exp.findFile = function (href) {
|
||||
var rootpaths = _findFileInRoot([ROOT], href);
|
||||
var templatepaths = _findFileInHrefArray(TEMPLATE, href);
|
||||
var trashpaths = _findFileInTrash([TRASH], href);
|
||||
var findFile = exp.findFile = function (file) {
|
||||
var rootpaths = _findFileInRoot([ROOT], file);
|
||||
var templatepaths = _findFileInHrefArray(TEMPLATE, file);
|
||||
var trashpaths = _findFileInTrash([TRASH], file);
|
||||
return rootpaths.concat(templatepaths, trashpaths);
|
||||
};
|
||||
exp.search = function (value) {
|
||||
if (typeof(value) !== "string") { return []; }
|
||||
var res = [];
|
||||
// Search in ROOT
|
||||
var findIn = function (root) {
|
||||
Object.keys(root).forEach(function (k) {
|
||||
if (isFile(root[k])) {
|
||||
if (k.toLowerCase().indexOf(value.toLowerCase()) !== -1) {
|
||||
res.push(root[k]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
findIn(root[k]);
|
||||
});
|
||||
};
|
||||
findIn(files[ROOT]);
|
||||
// Search in TRASH
|
||||
var trash = files[TRASH];
|
||||
Object.keys(trash).forEach(function (k) {
|
||||
if (k.toLowerCase().indexOf(value.toLowerCase()) !== -1) {
|
||||
trash[k].forEach(function (el) {
|
||||
if (isFile(el.element)) {
|
||||
res.push(el.element);
|
||||
}
|
||||
});
|
||||
}
|
||||
trash[k].forEach(function (el) {
|
||||
if (isFolder(el.element)) {
|
||||
findIn(el.element);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Search title
|
||||
var allFilesList = files[FILES_DATA].slice();
|
||||
allFilesList.forEach(function (t) {
|
||||
if (t.title && t.title.toLowerCase().indexOf(value.toLowerCase()) !== -1) {
|
||||
res.push(t.href);
|
||||
var allFilesList = files[FILES_DATA];
|
||||
var lValue = value.toLowerCase();
|
||||
getFiles([FILES_DATA]).forEach(function (id) {
|
||||
var data = allFilesList[id];
|
||||
if ((data.title && data.title.toLowerCase().indexOf(lValue) !== -1) ||
|
||||
(data.filename && data.filename.toLowerCase().indexOf(lValue) !== -1)) {
|
||||
res.push(id);
|
||||
}
|
||||
});
|
||||
|
||||
// Search Href
|
||||
var href = Cryptpad.getRelativeHref(value);
|
||||
if (href) {
|
||||
res.push(href);
|
||||
var id = getIdFromHref(href);
|
||||
if (id) { res.push(id); }
|
||||
}
|
||||
|
||||
res = Cryptpad.deduplicateString(res);
|
||||
@@ -442,24 +418,21 @@ define([
|
||||
var pushFileData = exp.pushData = function (data, cb) {
|
||||
if (typeof cb !== "function") { cb = function () {}; }
|
||||
var todo = function () {
|
||||
files[FILES_DATA].push(data);
|
||||
cb();
|
||||
var id = Cryptpad.createRandomInteger();
|
||||
files[FILES_DATA][id] = data;
|
||||
cb(null, id);
|
||||
};
|
||||
if (!Cryptpad.isLoggedIn() || !AppConfig.enablePinning) { return void todo(); }
|
||||
if (!Cryptpad.isLoggedIn() || !AppConfig.enablePinning || config.testMode) {
|
||||
return void todo();
|
||||
}
|
||||
Cryptpad.pinPads([Cryptpad.hrefToHexChannelId(data.href)], function (e) {
|
||||
if (e) { return void cb(e); }
|
||||
todo();
|
||||
});
|
||||
};
|
||||
var spliceFileData = exp.removeData = function (idx) {
|
||||
var data = files[FILES_DATA][idx];
|
||||
if (typeof data === "object" && Cryptpad.isLoggedIn() && AppConfig.enablePinning) {
|
||||
Cryptpad.unpinPads([Cryptpad.hrefToHexChannelId(data.href)], function (e, hash) {
|
||||
if (e) { return void logError(e); }
|
||||
debug('UNPIN', hash);
|
||||
});
|
||||
}
|
||||
files[FILES_DATA].splice(idx, 1);
|
||||
var spliceFileData = exp.removeData = function (id) {
|
||||
files[FILES_DATA][id] = undefined;
|
||||
delete files[FILES_DATA][id];
|
||||
};
|
||||
|
||||
// MOVE
|
||||
@@ -506,16 +479,10 @@ define([
|
||||
}
|
||||
}
|
||||
// Move to root
|
||||
var name;
|
||||
if (isPathIn(elementPath, ['hrefArray'])) {
|
||||
name = getTitle(element);
|
||||
} else if (isInTrashRoot(elementPath)) {
|
||||
// Element from the trash root: elementPath = [TRASH, "{dirName}", 0, 'element']
|
||||
name = elementPath[1];
|
||||
} else {
|
||||
name = elementPath[elementPath.length-1];
|
||||
}
|
||||
var newName = !isPathIn(elementPath, [ROOT]) ? getAvailableName(newParent, name) : name;
|
||||
var newName = isFile(element) ?
|
||||
getAvailableName(newParent, Cryptpad.createChannelId()) :
|
||||
isInTrashRoot(elementPath) ?
|
||||
elementPath[1] : elementPath.pop();
|
||||
|
||||
if (typeof(newParent[newName]) !== "undefined") {
|
||||
log(Messages.fo_unavailableName);
|
||||
@@ -536,7 +503,7 @@ define([
|
||||
return;
|
||||
}
|
||||
// Try to copy, and if success, remove the element from the old location
|
||||
if (copyElement(p, newPath)) {
|
||||
if (copyElement(p.slice(), newPath)) {
|
||||
toRemove.push(p);
|
||||
}
|
||||
});
|
||||
@@ -552,11 +519,10 @@ define([
|
||||
|
||||
|
||||
// ADD
|
||||
var add = exp.add = function (data, path) {
|
||||
var add = exp.add = function (id, path) {
|
||||
if (!Cryptpad.isLoggedIn()) { return; }
|
||||
var data = files[FILES_DATA][id];
|
||||
if (!data || typeof(data) !== "object") { return; }
|
||||
var href = data.href;
|
||||
var name = data.title;
|
||||
var newPath = path, parentEl;
|
||||
if (path && !Array.isArray(path)) {
|
||||
newPath = decodeURIComponent(path).split(',');
|
||||
@@ -564,43 +530,20 @@ define([
|
||||
// Add to href array
|
||||
if (path && isPathIn(newPath, ['hrefArray'])) {
|
||||
parentEl = find(newPath);
|
||||
parentEl.push(href);
|
||||
parentEl.push(id);
|
||||
return;
|
||||
}
|
||||
// Add to root if path is ROOT or if no path
|
||||
var filesList = getFiles([ROOT, TRASH, 'hrefArray']);
|
||||
if ((path && isPathIn(newPath, [ROOT]) || filesList.indexOf(href) === -1) && name) {
|
||||
if (path && isPathIn(newPath, [ROOT]) || filesList.indexOf(href) === -1) {
|
||||
parentEl = find(newPath || [ROOT]);
|
||||
if (parentEl) {
|
||||
var newName = getAvailableName(parentEl, name);
|
||||
parentEl[newName] = href;
|
||||
var newName = getAvailableName(parentEl, Cryptpad.createChannelId());
|
||||
parentEl[newName] = id;
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
exp.addFile = function (filePath, name, type, cb) {
|
||||
var parentEl = findElement(files, filePath);
|
||||
var fileName = getAvailableName(parentEl, name || NEW_FILE_NAME);
|
||||
var href = '/' + type + '/#' + Cryptpad.createRandomHash();
|
||||
|
||||
pushFileData({
|
||||
href: href,
|
||||
title: fileName,
|
||||
atime: +new Date(),
|
||||
ctime: +new Date()
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
logError(err);
|
||||
return void cb(err);
|
||||
}
|
||||
parentEl[fileName] = href;
|
||||
var newPath = filePath.slice();
|
||||
newPath.push(fileName);
|
||||
cb(void 0, {
|
||||
newPath: newPath
|
||||
});
|
||||
});
|
||||
};
|
||||
exp.addFolder = function (folderPath, name, cb) {
|
||||
var parentEl = find(folderPath);
|
||||
var folderName = getAvailableName(parentEl, name || NEW_FOLDER_NAME);
|
||||
@@ -614,19 +557,15 @@ define([
|
||||
|
||||
// FORGET (move with href not path)
|
||||
exp.forget = function (href) {
|
||||
var id = getIdFromHref(href);
|
||||
if (!id) { return; }
|
||||
if (!Cryptpad.isLoggedIn()) {
|
||||
// delete permanently
|
||||
var data = getFileData(href);
|
||||
if (data) {
|
||||
var i = find([FILES_DATA]).indexOf(data);
|
||||
if (i !== -1) {
|
||||
exp.removePadAttribute(href);
|
||||
spliceFileData(i);
|
||||
}
|
||||
}
|
||||
exp.removePadAttribute(href);
|
||||
spliceFileData(id);
|
||||
return;
|
||||
}
|
||||
var paths = findFile(href);
|
||||
var paths = findFile(id);
|
||||
move(paths, [TRASH]);
|
||||
};
|
||||
|
||||
@@ -648,29 +587,21 @@ define([
|
||||
});
|
||||
};
|
||||
var checkDeletedFiles = function () {
|
||||
// Nothing in FILES_DATA for workgroups
|
||||
// Nothing in OLD_FILES_DATA for workgroups
|
||||
if (workgroup || !Cryptpad.isLoggedIn()) { return; }
|
||||
|
||||
var filesList = getFiles([ROOT, 'hrefArray', TRASH]);
|
||||
var toRemove = [];
|
||||
files[FILES_DATA].forEach(function (arr) {
|
||||
var f = arr.href;
|
||||
if (filesList.indexOf(f) === -1) {
|
||||
toRemove.push(arr);
|
||||
}
|
||||
});
|
||||
toRemove.forEach(function (f) {
|
||||
var idx = files[FILES_DATA].indexOf(f);
|
||||
if (idx !== -1) {
|
||||
debug("Removing", f, "from filesData");
|
||||
spliceFileData(idx);
|
||||
removePadAttribute(f.href);
|
||||
var fData = files[FILES_DATA];
|
||||
getFiles([FILES_DATA]).forEach(function (id) {
|
||||
if (filesList.indexOf(id) === -1) {
|
||||
removePadAttribute(fData[id].href);
|
||||
spliceFileData(id);
|
||||
}
|
||||
});
|
||||
};
|
||||
var deleteHrefs = function (hrefs) {
|
||||
hrefs.forEach(function (obj) {
|
||||
var idx = files[obj.root].indexOf(obj.href);
|
||||
var deleteHrefs = function (ids) {
|
||||
ids.forEach(function (obj) {
|
||||
var idx = files[obj.root].indexOf(obj.id);
|
||||
files[obj.root].splice(idx, 1);
|
||||
});
|
||||
};
|
||||
@@ -690,27 +621,24 @@ define([
|
||||
var toSplice = [];
|
||||
allFilesPaths.forEach(function (path) {
|
||||
var el = find(path);
|
||||
toSplice.push(el);
|
||||
});
|
||||
toSplice.forEach(function (el) {
|
||||
var i = find([FILES_DATA]).indexOf(el);
|
||||
if (i === -1) { return; }
|
||||
if (!el) { return; }
|
||||
var id = getIdFromHref(el.href);
|
||||
if (!id) { return; }
|
||||
spliceFileData(id);
|
||||
removePadAttribute(el.href);
|
||||
console.log(el.href);
|
||||
spliceFileData(i);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var hrefs = [];
|
||||
var ids = [];
|
||||
hrefPaths.forEach(function (path) {
|
||||
var href = find(path);
|
||||
hrefs.push({
|
||||
var id = find(path);
|
||||
ids.push({
|
||||
root: path[0],
|
||||
href: href
|
||||
id: id
|
||||
});
|
||||
});
|
||||
deleteHrefs(hrefs);
|
||||
deleteHrefs(ids);
|
||||
|
||||
rootPaths.forEach(function (path) {
|
||||
var parentPath = path.slice();
|
||||
@@ -741,7 +669,7 @@ define([
|
||||
deleteMultipleTrashRoot(trashRoot);
|
||||
|
||||
// In some cases, we want to remove pads from a location without removing them from
|
||||
// FILES_DATA (replaceHref)
|
||||
// OLD_FILES_DATA (replaceHref)
|
||||
if (!nocheck) { checkDeletedFiles(); }
|
||||
};
|
||||
exp.delete = function (paths, cb, nocheck) {
|
||||
@@ -760,65 +688,70 @@ define([
|
||||
logError('Renaming `root` is forbidden');
|
||||
return;
|
||||
}
|
||||
if (!newName || newName.trim() === "") { return; }
|
||||
// Copy the element path and remove the last value to have the parent path and the old name
|
||||
var element = find(path);
|
||||
var parentPath = path.slice();
|
||||
var oldName = parentPath.pop();
|
||||
if (oldName === newName) {
|
||||
|
||||
// Folders
|
||||
if (isFolder(element)) {
|
||||
var parentPath = path.slice();
|
||||
var oldName = parentPath.pop();
|
||||
if (!newName || !newName.trim() || oldName === newName) { return; }
|
||||
var parentEl = find(parentPath);
|
||||
if (typeof(parentEl[newName]) !== "undefined") {
|
||||
log(Messages.fo_existingNameError);
|
||||
return;
|
||||
}
|
||||
parentEl[newName] = element;
|
||||
parentEl[oldName] = undefined;
|
||||
delete parentEl[oldName];
|
||||
if (typeof cb === "function") { cb(); }
|
||||
return;
|
||||
}
|
||||
var parentEl = find(parentPath);
|
||||
if (typeof(parentEl[newName]) !== "undefined") {
|
||||
log(Messages.fo_existingNameError);
|
||||
|
||||
// Files
|
||||
var data = files[FILES_DATA][element];
|
||||
if (!data) { return; }
|
||||
if (!newName || newName.trim() === "") {
|
||||
data.filename = undefined;
|
||||
delete data.filename;
|
||||
if (typeof cb === "function") { cb(); }
|
||||
return;
|
||||
}
|
||||
parentEl[newName] = element;
|
||||
parentEl[oldName] = undefined;
|
||||
delete parentEl[oldName];
|
||||
var oldName = getTitle(element, 'name');
|
||||
if (oldName === newName) { return; }
|
||||
data.filename = newName;
|
||||
if (typeof cb === "function") { cb(); }
|
||||
};
|
||||
|
||||
// REPLACE
|
||||
var replaceFile = function (path, o, n) {
|
||||
var root = find(path);
|
||||
|
||||
if (isFile(root)) { return; }
|
||||
for (var e in root) {
|
||||
if (isFile(root[e])) {
|
||||
if (compareFiles(o, root[e])) {
|
||||
root[e] = n;
|
||||
}
|
||||
} else {
|
||||
var nPath = path.slice();
|
||||
nPath.push(e);
|
||||
replaceFile(nPath, o, n);
|
||||
}
|
||||
}
|
||||
};
|
||||
// Replace a href by a stronger one everywhere in the drive (except FILES_DATA)
|
||||
exp.replace = function (o, n) {
|
||||
if (!isFile(o) || !isFile(n)) { return; }
|
||||
var paths = findFile(o);
|
||||
var idO = getIdFromHref(o);
|
||||
if (!idO || !isFile(idO)) { return; }
|
||||
var data = getFileData(idO);
|
||||
if (!data) { return; }
|
||||
data.href = n;
|
||||
};
|
||||
// If all the occurences of an href are in the trash, remvoe them and add the file in root.
|
||||
// This is use with setPadTitle when we open a stronger version of a deleted pad
|
||||
exp.restoreHref = function (href) {
|
||||
var idO = getIdFromHref(href);
|
||||
|
||||
if (!idO || !isFile(idO)) { return; }
|
||||
|
||||
var paths = findFile(idO);
|
||||
|
||||
// Remove all the occurences in the trash
|
||||
// Replace all the occurences not in the trash
|
||||
// If all the occurences are in the trash or no occurence, add the pad to unsorted
|
||||
// If all the occurences are in the trash or no occurence, add the pad to root
|
||||
var allInTrash = true;
|
||||
paths.forEach(function (p) {
|
||||
if (p[0] === TRASH) {
|
||||
exp.delete(p, null, true); // 3rd parameter means skip "checkDeletedFiles"
|
||||
return;
|
||||
} else {
|
||||
allInTrash = false;
|
||||
var parentPath = p.slice();
|
||||
var key = parentPath.pop();
|
||||
var parentEl = find(parentPath);
|
||||
parentEl[key] = n;
|
||||
}
|
||||
allInTrash = false;
|
||||
});
|
||||
if (allInTrash) {
|
||||
add(n);
|
||||
add(idO);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -831,7 +764,7 @@ define([
|
||||
// * 'root', 'trash', 'unsorted' and 'filesData' exist and are objects
|
||||
// * ROOT: Folders are objects, files are href
|
||||
// * TRASH: Trash root contains only arrays, each element of the array is an object {element:.., path:..}
|
||||
// * FILES_DATA: - Data (title, cdate, adte) are stored in filesData. filesData contains only href keys linking to object with title, cdate, adate.
|
||||
// * OLD_FILES_DATA: - Data (title, cdate, adte) are stored in filesData. filesData contains only href keys linking to object with title, cdate, adate.
|
||||
// - Dates (adate, cdate) can be parsed/formatted
|
||||
// - All files in filesData should be either in 'root', 'trash' or 'unsorted'. If that's not the case, copy the fily to 'unsorted'
|
||||
// * TEMPLATE: Contains only files (href), and does not contains files that are in ROOT
|
||||
@@ -843,7 +776,7 @@ define([
|
||||
if (typeof(files[ROOT]) !== "object") { debug("ROOT was not an object"); files[ROOT] = {}; }
|
||||
var element = elem || files[ROOT];
|
||||
for (var el in element) {
|
||||
if (!isFile(element[el]) && !isFolder(element[el])) {
|
||||
if (!isFile(element[el], true) && !isFolder(element[el])) {
|
||||
debug("An element in ROOT was not a folder nor a file. ", element[el]);
|
||||
element[el] = undefined;
|
||||
delete element[el];
|
||||
@@ -857,7 +790,7 @@ define([
|
||||
// We have an old file (href) which is not in filesData: add it
|
||||
var id = Cryptpad.createRandomInteger();
|
||||
var key = Cryptpad.createChannelId();
|
||||
files[NEW_FILES_DATA][id] = {href: element[el], filename: el};
|
||||
files[FILES_DATA][id] = {href: element[el], filename: el};
|
||||
element[key] = id;
|
||||
delete element[el];
|
||||
}
|
||||
@@ -869,21 +802,25 @@ define([
|
||||
var toClean;
|
||||
var addToClean = function (obj, idx, el) {
|
||||
if (typeof(obj) !== "object") { toClean.push(idx); return; }
|
||||
if (!isFile(obj.element) && !isFolder(obj.element)) { toClean.push(idx); return; }
|
||||
if (!isFile(obj.element, true) && !isFolder(obj.element)) { toClean.push(idx); return; }
|
||||
if (!$.isArray(obj.path)) { toClean.push(idx); return; }
|
||||
if (typeof obj.element === "string") {
|
||||
// We have an old file (href) which is not in filesData: add it
|
||||
var id = Cryptpad.createRandomInteger();
|
||||
files[NEW_FILES_DATA][id] = {href: obj.element, filename: el};
|
||||
files[FILES_DATA][id] = {href: obj.element, filename: el};
|
||||
obj.element = id;
|
||||
}
|
||||
if (isFolder(obj.element)) { fixRoot(obj.element); }
|
||||
};
|
||||
for (var el in tr) {
|
||||
if (!$.isArray(tr[el])) {
|
||||
if (!Array.isArray(tr[el])) {
|
||||
debug("An element in TRASH root is not an array. ", tr[el]);
|
||||
tr[el] = undefined;
|
||||
delete tr[el];
|
||||
} else if (tr[el].length === 0) {
|
||||
debug("Empty array in TRASH root. ", tr[el]);
|
||||
tr[el] = undefined;
|
||||
delete tr[el];
|
||||
} else {
|
||||
toClean = [];
|
||||
tr[el].forEach(function (obj, idx) { addToClean(obj, idx, el); });
|
||||
@@ -900,13 +837,13 @@ define([
|
||||
var rootFiles = getFiles([ROOT]).slice();
|
||||
var toClean = [];
|
||||
us.forEach(function (el, idx) {
|
||||
if (!isFile(el) || rootFiles.indexOf(el) !== -1) {
|
||||
if (!isFile(el, true) || rootFiles.indexOf(el) !== -1) {
|
||||
toClean.push(idx);
|
||||
}
|
||||
if (typeof el === "string") {
|
||||
// We have an old file (href) which is not in filesData: add it
|
||||
var id = Cryptpad.createRandomInteger();
|
||||
files[NEW_FILES_DATA][id] = {href: el};
|
||||
files[FILES_DATA][id] = {href: el};
|
||||
us[idx] = id;
|
||||
}
|
||||
});
|
||||
@@ -915,8 +852,8 @@ define([
|
||||
});
|
||||
};
|
||||
var fixFilesData = function () {
|
||||
if (typeof files[NEW_FILES_DATA] !== "object") { debug("FILES_DATA was not an object"); files[NEW_FILES_DATA] = {}; }
|
||||
var fd = files[NEW_FILES_DATA];
|
||||
if (typeof files[FILES_DATA] !== "object") { debug("OLD_FILES_DATA was not an object"); files[FILES_DATA] = {}; }
|
||||
var fd = files[FILES_DATA];
|
||||
var rootFiles = getFiles([ROOT, TRASH, 'hrefArray']);
|
||||
var root = find([ROOT]);
|
||||
var toClean = [];
|
||||
@@ -925,12 +862,12 @@ define([
|
||||
var el = fd[id];
|
||||
if (!el || typeof(el) !== "object") {
|
||||
debug("An element in filesData was not an object.", el);
|
||||
toClean.push(el);
|
||||
toClean.push(id);
|
||||
continue;
|
||||
}
|
||||
if (!el.href) {
|
||||
debug("Removing an element in filesData with a missing href.", el);
|
||||
toClean.push(el);
|
||||
toClean.push(id);
|
||||
continue;
|
||||
}
|
||||
if (Cryptpad.isLoggedIn() && rootFiles.indexOf(id) === -1) {
|
||||
@@ -940,19 +877,16 @@ define([
|
||||
continue;
|
||||
}
|
||||
};
|
||||
toClean.forEach(function (el) {
|
||||
var idx = fd.indexOf(el);
|
||||
if (idx !== -1) {
|
||||
spliceFileData(idx);
|
||||
}
|
||||
toClean.forEach(function (id) {
|
||||
spliceFileData(id);
|
||||
});
|
||||
};
|
||||
|
||||
// Make sure unsorted doesn't exist anymore
|
||||
// Note: Unsorted only works with ld structure where pads are href
|
||||
// Note: Unsorted only works with the old structure where pads are href
|
||||
// It should be called before the migration code
|
||||
var fixUnsorted = function () {
|
||||
if (!files[UNSORTED] || !files[FILES_DATA]) { return; }
|
||||
if (!files[UNSORTED] || !files[OLD_FILES_DATA]) { return; }
|
||||
debug("UNSORTED still exists in the object, removing it...");
|
||||
var us = files[UNSORTED];
|
||||
if (us.length === 0) {
|
||||
@@ -964,38 +898,34 @@ define([
|
||||
if (typeof el !== "string") {
|
||||
return;
|
||||
}
|
||||
var data = files[FILES_DATA].filter(function (x) {
|
||||
var data = files[OLD_FILES_DATA].filter(function (x) {
|
||||
return x.href === el;
|
||||
});
|
||||
if (data.length === 0) {
|
||||
files[FILES_DATA].push({
|
||||
files[OLD_FILES_DATA].push({
|
||||
href: el
|
||||
});
|
||||
}
|
||||
return;
|
||||
/*
|
||||
TODO remove
|
||||
var name = data.length !== 0 ? data[0].title : NEW_FILE_NAME;
|
||||
var newName = getAvailableName(root, name);
|
||||
root[newName] = el;
|
||||
*/
|
||||
});
|
||||
delete files[UNSORTED];
|
||||
};
|
||||
// TODO: getRecentPads in cryptpad-common
|
||||
// TODO: in fixRoot, trash and template, make sure we have a filedata associated to the id
|
||||
// mergeDrive...
|
||||
var migrateToNewFormat = function () {
|
||||
if (!files[FILES_DATA]) { return; }
|
||||
if (!files[OLD_FILES_DATA]) { return; }
|
||||
try {
|
||||
var oldData = files[FILES_DATA].slice();
|
||||
var newData = files[NEW_FILES_DATA] = {};
|
||||
var oldData = files[OLD_FILES_DATA].slice();
|
||||
if (!files[FILES_DATA]) {
|
||||
files[FILES_DATA] = {};
|
||||
}
|
||||
var newData = files[FILES_DATA];
|
||||
//var oldFiles = oldData.map(function (o) { return o.href; });
|
||||
oldData.forEach(function (obj) {
|
||||
if (!obj || !obj.href) { return; }
|
||||
var href = obj.href;
|
||||
var id = Cryptpad.createRandomInteger();
|
||||
var paths = findFile(href);
|
||||
var data = getFileData(href);
|
||||
var data = obj;
|
||||
var key = Cryptpad.createChannelId();
|
||||
if (data) {
|
||||
newData[id] = data;
|
||||
@@ -1021,7 +951,7 @@ define([
|
||||
delete parent[okey];
|
||||
});
|
||||
});
|
||||
delete files[FILES_DATA];
|
||||
delete files[OLD_FILES_DATA];
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user