Stackedit/js/gdrive.js

196 lines
5.0 KiB
JavaScript
Raw Normal View History

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-03-27 20:19:12 +00:00
2013-03-30 11:56:17 +00:00
var gdriveDelayedFunction = undefined;
function runGdriveDelayedFunction() {
if(gdriveDelayedFunction !== undefined) {
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;
var doNothing = function() {};
2013-03-27 20:19:12 +00:00
2013-03-30 11:56:17 +00:00
var gdrive = {};
// Try to connect Gdrive by downloading client.js
function connect(callback) {
callback = callback || doNothing;
var asyncTask = {};
asyncTask.run = function() {
if(connected === true) {
asyncTask.success();
return;
}
gdriveDelayedFunction = function() {
asyncTask.success();
};
$.ajax({
url: "https://apis.google.com/js/client.js?onload=runGdriveDelayedFunction",
dataType: "script",
timeout: 5000
})
.fail(function() {
asyncTask.error();
2013-03-27 22:09:27 +00:00
});
2013-03-30 11:56:17 +00:00
};
asyncTask.onSuccess = function() {
gdriveDelayedFunction = undefined;
connected = true;
callback();
2013-03-30 11:56:17 +00:00
};
asyncTask.onError = function() {
gdriveDelayedFunction = undefined;
onOffline();
callback();
};
asyncTaskRunner.addTask(asyncTask);
}
// Try to authenticate with Oauth
function authenticate(callback, immediate) {
callback = callback || doNothing;
if(immediate === undefined) {
immediate = true;
2013-03-27 22:09:27 +00:00
}
2013-03-30 11:56:17 +00:00
connect(function() {
if(connected === false) {
callback();
return;
}
var asyncTask = {};
asyncTask.run = function() {
if(authenticated === true) {
asyncTask.success();
return;
}
2013-03-31 14:11:07 +00:00
gapi.auth.authorize({ 'client_id' : GOOGLE_CLIENT_ID, 'scope' : SCOPES,
2013-03-30 11:56:17 +00:00
'immediate' : immediate }, function(authResult) {
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
if(connected === true && immediate === true) {
authenticate(callback, false);
return;
}
callback();
};
asyncTaskRunner.addTask(asyncTask);
});
2013-03-27 20:19:12 +00:00
}
2013-03-30 11:56:17 +00:00
function upload(fileId, parentId, title, content, callback) {
callback = callback || doNothing;
authenticate(function() {
if(connected === false) {
callback();
return;
}
var fileIndex = undefined;
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 };
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
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: '
+ contentType + '\r\n' + 'Content-Transfer-Encoding: base64\r\n'
+ '\r\n' + base64Data + close_delim;
2013-03-27 20:19:12 +00:00
2013-03-30 11:56:17 +00:00
var request = gapi.client.request({
'path' : path,
'method' : method,
'params' : { 'uploadType' : 'multipart', },
'headers' : { 'Content-Type' : 'multipart/mixed; boundary="'
+ boundary + '"', }, 'body' : multipartRequestBody, });
request.execute(function(file) {
if(file.id) {
// Upload success
fileIndex = SYNC_PROVIDER_GDRIVE + file.id;
localStorage[fileIndex + ".etag"] = file.etag;
asyncTask.success();
return
}
// Upload failed, try to analyse
if(file.error.code === 401) {
showError("Google Drive is not accessible.");
}
else {
connected = false;
authenticated = false;
onOffline();
}
asyncTask.error();
});
};
asyncTask.onSuccess = function() {
callback(fileIndex);
};
asyncTask.onError = function() {
callback();
};
asyncTaskRunner.addTask(asyncTask);
});
2013-03-27 20:19:12 +00:00
}
;
gdrive.init = function() {
2013-03-30 11:56:17 +00:00
try {
var state = JSON.parse(decodeURI((/state=(.+?)(&|$)/
.exec(location.search) || [ , null ])[1]));
if (state.action == 'create') {
upload(undefined, state.folderId,
fileManager.currentFile, fileManager.content, function(
fileIndex) {
console.log(fileIndex);
});
2013-03-27 20:19:12 +00:00
}
2013-03-30 11:56:17 +00:00
} catch (e) {
}
2013-03-27 20:19:12 +00:00
};
2013-03-27 22:09:27 +00:00
gdrive.createFile = function(title, content, callback) {
2013-03-30 11:56:17 +00:00
upload(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) {
2013-03-30 11:56:17 +00:00
upload(id, undefined, title, content, callback);
2013-03-27 22:09:27 +00:00
};
2013-03-27 20:19:12 +00:00
return gdrive;
2013-03-30 11:56:17 +00:00
})(jQuery);