Stackedit/js/gdrive.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-03-27 20:19:12 +00:00
var gdrive = (function() {
var CLIENT_ID = '241271498917-jpto9lls9fqnem1e4h6ppds9uob8rpvu.apps.googleusercontent.com';
var SCOPES = [ 'https://www.googleapis.com/auth/drive.install',
'https://www.googleapis.com/auth/drive.file' ];
var driveEnabled = false;
var gdrive = {};
function askAuth(immediate, callback) {
2013-03-27 22:09:27 +00:00
if (!driveEnabled) {
gapi.auth.authorize({ 'client_id' : CLIENT_ID, 'scope' : SCOPES,
'immediate' : immediate }, function(authResult) {
if (authResult && !authResult.error) {
// $("#drive-link").hide();
gapi.client.load('drive', 'v2', function() {
driveEnabled = true;
callback();
});
}
});
} else {
callback();
2013-03-27 22:09:27 +00:00
}
2013-03-27 20:19:12 +00:00
}
2013-03-27 22:09:27 +00:00
function uploadFile(fileId, parentId, title, content, callback) {
2013-03-28 22:00:11 +00:00
setWorkingIndicator(FLAG_GDRIVE_UPLOAD);
2013-03-27 20:19:12 +00:00
var boundary = '-------314159265358979323846';
var delimiter = "\r\n--" + boundary + "\r\n";
var close_delim = "\r\n--" + boundary + "--";
var contentType = 'text/x-markdown';
var metadata = { title : title, mimeType : contentType };
2013-03-27 22:09:27 +00:00
if (parentId) {
// Specify the directory
metadata.parents = [ { kind : 'drive#fileLink', id : parentId } ];
}
var path = '/upload/drive/v2/files';
var method = 'POST';
if (fileId) {
// If it's an update
2013-03-28 23:06:03 +00:00
path += "/" + fileId;
2013-03-27 22:09:27 +00:00
method = 'PUT';
2013-03-27 20:19:12 +00:00
}
var base64Data = btoa(content);
var multipartRequestBody = delimiter
+ 'Content-Type: application/json\r\n\r\n'
+ JSON.stringify(metadata) + delimiter + 'Content-Type: '
+ contentType + '\r\n' + 'Content-Transfer-Encoding: base64\r\n'
+ '\r\n' + base64Data + close_delim;
var request = gapi.client.request({
2013-03-27 22:09:27 +00:00
'path' : path,
'method' : method,
2013-03-27 20:19:12 +00:00
'params' : { 'uploadType' : 'multipart', },
'headers' : { 'Content-Type' : 'multipart/mixed; boundary="'
+ boundary + '"', }, 'body' : multipartRequestBody, });
request.execute(function(file) {
2013-03-28 22:00:11 +00:00
unsetWorkingIndicator(FLAG_GDRIVE_UPLOAD);
2013-03-28 23:06:03 +00:00
var fileSyncIndex = undefined;
if(file.id) {
fileSyncIndex = SYNC_PROVIDER_GDRIVE + file.id;
localStorage[fileSyncIndex + ".etag"] = file.etag;
}
2013-03-27 20:19:12 +00:00
if (callback) {
2013-03-28 22:00:11 +00:00
callback(fileSyncIndex);
2013-03-27 20:19:12 +00:00
}
});
}
;
gdrive.init = function() {
askAuth(true, function() {
try {
var state = JSON.parse(decodeURI((/state=(.+?)(&|$)/
.exec(location.search) || [ , null ])[1]));
if (state.action == 'create') {
2013-03-27 22:09:27 +00:00
uploadFile(undefined, state.folderId,
fileManager.currentFile, fileManager.content, function(
file) {
2013-03-27 20:19:12 +00:00
console.log(file);
});
}
} catch (e) {
}
});
};
2013-03-27 22:09:27 +00:00
gdrive.createFile = function(title, content, callback) {
askAuth(false, function() {
uploadFile(undefined, undefined, title, content, callback);
});
2013-03-27 20:19:12 +00:00
};
2013-03-27 22:09:27 +00:00
gdrive.updateFile = function(id, title, content, callback) {
askAuth(false, function() {
uploadFile(id, undefined, title, content, callback);
});
};
2013-03-27 20:19:12 +00:00
return gdrive;
})();