cryptpad/www/common/mergeDrive.js

93 lines
3.9 KiB
JavaScript
Raw Normal View History

define([
'/common/cryptget.js',
'/common/userObject.js',
2017-11-13 15:32:40 +00:00
'/common/common-hash.js',
2017-11-30 09:33:09 +00:00
'/common/common-realtime.js',
], function (Crypt, FO, Hash, Realtime) {
var exp = {};
2017-09-22 17:35:06 +00:00
exp.anonDriveIntoUser = function (proxyData, fsHash, cb) {
// Make sure we have an FS_hash and we don't use it, otherwise just stop the migration and cb
2017-11-30 09:33:09 +00:00
if (!fsHash || !proxyData.loggedIn) {
2017-07-04 12:51:35 +00:00
if (typeof(cb) === "function") { return void cb(); }
}
// Get the content of FS_hash and then merge the objects, remove the migration key and cb
var todo = function (err, doc) {
if (err) { console.error("Cannot migrate recent pads", err); return; }
var parsed;
if (!doc) {
if (typeof(cb) === "function") { cb(); }
return;
}
try { parsed = JSON.parse(doc); } catch (e) {
if (typeof(cb) === "function") { cb(); }
console.error("Cannot parsed recent pads", e);
return;
}
if (parsed) {
2017-09-22 17:35:06 +00:00
var proxy = proxyData.proxy;
var oldFo = FO.init(parsed.drive, {
readOnly: false,
2018-07-10 16:23:16 +00:00
loggedIn: true,
2018-07-10 13:10:07 +00:00
outer: true
});
var onMigrated = function () {
oldFo.fixFiles(true);
2018-07-10 16:23:16 +00:00
var manager = proxyData.manager;
var oldFiles = oldFo.getFiles([oldFo.FILES_DATA]);
2017-06-09 10:29:19 +00:00
oldFiles.forEach(function (id) {
var data = oldFo.getFileData(id);
2018-07-10 16:23:16 +00:00
var channel = data.channel;
2019-10-07 16:30:46 +00:00
var datas = manager.findChannel(channel);
2018-07-10 16:23:16 +00:00
// Do not migrate a pad if we already have it, it would create a duplicate
// in the drive
if (datas.length !== 0) {
// We want to merge a read-only pad: it can't be stronger than what
// we already have so abort
if (!data.href) { return; }
// We want to merge an edit pad: check if we have the same channel
// but read-only and upgrade it in that case
datas.forEach(function (pad) {
2019-10-07 16:30:46 +00:00
if (pad.data && !pad.data.href) {
pad.userObject.setHref(channel, null, data.href);
}
2018-07-10 16:23:16 +00:00
});
return;
}
// Here it means we have a new href, so we should add it to the drive
2017-06-09 10:29:19 +00:00
if (data) {
2018-07-10 16:23:16 +00:00
manager.addPad(null, data, function (err) {
if (err) {
return void console.error("Cannot import file:", data, err);
}
2017-06-09 10:29:19 +00:00
});
}
});
if (!proxy.FS_hashes || !Array.isArray(proxy.FS_hashes)) {
proxy.FS_hashes = [];
}
if (proxy.FS_hashes.indexOf(fsHash) === -1) {
proxy.FS_hashes.push(fsHash);
}
2017-11-30 09:33:09 +00:00
if (typeof(cb) === "function") {
Realtime.whenRealtimeSyncs(proxyData.realtime, cb);
}
2017-06-09 10:29:19 +00:00
};
if (oldFo && typeof(oldFo.migrate) === 'function') {
oldFo.migrate(onMigrated);
} else {
console.log('oldFo.migrate is not a function');
onMigrated();
}
return;
}
if (typeof(cb) === "function") { cb(); }
};
2017-09-22 17:35:06 +00:00
Crypt.get(fsHash, todo);
};
return exp;
});