2013-03-31 15:33:28 +00:00
|
|
|
var GOOGLE_CLIENT_ID = '241271498917-jpto9lls9fqnem1e4h6ppds9uob8rpvu.apps.googleusercontent.com';
|
2013-03-30 11:56:17 +00:00
|
|
|
var SCOPES = [ 'https://www.googleapis.com/auth/drive.install',
|
|
|
|
'https://www.googleapis.com/auth/drive.file' ];
|
2013-04-01 01:06:52 +00:00
|
|
|
var AUTH_POPUP_TIMEOUT = 90000;
|
2013-04-01 23:12:28 +00:00
|
|
|
var DEFAULT_GDRIVE_FILE_TITLE = "New Markdown document";
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
var gdriveDelayedFunction = undefined;
|
|
|
|
function runGdriveDelayedFunction() {
|
2013-04-01 01:06:52 +00:00
|
|
|
if (gdriveDelayedFunction !== undefined) {
|
2013-03-30 11:56:17 +00:00
|
|
|
gdriveDelayedFunction();
|
|
|
|
}
|
|
|
|
}
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
var gdrive = (function($) {
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
var connected = false;
|
|
|
|
var authenticated = false;
|
2013-04-01 01:06:52 +00:00
|
|
|
var doNothing = function() {
|
|
|
|
};
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
var gdrive = {};
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
// Try to connect Gdrive by downloading client.js
|
|
|
|
function connect(callback) {
|
|
|
|
callback = callback || doNothing;
|
|
|
|
var asyncTask = {};
|
|
|
|
asyncTask.run = function() {
|
2013-04-01 01:06:52 +00:00
|
|
|
if (connected === true) {
|
2013-03-30 11:56:17 +00:00
|
|
|
asyncTask.success();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gdriveDelayedFunction = function() {
|
|
|
|
asyncTask.success();
|
|
|
|
};
|
2013-04-01 16:46:48 +00:00
|
|
|
$.ajax({
|
|
|
|
url : "https://apis.google.com/js/client.js?onload=runGdriveDelayedFunction",
|
|
|
|
dataType : "script", timeout : AJAX_TIMEOUT
|
|
|
|
}).fail(function() {
|
|
|
|
asyncTask.error();
|
|
|
|
});
|
2013-03-30 11:56:17 +00:00
|
|
|
};
|
|
|
|
asyncTask.onSuccess = function() {
|
|
|
|
gdriveDelayedFunction = undefined;
|
|
|
|
connected = true;
|
2013-03-27 22:11:29 +00:00
|
|
|
callback();
|
2013-03-30 11:56:17 +00:00
|
|
|
};
|
|
|
|
asyncTask.onError = function() {
|
|
|
|
gdriveDelayedFunction = undefined;
|
|
|
|
onOffline();
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
asyncTaskRunner.addTask(asyncTask);
|
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
// Try to authenticate with Oauth
|
|
|
|
function authenticate(callback, immediate) {
|
|
|
|
callback = callback || doNothing;
|
2013-04-01 01:06:52 +00:00
|
|
|
if (immediate === undefined) {
|
2013-03-30 11:56:17 +00:00
|
|
|
immediate = true;
|
2013-03-27 22:09:27 +00:00
|
|
|
}
|
2013-03-30 11:56:17 +00:00
|
|
|
connect(function() {
|
2013-04-01 01:06:52 +00:00
|
|
|
if (connected === false) {
|
2013-03-30 11:56:17 +00:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
var asyncTask = {};
|
2013-03-31 15:33:28 +00:00
|
|
|
// If not immediate we add time for user to enter his credentials
|
2013-04-01 01:06:52 +00:00
|
|
|
if (immediate === false) {
|
|
|
|
asyncTask.timeout = AUTH_POPUP_TIMEOUT;
|
2013-03-31 15:33:28 +00:00
|
|
|
}
|
2013-03-30 11:56:17 +00:00
|
|
|
asyncTask.run = function() {
|
2013-04-01 01:06:52 +00:00
|
|
|
if (authenticated === true) {
|
2013-03-30 11:56:17 +00:00
|
|
|
asyncTask.success();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
if (immediate === false) {
|
2013-04-01 16:46:48 +00:00
|
|
|
showMessage("Please make sure the Google authorization popup is not blocked by your browser.");
|
2013-04-01 01:06:52 +00:00
|
|
|
}
|
|
|
|
gapi.auth.authorize({ 'client_id' : GOOGLE_CLIENT_ID,
|
|
|
|
'scope' : SCOPES, 'immediate' : immediate }, function(
|
|
|
|
authResult) {
|
2013-03-30 11:56:17 +00:00
|
|
|
if (!authResult || authResult.error) {
|
|
|
|
asyncTask.error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gapi.client.load('drive', 'v2', function() {
|
|
|
|
authenticated = true;
|
|
|
|
asyncTask.success();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
asyncTask.onSuccess = function() {
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
asyncTask.onError = function() {
|
|
|
|
// If immediate did not work retry without immediate flag
|
2013-04-01 01:06:52 +00:00
|
|
|
if (connected === true && immediate === true) {
|
2013-03-30 11:56:17 +00:00
|
|
|
authenticate(callback, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
asyncTaskRunner.addTask(asyncTask);
|
|
|
|
});
|
2013-03-27 20:19:12 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 16:46:48 +00:00
|
|
|
function handleError(error, asyncTask, callback) {
|
2013-04-01 01:06:52 +00:00
|
|
|
var errorMsg = undefined;
|
2013-04-01 16:46:48 +00:00
|
|
|
if (error) {
|
2013-04-01 01:06:52 +00:00
|
|
|
// Try to analyze the error
|
|
|
|
if (error.code >= 500 && error.code < 600) {
|
|
|
|
errorMsg = "Google Drive is not accessible.";
|
|
|
|
// Retry as described in Google's best practices
|
|
|
|
asyncTask.retry();
|
|
|
|
return;
|
|
|
|
} else if (error.code === 401) {
|
|
|
|
authenticated = false;
|
|
|
|
errorMsg = "Access to Google Drive is not authorized.";
|
2013-04-01 16:46:48 +00:00
|
|
|
} else if (error.code <= 0) {
|
2013-04-01 01:06:52 +00:00
|
|
|
connected = false;
|
|
|
|
authenticated = false;
|
|
|
|
onOffline();
|
|
|
|
} else {
|
|
|
|
errorMsg = "Google Drive error (" + error.code + ": "
|
|
|
|
+ error.message + ").";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
asyncTask.onError = function() {
|
|
|
|
if (errorMsg !== undefined) {
|
|
|
|
showError(errorMsg);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
asyncTask.error();
|
|
|
|
}
|
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
function upload(fileId, parentId, title, content, callback) {
|
|
|
|
callback = callback || doNothing;
|
|
|
|
authenticate(function() {
|
2013-04-01 01:06:52 +00:00
|
|
|
if (connected === false) {
|
2013-03-30 11:56:17 +00:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-04-01 23:12:28 +00:00
|
|
|
var fileSyncIndex = undefined;
|
2013-03-30 11:56:17 +00:00
|
|
|
var asyncTask = {};
|
|
|
|
asyncTask.run = function() {
|
|
|
|
var boundary = '-------314159265358979323846';
|
|
|
|
var delimiter = "\r\n--" + boundary + "\r\n";
|
|
|
|
var close_delim = "\r\n--" + boundary + "--";
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
var contentType = 'text/x-markdown';
|
|
|
|
var metadata = { title : title, mimeType : contentType };
|
2013-04-01 01:06:52 +00:00
|
|
|
if (parentId !== undefined) {
|
2013-03-30 11:56:17 +00:00
|
|
|
// Specify the directory
|
2013-04-01 01:06:52 +00:00
|
|
|
metadata.parents = [ { kind : 'drive#fileLink',
|
|
|
|
id : parentId } ];
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
|
|
|
var path = '/upload/drive/v2/files';
|
|
|
|
var method = 'POST';
|
2013-04-01 01:06:52 +00:00
|
|
|
if (fileId !== undefined) {
|
2013-03-30 11:56:17 +00:00
|
|
|
// If it's an update
|
|
|
|
path += "/" + fileId;
|
|
|
|
method = 'PUT';
|
|
|
|
}
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-03-31 01:00:03 +00:00
|
|
|
var base64Data = base64.encode(content);
|
2013-03-30 11:56:17 +00:00
|
|
|
var multipartRequestBody = delimiter
|
|
|
|
+ 'Content-Type: application/json\r\n\r\n'
|
|
|
|
+ JSON.stringify(metadata) + delimiter + 'Content-Type: '
|
2013-04-01 01:06:52 +00:00
|
|
|
+ contentType + '\r\n'
|
|
|
|
+ 'Content-Transfer-Encoding: base64\r\n' + '\r\n'
|
|
|
|
+ base64Data + close_delim;
|
|
|
|
|
|
|
|
var request = gapi.client
|
|
|
|
.request({
|
|
|
|
'path' : path,
|
|
|
|
'method' : method,
|
|
|
|
'params' : { 'uploadType' : 'multipart', },
|
|
|
|
'headers' : { 'Content-Type' : 'multipart/mixed; boundary="'
|
|
|
|
+ boundary + '"', }, 'body' : multipartRequestBody, });
|
|
|
|
request.execute(function(response) {
|
|
|
|
if (response && response.id) {
|
2013-03-30 11:56:17 +00:00
|
|
|
// Upload success
|
2013-04-01 23:12:28 +00:00
|
|
|
fileSyncIndex = SYNC_PROVIDER_GDRIVE + response.id;
|
|
|
|
localStorage[fileSyncIndex + ".etag"] = response.etag;
|
2013-03-30 11:56:17 +00:00
|
|
|
asyncTask.success();
|
2013-04-01 01:06:52 +00:00
|
|
|
return;
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
2013-04-01 16:46:48 +00:00
|
|
|
var error = response.error;
|
2013-04-01 01:06:52 +00:00
|
|
|
// If file has been removed from Google Drive
|
2013-04-01 16:46:48 +00:00
|
|
|
if(error !== undefined && fileId !== undefined && error.code === 404) {
|
2013-04-01 01:06:52 +00:00
|
|
|
showMessage('"' + title + '" has been removed from Google Drive.');
|
|
|
|
fileManager.removeSync(SYNC_PROVIDER_GDRIVE + fileId);
|
|
|
|
fileManager.updateFileTitles();
|
|
|
|
// Avoid error analyzed by handleError
|
2013-04-01 16:46:48 +00:00
|
|
|
error = undefined;
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
// Handle error
|
2013-04-01 16:46:48 +00:00
|
|
|
handleError(error, asyncTask, callback);
|
2013-03-30 11:56:17 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
asyncTask.onSuccess = function() {
|
2013-04-01 23:12:28 +00:00
|
|
|
callback(fileSyncIndex);
|
2013-03-30 11:56:17 +00:00
|
|
|
};
|
2013-04-01 01:06:52 +00:00
|
|
|
asyncTaskRunner.addTask(asyncTask);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
gdrive.checkUpdates = function(lastChangeId, callback) {
|
|
|
|
callback = callback || doNothing;
|
|
|
|
authenticate(function() {
|
|
|
|
if (connected === false) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var changes = [];
|
|
|
|
var newChangeId = lastChangeId || 0;
|
|
|
|
function retrievePageOfChanges(request) {
|
|
|
|
var nextPageToken = undefined;
|
|
|
|
var asyncTask = {};
|
|
|
|
asyncTask.run = function() {
|
2013-04-01 16:46:48 +00:00
|
|
|
request.execute(function(response) {
|
|
|
|
if (response && response.largestChangeId) {
|
|
|
|
// Retrieve success
|
|
|
|
newChangeId = response.largestChangeId;
|
|
|
|
nextPageToken = response.nextPageToken;
|
|
|
|
if (response.items !== undefined) {
|
|
|
|
for ( var i = 0; i < response.items.length; i++) {
|
|
|
|
var item = response.items[i];
|
|
|
|
var etag = localStorage[SYNC_PROVIDER_GDRIVE
|
|
|
|
+ item.fileId + ".etag"];
|
|
|
|
if (etag
|
|
|
|
&& (item.deleted === true || item.file.etag != etag)) {
|
|
|
|
changes.push(item);
|
2013-04-01 01:06:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-01 16:46:48 +00:00
|
|
|
asyncTask.success();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Handle error
|
|
|
|
handleError(response.error, asyncTask, callback);
|
|
|
|
});
|
2013-04-01 01:06:52 +00:00
|
|
|
};
|
|
|
|
asyncTask.onSuccess = function() {
|
|
|
|
if (nextPageToken !== undefined) {
|
|
|
|
request = gapi.client.drive.changes
|
|
|
|
.list({ 'pageToken' : nextPageToken });
|
|
|
|
retrievePageOfChanges(request);
|
|
|
|
} else {
|
|
|
|
callback(changes, newChangeId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
asyncTaskRunner.addTask(asyncTask);
|
|
|
|
}
|
|
|
|
var initialRequest = gapi.client.drive.changes
|
|
|
|
.list({ 'startChangeId' : newChangeId + 1 });
|
|
|
|
retrievePageOfChanges(initialRequest);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-04-01 23:12:28 +00:00
|
|
|
gdrive.downloadMetadata = function(ids, callback, result) {
|
|
|
|
callback = callback || doNothing;
|
|
|
|
result = result || [];
|
|
|
|
if(ids.length === 0) {
|
|
|
|
callback(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticate(function() {
|
|
|
|
if (connected === false) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var id = ids.pop();
|
|
|
|
var asyncTask = {};
|
|
|
|
asyncTask.run = function() {
|
|
|
|
var accessToken = gapi.auth.getToken().access_token;
|
|
|
|
$.ajax({
|
|
|
|
url : "https://www.googleapis.com/drive/v2/files/" + id,
|
|
|
|
headers : { "Authorization" : "Bearer " + accessToken },
|
|
|
|
dataType : "json",
|
|
|
|
timeout : AJAX_TIMEOUT
|
|
|
|
}).done(function(data, textStatus, jqXHR) {
|
|
|
|
result.push(data);
|
|
|
|
asyncTask.success();
|
|
|
|
}).fail(function(jqXHR) {
|
|
|
|
var error = {
|
|
|
|
code: jqXHR.status,
|
|
|
|
message: jqXHR.statusText
|
|
|
|
};
|
|
|
|
// Handle error
|
|
|
|
handleError(error, asyncTask, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
asyncTask.onSuccess = function() {
|
|
|
|
gdrive.downloadMetadata(ids, callback, result);
|
|
|
|
};
|
|
|
|
asyncTaskRunner.addTask(asyncTask);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-04-01 01:06:52 +00:00
|
|
|
gdrive.downloadContent = function(objects, callback, result) {
|
|
|
|
callback = callback || doNothing;
|
|
|
|
result = result || [];
|
|
|
|
if(objects.length === 0) {
|
|
|
|
callback(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var object = objects.pop();
|
|
|
|
result.push(object);
|
|
|
|
var file = undefined;
|
|
|
|
// object may be a file
|
|
|
|
if(object.kind == "drive#file") {
|
|
|
|
file = object;
|
|
|
|
}
|
|
|
|
// object may be a change
|
|
|
|
else if(object.kind == "drive#change") {
|
|
|
|
file = object.file;
|
|
|
|
}
|
|
|
|
if(file === undefined) {
|
|
|
|
this.downloadContent(objects, callback, result);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-01 23:12:28 +00:00
|
|
|
|
2013-04-01 01:06:52 +00:00
|
|
|
authenticate(function() {
|
|
|
|
if (connected === false) {
|
2013-03-30 11:56:17 +00:00
|
|
|
callback();
|
2013-04-01 01:06:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-01 23:12:28 +00:00
|
|
|
|
2013-04-01 01:06:52 +00:00
|
|
|
var asyncTask = {};
|
|
|
|
asyncTask.run = function() {
|
|
|
|
var accessToken = gapi.auth.getToken().access_token;
|
2013-04-01 16:46:48 +00:00
|
|
|
$.ajax({
|
|
|
|
url : file.downloadUrl,
|
2013-04-01 23:12:28 +00:00
|
|
|
headers : { "Authorization" : "Bearer " + accessToken },
|
|
|
|
dataType : "text",
|
2013-04-01 16:46:48 +00:00
|
|
|
timeout : AJAX_TIMEOUT
|
|
|
|
}).done(function(data, textStatus, jqXHR) {
|
|
|
|
file.content = data;
|
|
|
|
asyncTask.success();
|
|
|
|
}).fail(function(jqXHR) {
|
|
|
|
var error = {
|
|
|
|
code: jqXHR.status,
|
|
|
|
message: jqXHR.statusText
|
|
|
|
};
|
|
|
|
// Handle error
|
|
|
|
handleError(error, asyncTask, callback);
|
|
|
|
});
|
2013-04-01 01:06:52 +00:00
|
|
|
};
|
|
|
|
asyncTask.onSuccess = function() {
|
|
|
|
gdrive.downloadContent(objects, callback, result);
|
2013-03-30 11:56:17 +00:00
|
|
|
};
|
|
|
|
asyncTaskRunner.addTask(asyncTask);
|
2013-04-01 01:06:52 +00:00
|
|
|
});
|
|
|
|
};
|
2013-04-01 23:12:28 +00:00
|
|
|
|
2013-04-01 01:06:52 +00:00
|
|
|
gdrive.createFile = function(title, content, callback) {
|
|
|
|
upload(undefined, undefined, title, content, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
gdrive.updateFile = function(id, title, content, callback) {
|
|
|
|
upload(id, undefined, title, content, callback);
|
|
|
|
};
|
2013-03-27 20:19:12 +00:00
|
|
|
|
|
|
|
gdrive.init = function() {
|
2013-04-01 23:12:28 +00:00
|
|
|
var state = localStorage["sync.gdrive.state"];
|
|
|
|
if(state === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
localStorage.removeItem("sync.gdrive.state");
|
|
|
|
state = JSON.parse(state);
|
|
|
|
if (state.action == "create") {
|
|
|
|
upload(undefined, state.folderId, DEFAULT_GDRIVE_FILE_TITLE,
|
|
|
|
"", function(fileSyncIndex) {
|
|
|
|
if(fileSyncIndex === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var fileIndex = fileManager.createFile(DEFAULT_GDRIVE_FILE_TITLE, "", [fileSyncIndex]);
|
|
|
|
fileManager.selectFile(fileIndex);
|
|
|
|
showMessage('"' + DEFAULT_GDRIVE_FILE_TITLE + '" created successfully on Google Drive.');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (state.action == "open") {
|
|
|
|
var importIds = [];
|
|
|
|
for(var i=0; i<state.ids.length; i++) {
|
|
|
|
var id = state.ids[i];
|
|
|
|
var fileSyncIndex = SYNC_PROVIDER_GDRIVE + id;
|
|
|
|
var fileIndex = fileManager.getFileIndexFromSync(fileSyncIndex);
|
|
|
|
if(fileIndex !== undefined) {
|
|
|
|
fileManager.selectFile(fileIndex);
|
|
|
|
} else {
|
|
|
|
importIds.push(id);
|
|
|
|
}
|
2013-03-27 20:19:12 +00:00
|
|
|
}
|
2013-04-01 23:12:28 +00:00
|
|
|
gdrive.downloadMetadata(importIds, function(result) {
|
|
|
|
if(result === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gdrive.downloadContent(result, function(result) {
|
|
|
|
if(result === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(var i=0; i<result.length; i++) {
|
|
|
|
var file = result[i];
|
|
|
|
fileSyncIndex = SYNC_PROVIDER_GDRIVE + file.id;
|
|
|
|
localStorage[fileSyncIndex + ".etag"] = file.etag;
|
|
|
|
var fileIndex = fileManager.createFile(file.title, file.content, [fileSyncIndex]);
|
|
|
|
fileManager.selectFile(fileIndex);
|
|
|
|
showMessage('"' + file.title + '" imported successfully from Google Drive.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
2013-03-27 20:19:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return gdrive;
|
2013-03-30 11:56:17 +00:00
|
|
|
})(jQuery);
|