2013-05-27 19:45:33 +00:00
|
|
|
define([
|
|
|
|
"underscore",
|
|
|
|
"utils",
|
2013-11-05 23:03:38 +00:00
|
|
|
"storage",
|
2014-03-28 00:49:49 +00:00
|
|
|
"settings",
|
2013-06-22 23:48:57 +00:00
|
|
|
"classes/Provider",
|
2013-07-30 08:46:36 +00:00
|
|
|
"eventMgr",
|
2013-06-10 21:22:32 +00:00
|
|
|
"fileMgr",
|
|
|
|
"helpers/dropboxHelper"
|
2014-03-28 00:49:49 +00:00
|
|
|
], function(_, utils, storage, settings, Provider, eventMgr, fileMgr, dropboxHelper) {
|
2013-04-20 00:14:20 +00:00
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
var PROVIDER_DROPBOX = "dropbox";
|
2013-04-20 00:14:20 +00:00
|
|
|
|
2013-06-22 23:48:57 +00:00
|
|
|
var dropboxProvider = new Provider(PROVIDER_DROPBOX, "Dropbox");
|
|
|
|
dropboxProvider.defaultPublishFormat = "template";
|
2013-05-29 19:55:23 +00:00
|
|
|
|
|
|
|
function checkPath(path) {
|
|
|
|
if(path === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if(!path.match(/^[^\\<>:"\|?\*]+$/)) {
|
2013-07-30 08:46:36 +00:00
|
|
|
eventMgr.onError('"' + path + '" contains invalid characters.');
|
2013-05-29 19:55:23 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if(path.indexOf("/") !== 0) {
|
|
|
|
return "/" + path;
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSyncIndex(path) {
|
|
|
|
return "sync." + PROVIDER_DROPBOX + "." + encodeURIComponent(path.toLowerCase());
|
|
|
|
}
|
|
|
|
|
2014-04-07 23:19:47 +00:00
|
|
|
var merge = settings.conflictMode == 'merge';
|
|
|
|
function createSyncAttributes(path, versionTag, content, discussionListJSON) {
|
|
|
|
discussionListJSON = discussionListJSON || '{}';
|
2013-05-29 19:55:23 +00:00
|
|
|
var syncAttributes = {};
|
|
|
|
syncAttributes.provider = dropboxProvider;
|
|
|
|
syncAttributes.path = path;
|
|
|
|
syncAttributes.version = versionTag;
|
|
|
|
syncAttributes.contentCRC = utils.crc32(content);
|
2014-04-07 23:19:47 +00:00
|
|
|
syncAttributes.discussionListCRC = utils.crc32(discussionListJSON);
|
2013-05-29 19:55:23 +00:00
|
|
|
syncAttributes.syncIndex = createSyncIndex(path);
|
2014-04-07 23:19:47 +00:00
|
|
|
if(merge === true) {
|
|
|
|
// Need to store the whole content for merge
|
|
|
|
syncAttributes.content = content;
|
|
|
|
syncAttributes.discussionList = discussionListJSON;
|
|
|
|
}
|
2013-05-29 19:55:23 +00:00
|
|
|
return syncAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
function importFilesFromPaths(paths) {
|
|
|
|
dropboxHelper.downloadMetadata(paths, function(error, result) {
|
|
|
|
if(error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dropboxHelper.downloadContent(result, function(error, result) {
|
|
|
|
if(error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var fileDescList = [];
|
|
|
|
_.each(result, function(file) {
|
2014-04-10 23:22:30 +00:00
|
|
|
var parsedContent = dropboxProvider.parseContent(file.content);
|
2014-04-07 23:19:47 +00:00
|
|
|
var syncAttributes = createSyncAttributes(file.path, file.versionTag, parsedContent.content, parsedContent.discussionListJSON);
|
2013-05-29 19:55:23 +00:00
|
|
|
var syncLocations = {};
|
|
|
|
syncLocations[syncAttributes.syncIndex] = syncAttributes;
|
2014-04-07 23:19:47 +00:00
|
|
|
var fileDesc = fileMgr.createFile(file.name, parsedContent.content, parsedContent.discussionListJSON, syncLocations);
|
2013-05-29 19:55:23 +00:00
|
|
|
fileMgr.selectFile(fileDesc);
|
|
|
|
fileDescList.push(fileDesc);
|
|
|
|
});
|
2013-05-29 23:04:52 +00:00
|
|
|
if(fileDescList.length !== 0) {
|
2013-07-30 08:46:36 +00:00
|
|
|
eventMgr.onSyncImportSuccess(fileDescList, dropboxProvider);
|
2013-05-29 23:04:52 +00:00
|
|
|
}
|
2013-05-29 19:55:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dropboxProvider.importFiles = function() {
|
|
|
|
dropboxHelper.picker(function(error, paths) {
|
|
|
|
if(error || paths.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var importPaths = [];
|
|
|
|
_.each(paths, function(path) {
|
|
|
|
var syncIndex = createSyncIndex(path);
|
|
|
|
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
|
|
|
|
if(fileDesc !== undefined) {
|
2014-08-13 23:44:33 +00:00
|
|
|
return eventMgr.onError('"' + fileDesc.title + '" is already in your local documents.');
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
|
|
|
importPaths.push(path);
|
|
|
|
});
|
|
|
|
importFilesFromPaths(importPaths);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-04-07 23:19:47 +00:00
|
|
|
dropboxProvider.exportFile = function(event, title, content, discussionListJSON, callback) {
|
2013-08-14 23:44:51 +00:00
|
|
|
var path = utils.getInputTextValue("#input-sync-export-dropbox-path", event);
|
2013-05-29 19:55:23 +00:00
|
|
|
path = checkPath(path);
|
|
|
|
if(path === undefined) {
|
2014-03-28 00:49:49 +00:00
|
|
|
return callback(true);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
2013-08-14 23:44:51 +00:00
|
|
|
// Check that file is not synchronized with another one
|
2013-05-29 19:55:23 +00:00
|
|
|
var syncIndex = createSyncIndex(path);
|
|
|
|
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
|
|
|
|
if(fileDesc !== undefined) {
|
|
|
|
var existingTitle = fileDesc.title;
|
2013-07-30 08:46:36 +00:00
|
|
|
eventMgr.onError('File path is already synchronized with "' + existingTitle + '".');
|
2014-03-28 00:49:49 +00:00
|
|
|
return callback(true);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
2014-04-21 01:12:57 +00:00
|
|
|
var data = dropboxProvider.serializeContent(content, discussionListJSON);
|
|
|
|
dropboxHelper.upload(path, data, function(error, result) {
|
2013-05-29 19:55:23 +00:00
|
|
|
if(error) {
|
2014-03-28 00:49:49 +00:00
|
|
|
return callback(error);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
2014-04-07 23:19:47 +00:00
|
|
|
var syncAttributes = createSyncAttributes(result.path, result.versionTag, content, discussionListJSON);
|
2013-05-29 19:55:23 +00:00
|
|
|
callback(undefined, syncAttributes);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-03-28 00:49:49 +00:00
|
|
|
dropboxProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) {
|
|
|
|
if(
|
|
|
|
(syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed
|
|
|
|
(syncAttributes.discussionListCRC == discussionListCRC) // Discussion list CRC hasn't changed
|
|
|
|
) {
|
|
|
|
return callback(undefined, false);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
2014-03-28 00:49:49 +00:00
|
|
|
var uploadedContent = dropboxProvider.serializeContent(content, discussionList);
|
|
|
|
dropboxHelper.upload(syncAttributes.path, uploadedContent, function(error, result) {
|
2013-05-29 19:55:23 +00:00
|
|
|
if(error) {
|
2014-03-28 00:49:49 +00:00
|
|
|
return callback(error, true);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
|
|
|
syncAttributes.version = result.versionTag;
|
2014-03-28 00:49:49 +00:00
|
|
|
if(merge === true) {
|
|
|
|
// Need to store the whole content for merge
|
|
|
|
syncAttributes.content = content;
|
|
|
|
syncAttributes.discussionList = discussionList;
|
|
|
|
}
|
|
|
|
syncAttributes.contentCRC = contentCRC;
|
2014-03-31 00:10:28 +00:00
|
|
|
syncAttributes.titleCRC = titleCRC; // Not synchronized but has to be there for syncMerge
|
2014-03-28 00:49:49 +00:00
|
|
|
syncAttributes.discussionListCRC = discussionListCRC;
|
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
callback(undefined, true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
dropboxProvider.syncDown = function(callback) {
|
2013-11-05 23:03:38 +00:00
|
|
|
var lastChangeId = storage[PROVIDER_DROPBOX + ".lastChangeId"];
|
2013-05-29 19:55:23 +00:00
|
|
|
dropboxHelper.checkChanges(lastChangeId, function(error, changes, newChangeId) {
|
|
|
|
if(error) {
|
2014-03-28 00:49:49 +00:00
|
|
|
return callback(error);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
|
|
|
var interestingChanges = [];
|
|
|
|
_.each(changes, function(change) {
|
|
|
|
var syncIndex = createSyncIndex(change.path);
|
2014-04-10 23:22:30 +00:00
|
|
|
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
|
|
|
|
var syncAttributes = fileDesc && fileDesc.syncLocations[syncIndex];
|
|
|
|
if(!syncAttributes) {
|
2013-05-29 19:55:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-04-10 23:22:30 +00:00
|
|
|
// Store fileDesc and syncAttributes references to avoid 2 times search
|
|
|
|
change.fileDesc = fileDesc;
|
2013-05-29 19:55:23 +00:00
|
|
|
change.syncAttributes = syncAttributes;
|
|
|
|
// Delete
|
|
|
|
if(change.wasRemoved === true) {
|
|
|
|
interestingChanges.push(change);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Modify
|
|
|
|
if(syncAttributes.version != change.stat.versionTag) {
|
|
|
|
interestingChanges.push(change);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dropboxHelper.downloadContent(interestingChanges, function(error, changes) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-21 15:36:22 +00:00
|
|
|
function mergeChange() {
|
2014-03-30 01:44:51 +00:00
|
|
|
if(changes.length === 0) {
|
|
|
|
storage[PROVIDER_DROPBOX + ".lastChangeId"] = newChangeId;
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
var change = changes.pop();
|
2014-04-10 23:22:30 +00:00
|
|
|
var fileDesc = change.fileDesc;
|
2013-05-29 19:55:23 +00:00
|
|
|
var syncAttributes = change.syncAttributes;
|
|
|
|
// File deleted
|
|
|
|
if(change.wasRemoved === true) {
|
2014-03-30 01:44:51 +00:00
|
|
|
eventMgr.onError('"' + fileDesc.title + '" has been removed from Dropbox.');
|
2013-06-16 10:47:35 +00:00
|
|
|
fileDesc.removeSyncLocation(syncAttributes);
|
2014-03-28 00:49:49 +00:00
|
|
|
return eventMgr.onSyncRemoved(fileDesc, syncAttributes);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
|
|
|
var file = change.stat;
|
2014-04-10 23:22:30 +00:00
|
|
|
var parsedContent = dropboxProvider.parseContent(file.content);
|
2014-03-30 01:44:51 +00:00
|
|
|
var remoteContent = parsedContent.content;
|
2014-04-07 23:19:47 +00:00
|
|
|
var remoteDiscussionListJSON = parsedContent.discussionListJSON;
|
2014-04-10 23:22:30 +00:00
|
|
|
var remoteDiscussionList = parsedContent.discussionList;
|
2014-04-07 23:19:47 +00:00
|
|
|
var remoteCRC = dropboxProvider.syncMerge(fileDesc, syncAttributes, remoteContent, fileDesc.title, remoteDiscussionList, remoteDiscussionListJSON);
|
2013-05-29 19:55:23 +00:00
|
|
|
// Update syncAttributes
|
|
|
|
syncAttributes.version = file.versionTag;
|
2014-03-30 01:44:51 +00:00
|
|
|
if(merge === true) {
|
|
|
|
// Need to store the whole content for merge
|
|
|
|
syncAttributes.content = remoteContent;
|
|
|
|
syncAttributes.discussionList = remoteDiscussionList;
|
|
|
|
}
|
|
|
|
syncAttributes.contentCRC = remoteCRC.contentCRC;
|
|
|
|
syncAttributes.discussionListCRC = remoteCRC.discussionListCRC;
|
2013-05-29 19:55:23 +00:00
|
|
|
utils.storeAttributes(syncAttributes);
|
2014-04-21 15:36:22 +00:00
|
|
|
setTimeout(mergeChange, 5);
|
2014-03-30 01:44:51 +00:00
|
|
|
}
|
2014-04-21 15:36:22 +00:00
|
|
|
setTimeout(mergeChange, 5);
|
2013-05-29 19:55:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-10-06 14:34:40 +00:00
|
|
|
dropboxProvider.publish = function(publishAttributes, frontMatter, title, content, callback) {
|
2013-05-29 19:55:23 +00:00
|
|
|
var path = checkPath(publishAttributes.path);
|
|
|
|
if(path === undefined) {
|
2014-03-28 00:49:49 +00:00
|
|
|
return callback(true);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
|
|
|
dropboxHelper.upload(path, content, callback);
|
|
|
|
};
|
2013-04-16 15:02:24 +00:00
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
dropboxProvider.newPublishAttributes = function(event) {
|
|
|
|
var publishAttributes = {};
|
|
|
|
publishAttributes.path = utils.getInputTextValue("#input-publish-dropbox-path", event);
|
|
|
|
if(event.isPropagationStopped()) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return publishAttributes;
|
|
|
|
};
|
2013-04-16 15:02:24 +00:00
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
return dropboxProvider;
|
2014-03-23 02:33:41 +00:00
|
|
|
});
|