Stackedit/js/providers/gdriveProvider.js

284 lines
12 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"underscore",
"core",
"utils",
2013-05-30 22:16:12 +00:00
"settings",
2013-06-10 21:22:32 +00:00
"extensionMgr",
"fileMgr",
"helpers/googleHelper"
2013-05-30 22:16:12 +00:00
], function(_, core, utils, settings, extensionMgr, fileMgr, googleHelper) {
2013-04-20 00:14:20 +00:00
2013-05-29 19:55:23 +00:00
var PROVIDER_GDRIVE = "gdrive";
2013-04-20 00:14:20 +00:00
2013-05-29 19:55:23 +00:00
var gdriveProvider = {
providerId: PROVIDER_GDRIVE,
providerName: "Google Drive",
defaultPublishFormat: "template",
exportPreferencesInputIds: [
"gdrive-parentid"
]
};
function createSyncIndex(id) {
return "sync." + PROVIDER_GDRIVE + "." + id;
}
function createSyncAttributes(id, etag, content, title) {
var syncAttributes = {};
syncAttributes.provider = gdriveProvider;
syncAttributes.id = id;
syncAttributes.etag = etag;
syncAttributes.contentCRC = utils.crc32(content);
syncAttributes.titleCRC = utils.crc32(title);
syncAttributes.syncIndex = createSyncIndex(id);
return syncAttributes;
}
function importFilesFromIds(ids) {
googleHelper.downloadMetadata(ids, function(error, result) {
if(error) {
return;
}
googleHelper.downloadContent(result, function(error, result) {
if(error) {
return;
}
var fileDescList = [];
_.each(result, function(file) {
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
var syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
var fileDesc = fileMgr.createFile(file.title, file.content, syncLocations);
fileMgr.selectFile(fileDesc);
fileDescList.push(fileDesc);
});
2013-05-29 23:04:52 +00:00
if(fileDescList.length !== 0) {
extensionMgr.onSyncImportSuccess(fileDescList, gdriveProvider);
}
2013-05-29 19:55:23 +00:00
});
});
}
;
gdriveProvider.importFiles = function() {
2013-06-02 00:38:23 +00:00
googleHelper.picker(function(error, docs) {
2013-06-02 00:53:51 +00:00
if(error || docs.length === 0) {
2013-05-29 19:55:23 +00:00
return;
}
var importIds = [];
2013-06-02 00:38:23 +00:00
_.each(docs, function(doc) {
var syncIndex = createSyncIndex(doc.id);
2013-05-29 19:55:23 +00:00
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
2013-05-29 23:04:52 +00:00
extensionMgr.onError('"' + fileDesc.title + '" was already imported.');
2013-05-29 19:55:23 +00:00
return;
}
2013-06-02 00:38:23 +00:00
importIds.push(doc.id);
2013-05-29 19:55:23 +00:00
});
importFilesFromIds(importIds);
});
};
gdriveProvider.exportFile = function(event, title, content, callback) {
var parentId = utils.getInputTextValue("#input-sync-export-gdrive-parentid");
googleHelper.upload(undefined, parentId, title, content, undefined, function(error, result) {
if(error) {
callback(error);
return;
}
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
callback(undefined, syncAttributes);
});
};
gdriveProvider.exportManual = function(event, title, content, callback) {
var id = utils.getInputTextValue("#input-sync-manual-gdrive-id", event);
if(!id) {
return;
}
// Check that file is not synchronized with another an existing document
var syncIndex = createSyncIndex(id);
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
2013-05-29 23:04:52 +00:00
extensionMgr.onError('File ID is already synchronized with "' + fileDesc.title + '".');
2013-05-29 19:55:23 +00:00
callback(true);
return;
}
googleHelper.upload(id, undefined, title, content, undefined, function(error, result) {
if(error) {
callback(error);
return;
}
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
callback(undefined, syncAttributes);
});
};
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);
});
};
gdriveProvider.syncDown = function(callback) {
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) {
var syncIndex = createSyncIndex(change.fileId);
var syncAttributes = fileMgr.getSyncAttributes(syncIndex);
if(syncAttributes === undefined) {
return;
}
// Store syncAttributes to avoid 2 times searching
change.syncAttributes = syncAttributes;
// Delete
if(change.deleted === true) {
interestingChanges.push(change);
return;
}
// Modify
if(syncAttributes.etag != change.file.etag) {
interestingChanges.push(change);
}
});
googleHelper.downloadContent(interestingChanges, function(error, changes) {
if(error) {
callback(error);
return;
}
_.each(changes, function(change) {
var syncAttributes = change.syncAttributes;
var syncIndex = syncAttributes.syncIndex;
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
// No file corresponding (file may have been deleted
// locally)
if(fileDesc === undefined) {
return;
}
var localTitle = fileDesc.title;
// File deleted
if(change.deleted === true) {
extensionMgr.onError('"' + localTitle + '" has been removed from Google Drive.');
2013-06-16 10:47:35 +00:00
fileDesc.removeSyncLocation(syncAttributes);
extensionMgr.onSyncRemoved(fileDesc, syncAttributes);
2013-05-29 19:55:23 +00:00
return;
}
var localTitleChanged = syncAttributes.titleCRC != utils.crc32(localTitle);
var localContent = fileDesc.content;
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
var file = change.file;
2013-05-25 18:13:59 +00:00
var remoteTitleCRC = utils.crc32(file.title);
var remoteTitleChanged = syncAttributes.titleCRC != remoteTitleCRC;
var fileTitleChanged = localTitle != file.title;
2013-05-25 18:13:59 +00:00
var remoteContentCRC = utils.crc32(file.content);
var remoteContentChanged = syncAttributes.contentCRC != remoteContentCRC;
2013-05-29 19:55:23 +00:00
var fileContentChanged = localContent != file.content;
// Conflict detection
if((fileTitleChanged === true && localTitleChanged === true && remoteTitleChanged === true) || (fileContentChanged === true && localContentChanged === true && remoteContentChanged === true)) {
fileMgr.createFile(localTitle + " (backup)", localContent);
extensionMgr.onMessage('Conflict detected on "' + localTitle + '". A backup has been created locally.');
}
// If file title changed
if(fileTitleChanged && remoteTitleChanged === true) {
fileDesc.title = file.title;
2013-06-16 10:47:35 +00:00
extensionMgr.onTitleChanged(fileDesc);
2013-05-29 19:55:23 +00:00
extensionMgr.onMessage('"' + localTitle + '" has been renamed to "' + file.title + '" on Google Drive.');
}
// If file content changed
if(fileContentChanged && remoteContentChanged === true) {
fileDesc.content = file.content;
2013-06-16 10:47:35 +00:00
extensionMgr.onContentChanged(fileDesc);
2013-05-29 19:55:23 +00:00
extensionMgr.onMessage('"' + file.title + '" has been updated from Google Drive.');
2013-06-19 20:33:46 +00:00
if(fileMgr.currentFile === fileDesc) {
2013-05-29 19:55:23 +00:00
fileMgr.selectFile(); // Refresh editor
}
}
// Update syncAttributes
syncAttributes.etag = file.etag;
syncAttributes.contentCRC = remoteContentCRC;
syncAttributes.titleCRC = remoteTitleCRC;
utils.storeAttributes(syncAttributes);
});
localStorage[PROVIDER_GDRIVE + ".lastChangeId"] = newChangeId;
callback();
});
});
};
gdriveProvider.publish = function(publishAttributes, title, content, callback) {
googleHelper.upload(publishAttributes.id, undefined, publishAttributes.fileName || title, content, undefined, function(error, result) {
if(error) {
callback(error);
return;
}
publishAttributes.id = result.id;
callback();
});
};
gdriveProvider.newPublishAttributes = function(event) {
var publishAttributes = {};
publishAttributes.id = utils.getInputTextValue("#input-publish-gdrive-fileid");
publishAttributes.fileName = utils.getInputTextValue("#input-publish-gdrive-filename");
if(event.isPropagationStopped()) {
return undefined;
}
return publishAttributes;
};
2013-04-20 00:14:20 +00:00
2013-05-29 19:55:23 +00:00
core.onReady(function() {
2013-06-03 22:19:52 +00:00
var state = utils.retrieveIgnoreError(PROVIDER_GDRIVE + ".state");
2013-05-29 19:55:23 +00:00
if(state === undefined) {
return;
}
localStorage.removeItem(PROVIDER_GDRIVE + ".state");
if(state.action == "create") {
2013-05-30 22:16:12 +00:00
googleHelper.upload(undefined, state.folderId, GDRIVE_DEFAULT_FILE_TITLE, settings.defaultContent, undefined, function(error, file) {
2013-05-29 19:55:23 +00:00
if(error) {
return;
}
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
var syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
var fileDesc = fileMgr.createFile(file.title, file.content, syncAttributes);
fileMgr.selectFile(fileDesc);
extensionMgr.onMessage('"' + file.title + '" created successfully on Google Drive.');
});
}
else if(state.action == "open") {
var importIds = [];
_.each(state.ids, function(id) {
var syncIndex = createSyncIndex(id);
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
fileMgr.selectFile(fileDesc);
}
else {
importIds.push(id);
}
});
importFilesFromIds(importIds);
}
});
2013-04-20 00:14:20 +00:00
2013-05-29 19:55:23 +00:00
return gdriveProvider;
2013-04-20 00:14:20 +00:00
});