Stackedit/js/dropbox-provider.js

238 lines
7.6 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"underscore",
"utils",
"extension-manager",
"file-manager",
"dropbox-helper"
], function(_, utils, extensionMgr, fileMgr, dropboxHelper) {
2013-04-16 15:02:24 +00:00
2013-05-04 00:05:58 +00:00
var PROVIDER_DROPBOX = "dropbox";
2013-04-16 15:02:24 +00:00
var dropboxProvider = {
providerId: PROVIDER_DROPBOX,
providerName: "Dropbox",
2013-05-26 22:59:03 +00:00
defaultPublishFormat: "template"
2013-04-16 15:02:24 +00:00
};
2013-04-20 00:14:20 +00:00
function checkPath(path) {
if(path === undefined) {
return undefined;
}
if(!path.match(/^[^\\<>:"\|?\*]+$/)) {
2013-05-27 19:45:33 +00:00
extensionMgr.onError('"' + path + '" contains invalid characters.');
2013-04-20 00:14:20 +00:00
return undefined;
}
if(path.indexOf("/") !== 0) {
return "/" + path;
}
return path;
}
2013-05-26 22:59:03 +00:00
function createSyncIndex(path) {
return "sync." + PROVIDER_DROPBOX + "." + encodeURIComponent(path.toLowerCase());
}
2013-04-20 00:14:20 +00:00
function createSyncAttributes(path, versionTag, content) {
var syncAttributes = {};
2013-05-26 22:59:03 +00:00
syncAttributes.provider = dropboxProvider;
2013-04-20 00:14:20 +00:00
syncAttributes.path = path;
syncAttributes.version = versionTag;
2013-05-25 18:13:59 +00:00
syncAttributes.contentCRC = utils.crc32(content);
2013-05-26 22:59:03 +00:00
syncAttributes.syncIndex = createSyncIndex(path);
2013-05-04 00:05:58 +00:00
return syncAttributes;
}
2013-04-20 00:14:20 +00:00
function importFilesFromPaths(paths) {
dropboxHelper.downloadMetadata(paths, function(error, result) {
if(error) {
return;
}
dropboxHelper.downloadContent(result, function(error, result) {
if(error) {
return;
}
2013-05-26 22:59:03 +00:00
var fileDescList = [];
2013-04-20 00:14:20 +00:00
_.each(result, function(file) {
2013-05-04 00:05:58 +00:00
var syncAttributes = createSyncAttributes(file.path, file.versionTag, file.content);
2013-05-26 22:59:03 +00:00
localStorage[syncAttributes.syncIndex] = utils.serializeAttributes(syncAttributes);
var syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
2013-05-27 19:45:33 +00:00
var fileDesc = fileMgr.createFile(file.name, file.content, syncLocations);
fileMgr.selectFile(fileDesc);
2013-05-26 22:59:03 +00:00
fileDescList.push(fileDesc);
2013-04-20 00:14:20 +00:00
});
2013-05-27 19:45:33 +00:00
extensionMgr.onSyncImportSuccess(fileDescList, dropboxProvider);
2013-04-20 00:14:20 +00:00
});
});
}
dropboxProvider.importFiles = function() {
dropboxHelper.picker(function(error, paths) {
2013-04-28 22:33:17 +00:00
if(error || paths.length === 0) {
2013-04-20 00:14:20 +00:00
return;
}
var importPaths = [];
_.each(paths, function(path) {
2013-05-04 00:05:58 +00:00
var syncIndex = createSyncIndex(path);
2013-05-27 19:45:33 +00:00
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
2013-05-26 22:59:03 +00:00
if(fileDesc !== undefined) {
2013-05-27 19:45:33 +00:00
extensionMgr.onError('"' + fileDesc.title + '" was already imported');
2013-04-20 00:14:20 +00:00
return;
}
importPaths.push(path);
});
2013-04-21 00:07:27 +00:00
importFilesFromPaths(importPaths);
2013-04-20 00:14:20 +00:00
});
};
function exportFileToPath(path, title, content, callback) {
2013-04-21 00:07:27 +00:00
path = checkPath(path);
2013-04-20 00:14:20 +00:00
if(path === undefined) {
callback(true);
return;
}
// Check that file is not synchronized with an other one
2013-05-04 00:05:58 +00:00
var syncIndex = createSyncIndex(path);
2013-05-27 19:45:33 +00:00
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
2013-05-26 22:59:03 +00:00
if(fileDesc !== undefined) {
var existingTitle = fileDesc.title;
2013-05-27 19:45:33 +00:00
extensionMgr.onError('File path is already synchronized with "' + existingTitle + '"');
2013-04-20 00:14:20 +00:00
callback(true);
return;
}
dropboxHelper.upload(path, content, function(error, result) {
if (error) {
callback(error);
return;
}
2013-05-04 00:05:58 +00:00
var syncAttributes = createSyncAttributes(result.path, result.versionTag, content);
2013-05-26 22:59:03 +00:00
localStorage[syncAttributes.syncIndex] = utils.serializeAttributes(syncAttributes);
2013-05-26 23:38:13 +00:00
callback(undefined, syncAttributes);
2013-04-20 00:14:20 +00:00
});
}
dropboxProvider.exportFile = function(event, title, content, callback) {
2013-05-25 18:13:59 +00:00
var path = utils.getInputTextValue("#input-sync-export-dropbox-path", event);
2013-04-21 00:07:27 +00:00
exportFileToPath(path, title, content, callback);
2013-04-20 00:14:20 +00:00
};
dropboxProvider.exportManual = function(event, title, content, callback) {
2013-05-25 18:13:59 +00:00
var path = utils.getInputTextValue("#input-sync-manual-dropbox-path", event);
2013-04-21 00:07:27 +00:00
exportFileToPath(path, title, content, callback);
2013-04-20 00:14:20 +00:00
};
2013-04-20 17:40:05 +00:00
dropboxProvider.syncUp = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) {
var syncContentCRC = syncAttributes.contentCRC;
// Skip if CRC has not changed
if(uploadContentCRC == syncContentCRC) {
callback(undefined, false);
return;
}
dropboxHelper.upload(syncAttributes.path, uploadContent, function(error, result) {
if(error) {
callback(error, true);
return;
}
syncAttributes.version = result.versionTag;
syncAttributes.contentCRC = uploadContentCRC;
callback(undefined, true);
});
};
2013-04-21 00:07:27 +00:00
dropboxProvider.syncDown = function(callback) {
var lastChangeId = localStorage[PROVIDER_DROPBOX + ".lastChangeId"];
2013-04-20 17:40:05 +00:00
dropboxHelper.checkChanges(lastChangeId, function(error, changes, newChangeId) {
if (error) {
callback(error);
return;
}
var interestingChanges = [];
_.each(changes, function(change) {
2013-05-04 00:05:58 +00:00
var syncIndex = createSyncIndex(change.path);
2013-05-27 19:45:33 +00:00
var syncAttributes = fileMgr.getSyncAttributes(syncIndex);
2013-05-26 22:59:03 +00:00
if(syncAttributes === undefined) {
2013-04-20 17:40:05 +00:00
return;
}
2013-05-26 22:59:03 +00:00
// Store syncAttributes to avoid 2 times searching
change.syncAttributes = syncAttributes;
2013-04-20 17:40:05 +00:00
// Delete
if(change.wasRemoved === true) {
interestingChanges.push(change);
return;
}
// Modify
if(syncAttributes.version != change.stat.versionTag) {
interestingChanges.push(change);
}
});
2013-04-21 00:07:27 +00:00
dropboxHelper.downloadContent(interestingChanges, function(error, changes) {
2013-04-20 17:40:05 +00:00
if (error) {
callback(error);
return;
}
_.each(changes, function(change) {
2013-05-26 22:59:03 +00:00
var syncAttributes = change.syncAttributes;
var syncIndex = syncAttributes.syncIndex;
2013-05-27 19:45:33 +00:00
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
2013-04-20 17:40:05 +00:00
// No file corresponding (file may have been deleted locally)
2013-05-26 22:59:03 +00:00
if(fileDesc === undefined) {
2013-04-20 17:40:05 +00:00
return;
}
2013-05-26 22:59:03 +00:00
var localTitle = fileDesc.title;
2013-04-20 17:40:05 +00:00
// File deleted
if (change.wasRemoved === true) {
2013-05-27 19:45:33 +00:00
extensionMgr.onError('"' + localTitle + '" has been removed from Dropbox.');
fileMgr.removeSync(syncAttributes);
2013-04-20 17:40:05 +00:00
return;
}
2013-05-26 23:38:13 +00:00
var localContent = localStorage[fileDesc.fileIndex + ".content"];
2013-05-25 18:13:59 +00:00
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
2013-04-20 17:40:05 +00:00
var file = change.stat;
2013-05-25 18:13:59 +00:00
var remoteContentCRC = utils.crc32(file.content);
var remoteContentChanged = syncAttributes.contentCRC != remoteContentCRC;
2013-04-20 17:40:05 +00:00
var fileContentChanged = localContent != file.content;
// Conflict detection
if (fileContentChanged === true && localContentChanged === true && remoteContentChanged === true) {
2013-05-27 19:45:33 +00:00
var backupFileDesc = fileMgr.createFile(localTitle + " (backup)", localContent);
extensionMgr.onTitleChanged(backupFileDesc);
extensionMgr.onMessage('Conflict detected on "' + localTitle + '". A backup has been created locally.');
2013-04-20 17:40:05 +00:00
}
// If file content changed
if(fileContentChanged && remoteContentChanged === true) {
2013-05-26 23:38:13 +00:00
localStorage[fileDesc.fileIndex + ".content"] = file.content;
2013-05-27 19:45:33 +00:00
extensionMgr.onMessage('"' + localTitle + '" has been updated from Dropbox.');
if(fileMgr.isCurrentFile(fileDesc)) {
fileMgr.selectFile(); // Refresh editor
2013-04-20 17:40:05 +00:00
}
}
// Update syncAttributes
syncAttributes.version = file.versionTag;
syncAttributes.contentCRC = remoteContentCRC;
2013-05-26 22:59:03 +00:00
localStorage[syncIndex] = utils.serializeAttributes(syncAttributes);
2013-04-20 17:40:05 +00:00
});
localStorage[PROVIDER_DROPBOX + ".lastChangeId"] = newChangeId;
callback();
});
});
2013-04-21 00:07:27 +00:00
};
2013-04-20 17:40:05 +00:00
2013-04-16 15:02:24 +00:00
dropboxProvider.publish = function(publishAttributes, title, content, callback) {
2013-04-20 00:14:20 +00:00
var path = checkPath(publishAttributes.path);
2013-04-16 15:02:24 +00:00
if(path === undefined) {
callback(true);
return;
}
dropboxHelper.upload(path, content, callback);
};
dropboxProvider.newPublishAttributes = function(event) {
var publishAttributes = {};
2013-05-25 18:13:59 +00:00
publishAttributes.path = utils.getInputTextValue("#input-publish-dropbox-path", event);
2013-04-16 15:02:24 +00:00
if(event.isPropagationStopped()) {
return undefined;
}
return publishAttributes;
};
return dropboxProvider;
});