Stackedit/js/gdrive-provider.js

300 lines
10 KiB
JavaScript
Raw Normal View History

2013-04-21 00:07:27 +00:00
define(["jquery", "core", "google-helper", "underscore"], function($, core, googleHelper) {
2013-04-20 00:14:20 +00:00
2013-05-04 00:05:58 +00:00
var PROVIDER_GDRIVE = "gdrive";
2013-04-20 00:14:20 +00:00
var gdriveProvider = {
providerId: PROVIDER_GDRIVE,
2013-04-21 00:07:27 +00:00
providerName: "Google Drive",
2013-04-20 17:40:05 +00:00
defaultPublishFormat: "template",
exportPreferencesInputIds: ["gdrive-parentid"],
2013-04-20 17:40:05 +00:00
useSync: false
2013-04-20 00:14:20 +00:00
};
function createSyncAttributes(id, etag, content, title) {
var syncAttributes = {};
syncAttributes.provider = PROVIDER_GDRIVE;
syncAttributes.id = id;
syncAttributes.etag = etag;
syncAttributes.contentCRC = core.crc32(content);
syncAttributes.titleCRC = core.crc32(title);
2013-05-04 00:05:58 +00:00
return syncAttributes;
}
function createSyncIndex(id) {
return "sync." + PROVIDER_GDRIVE + "." + id;
2013-04-20 00:14:20 +00:00
}
function importFilesFromIds(ids) {
googleHelper.downloadMetadata(ids, function(error, result) {
if(error) {
return;
}
googleHelper.downloadContent(result, function(error, result) {
if(error) {
return;
}
2013-05-04 00:05:58 +00:00
var titleList = [];
2013-04-20 00:14:20 +00:00
_.each(result, function(file) {
2013-05-04 00:05:58 +00:00
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = JSON.stringify(syncAttributes);
2013-04-21 00:07:27 +00:00
var fileIndex = core.fileManager.createFile(file.title, file.content, [syncIndex]);
core.fileManager.selectFile(fileIndex);
2013-05-04 00:05:58 +00:00
titleList.push('"' + file.title + '"');
2013-04-20 00:14:20 +00:00
});
if(titleList.length === 0) {
return;
}
2013-05-04 00:05:58 +00:00
core.showMessage(titleList.join(", ") + ' imported successfully from Google Drive.');
2013-04-20 00:14:20 +00:00
});
});
};
gdriveProvider.importFiles = function() {
googleHelper.picker(function(error, ids) {
2013-04-28 22:33:17 +00:00
if(error || ids.length === 0) {
2013-04-20 00:14:20 +00:00
return;
}
var importIds = [];
_.each(ids, function(id) {
2013-05-04 00:05:58 +00:00
var syncIndex = createSyncIndex(id);
2013-04-21 00:07:27 +00:00
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
2013-04-20 00:14:20 +00:00
if(fileIndex !== undefined) {
var title = localStorage[fileIndex + ".title"];
core.showError('"' + title + '" was already imported');
return;
}
importIds.push(id);
});
importFilesFromIds(importIds);
});
};
gdriveProvider.exportFile = function(event, title, content, callback) {
var parentId = core.getInputValue($("#input-sync-export-gdrive-parentid"));
googleHelper.upload(undefined, parentId, title, content, undefined, function(error, result) {
2013-04-20 00:14:20 +00:00
if (error) {
callback(error);
return;
}
2013-05-04 00:05:58 +00:00
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = JSON.stringify(syncAttributes);
2013-04-20 00:14:20 +00:00
callback(undefined, syncIndex);
});
};
gdriveProvider.exportManual = function(event, title, content, callback) {
var id = core.getInputValue($("#input-sync-manual-gdrive-id"), event);
if(!id) {
return;
}
// Check that file is not synchronized with an other one
2013-05-04 00:05:58 +00:00
var syncIndex = createSyncIndex(id);
2013-04-21 00:07:27 +00:00
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
2013-04-20 00:14:20 +00:00
if(fileIndex !== undefined) {
var existingTitle = localStorage[fileIndex + ".title"];
core.showError('File ID is already synchronized with "' + existingTitle + '"');
callback(true);
return;
}
2013-04-20 17:40:05 +00:00
googleHelper.upload(id, undefined, title, content, undefined, function(error, result) {
2013-04-20 00:14:20 +00:00
if (error) {
callback(error);
return;
}
2013-05-04 00:05:58 +00:00
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = JSON.stringify(syncAttributes);
2013-04-20 00:14:20 +00:00
callback(undefined, syncIndex);
});
};
2013-04-20 17:40:05 +00:00
gdriveProvider.syncUp = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) {
var syncContentCRC = syncAttributes.contentCRC;
var syncTitleCRC = syncAttributes.titleCRC;
// Skip if CRC has not changed
if(uploadContentCRC == syncContentCRC && uploadTitleCRC == syncTitleCRC) {
callback(undefined, false);
return;
}
googleHelper.upload(syncAttributes.id, undefined, uploadTitle, uploadContent, syncAttributes.etag, function(error, result) {
if(error) {
callback(error, true);
return;
}
syncAttributes.etag = result.etag;
syncAttributes.contentCRC = uploadContentCRC;
syncAttributes.titleCRC = uploadTitleCRC;
callback(undefined, true);
});
};
2013-04-21 00:07:27 +00:00
gdriveProvider.syncDown = function(callback) {
2013-04-20 17:40:05 +00:00
if (gdriveProvider.useSync === false) {
callback();
return;
}
var lastChangeId = parseInt(localStorage[PROVIDER_GDRIVE + ".lastChangeId"]);
googleHelper.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.fileId);
2013-04-20 17:40:05 +00:00
var serializedAttributes = localStorage[syncIndex];
if(serializedAttributes === undefined) {
return;
}
// Store syncIndex to avoid 2 times formating
change.syncIndex = syncIndex;
// Delete
if(change.deleted === true) {
interestingChanges.push(change);
return;
}
// Modify
var syncAttributes = JSON.parse(serializedAttributes);
if(syncAttributes.etag != change.file.etag) {
interestingChanges.push(change);
// Store syncAttributes to avoid 2 times parsing
change.syncAttributes = syncAttributes;
}
});
2013-04-21 00:07:27 +00:00
googleHelper.downloadContent(interestingChanges, function(error, changes) {
2013-04-20 17:40:05 +00:00
if (error) {
callback(error);
return;
}
var updateFileTitles = false;
_.each(changes, function(change) {
var syncIndex = change.syncIndex;
2013-04-21 00:07:27 +00:00
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
2013-04-20 17:40:05 +00:00
// No file corresponding (file may have been deleted locally)
if(fileIndex === undefined) {
2013-04-21 00:07:27 +00:00
core.fileManager.removeSync(syncIndex);
2013-04-20 17:40:05 +00:00
return;
}
var localTitle = localStorage[fileIndex + ".title"];
// File deleted
if (change.deleted === true) {
2013-04-21 00:07:27 +00:00
core.fileManager.removeSync(syncIndex);
2013-04-20 17:40:05 +00:00
updateFileTitles = true;
core.showMessage('"' + localTitle + '" has been removed from Google Drive.');
return;
}
var syncAttributes = change.syncAttributes;
var localTitleChanged = syncAttributes.titleCRC != core.crc32(localTitle);
var localContent = localStorage[fileIndex + ".content"];
var localContentChanged = syncAttributes.contentCRC != core.crc32(localContent);
var file = change.file;
var remoteTitleCRC = core.crc32(file.title);
var remoteTitleChanged = syncAttributes.titleCRC != remoteTitleCRC;
var fileTitleChanged = localTitle != file.title;
var remoteContentCRC = core.crc32(file.content);
var remoteContentChanged = syncAttributes.contentCRC != remoteContentCRC;
2013-04-20 17:40:05 +00:00
var fileContentChanged = localContent != file.content;
// Conflict detection
if ((fileTitleChanged === true && localTitleChanged === true && remoteTitleChanged === true)
|| (fileContentChanged === true && localContentChanged === true && remoteContentChanged === true)) {
2013-04-21 00:07:27 +00:00
core.fileManager.createFile(localTitle + " (backup)", localContent);
2013-04-20 17:40:05 +00:00
updateFileTitles = true;
core.showMessage('Conflict detected on "' + localTitle + '". A backup has been created locally.');
}
// If file title changed
if(fileTitleChanged && remoteTitleChanged === true) {
2013-04-20 17:40:05 +00:00
localStorage[fileIndex + ".title"] = file.title;
updateFileTitles = true;
core.showMessage('"' + localTitle + '" has been renamed to "' + file.title + '" on Google Drive.');
}
// If file content changed
if(fileContentChanged && remoteContentChanged === true) {
2013-04-20 17:40:05 +00:00
localStorage[fileIndex + ".content"] = file.content;
core.showMessage('"' + file.title + '" has been updated from Google Drive.');
2013-04-21 00:07:27 +00:00
if(core.fileManager.isCurrentFileIndex(fileIndex)) {
2013-04-20 17:40:05 +00:00
updateFileTitles = false; // Done by next function
2013-04-21 00:07:27 +00:00
core.fileManager.selectFile(); // Refresh editor
2013-04-20 17:40:05 +00:00
}
}
// Update syncAttributes
syncAttributes.etag = file.etag;
syncAttributes.contentCRC = remoteContentCRC;
syncAttributes.titleCRC = remoteTitleCRC;
2013-04-20 17:40:05 +00:00
localStorage[syncIndex] = JSON.stringify(syncAttributes);
});
if(updateFileTitles) {
2013-04-21 00:07:27 +00:00
core.fileManager.updateFileTitles();
2013-04-20 17:40:05 +00:00
}
localStorage[PROVIDER_GDRIVE + ".lastChangeId"] = newChangeId;
callback();
});
});
2013-04-21 00:07:27 +00:00
};
2013-04-20 17:40:05 +00:00
2013-04-20 00:14:20 +00:00
gdriveProvider.publish = function(publishAttributes, title, content, callback) {
2013-04-21 00:07:27 +00:00
googleHelper.upload(
2013-05-02 00:02:57 +00:00
publishAttributes.id,
2013-04-21 00:07:27 +00:00
undefined,
publishAttributes.fileName || title,
content,
undefined,
function(error, result) {
if(error) {
callback(error);
return;
}
2013-05-02 00:02:57 +00:00
publishAttributes.id = result.id;
2013-04-21 00:07:27 +00:00
callback();
}
);
2013-04-20 00:14:20 +00:00
};
gdriveProvider.newPublishAttributes = function(event) {
var publishAttributes = {};
2013-05-02 00:02:57 +00:00
publishAttributes.id = $("#input-publish-gdrive-fileid").val() || undefined;
2013-04-21 00:07:27 +00:00
publishAttributes.fileName = $("#input-publish-gdrive-filename").val() || undefined;
2013-04-20 00:14:20 +00:00
if(event.isPropagationStopped()) {
return undefined;
}
return publishAttributes;
};
2013-04-22 01:04:12 +00:00
core.onReady(function() {
2013-04-21 00:07:27 +00:00
var state = localStorage[PROVIDER_GDRIVE + ".state"];
2013-04-20 00:14:20 +00:00
if(state === undefined) {
return;
}
2013-04-21 00:07:27 +00:00
localStorage.removeItem(PROVIDER_GDRIVE + ".state");
2013-04-20 00:14:20 +00:00
state = JSON.parse(state);
if (state.action == "create") {
googleHelper.upload(undefined, state.folderId, GDRIVE_DEFAULT_FILE_TITLE,
2013-04-20 17:40:05 +00:00
"", undefined, function(error, file) {
2013-04-20 00:14:20 +00:00
if(error) {
return;
}
var syncIndex = createSyncAttributes(file.id, file.etag, file.content, file.title);
2013-04-22 00:39:33 +00:00
var fileIndex = core.fileManager.createFile(file.title, file.content, [syncIndex]);
core.fileManager.selectFile(fileIndex);
2013-04-20 00:14:20 +00:00
core.showMessage('"' + file.title + '" created successfully on Google Drive.');
});
}
else if (state.action == "open") {
var importIds = [];
_.each(state.ids, function(id) {
2013-05-04 00:05:58 +00:00
var syncIndex = createSyncIndex(id);
2013-04-22 00:39:33 +00:00
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
2013-04-20 00:14:20 +00:00
if(fileIndex !== undefined) {
2013-04-22 00:39:33 +00:00
core.fileManager.selectFile(fileIndex);
2013-04-20 00:14:20 +00:00
}
else {
importIds.push(id);
}
});
importFilesFromIds(importIds);
}
2013-04-21 00:07:27 +00:00
});
2013-04-20 00:14:20 +00:00
return gdriveProvider;
});