Google Drive support
This commit is contained in:
parent
9470d0f2b0
commit
a0e5b7d730
72
index.html
72
index.html
@ -13,78 +13,8 @@
|
||||
<script type="text/javascript" src="js/Markdown.Editor.js"></script>
|
||||
<script type="text/javascript" src="js/main.js"></script>
|
||||
<script type="text/javascript">
|
||||
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 driveUpload = false;
|
||||
|
||||
/**
|
||||
* Called when the client library is loaded to start the auth flow.
|
||||
*/
|
||||
function handleClientLoad() {
|
||||
window.setTimeout(checkAuth, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has authorized the application.
|
||||
*/
|
||||
function checkAuth() {
|
||||
gapi.auth.authorize({ 'client_id' : CLIENT_ID, 'scope' : SCOPES,
|
||||
'immediate' : true }, handleAuthResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when authorization server replies.
|
||||
*
|
||||
* @param {Object} authResult Authorization result.
|
||||
*/
|
||||
function handleAuthResult(authResult) {
|
||||
if (authResult && !authResult.error) {
|
||||
$("#drive-link").hide();
|
||||
gapi.client.load('drive', 'v2', function() {
|
||||
driveUpload = true;
|
||||
});
|
||||
} else {
|
||||
$("#drive-link").click(function() {
|
||||
gapi.auth.authorize({ 'client_id' : CLIENT_ID,
|
||||
'scope' : SCOPES, 'immediate' : false }, handleAuthResult);
|
||||
});
|
||||
}
|
||||
//alert(authResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the file upload.
|
||||
*
|
||||
* @param {Object} evt Arguments from the file selector.
|
||||
*/
|
||||
function uploadFile(evt) {
|
||||
gapi.client.load('drive', 'v2', function() {
|
||||
var file = evt.target.files[0];
|
||||
insertFile(file);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert new file.
|
||||
*
|
||||
* @param {File} fileData File object to read data from.
|
||||
* @param {Function} callback Function to call when the request is complete.
|
||||
*/
|
||||
function insertFile(fileData, callback) {
|
||||
if(driveUpload) {
|
||||
var request = gapi.client.request({
|
||||
'path' : '/upload/drive/v2/files',
|
||||
'method' : 'POST',
|
||||
'params' : { 'uploadType' : 'media' },
|
||||
'headers' : { 'Content-Type' : 'text/plain' },
|
||||
'body' : fileData });
|
||||
if (!callback) {
|
||||
callback = function(file) {
|
||||
console.log(file)
|
||||
};
|
||||
}
|
||||
request.execute(callback);
|
||||
}
|
||||
window.setTimeout(gdrive.init, 1);
|
||||
}
|
||||
</script>
|
||||
<script type="text/javascript"
|
||||
|
116
js/main.js
116
js/main.js
@ -1,18 +1,8 @@
|
||||
(function($) {
|
||||
function showError(msg) {
|
||||
alert(msg);
|
||||
}
|
||||
|
||||
$(function() {
|
||||
var converter = Markdown.getSanitizingConverter();
|
||||
var editor = new Markdown.Editor(converter);
|
||||
editor.run();
|
||||
$(window).resize(resize);
|
||||
resize();
|
||||
|
||||
if (typeof (Storage) !== "undefined") {
|
||||
fileManager.init();
|
||||
} else {
|
||||
showError("Web storage is not available");
|
||||
}
|
||||
});
|
||||
var fileManager = (function($) {
|
||||
|
||||
var fileManager = {};
|
||||
|
||||
@ -49,17 +39,105 @@
|
||||
this.fileSystem[this.currentFile] = this.content;
|
||||
localStorage.fileSystem = JSON.stringify(this.fileSystem);
|
||||
localStorage.currentFile = this.currentFile;
|
||||
insertFile(this.content);
|
||||
//insertFile(this.currentFile, this.content);
|
||||
};
|
||||
|
||||
return fileManager;
|
||||
})(jQuery);
|
||||
|
||||
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 = {};
|
||||
|
||||
gdrive.init = function() {
|
||||
function start() {
|
||||
driveEnabled = true;
|
||||
try {
|
||||
var state = JSON.parse(decodeURI((/state=(.+?)(&|$)/
|
||||
.exec(location.search) || [ , null ])[1]));
|
||||
if (state.action == 'create') {
|
||||
gdrive.createFile(state.folderId, fileManager.currentFile, fileManager.content);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
function handleAuthResult(authResult) {
|
||||
if (authResult && !authResult.error) {
|
||||
$("#drive-link").hide();
|
||||
gapi.client.load('drive', 'v2', function() {
|
||||
start();
|
||||
});
|
||||
}
|
||||
}
|
||||
$("#drive-link").click(
|
||||
function() {
|
||||
gapi.auth.authorize({ 'client_id' : CLIENT_ID,
|
||||
'scope' : SCOPES, 'immediate' : false }, handleAuthResult);
|
||||
});
|
||||
gapi.auth.authorize({ 'client_id' : CLIENT_ID, 'scope' : SCOPES,
|
||||
'immediate' : true }, handleAuthResult);
|
||||
};
|
||||
|
||||
gdrive.createFile = function(folderId, title, content) {
|
||||
const boundary = '-------314159265358979323846';
|
||||
const delimiter = "\r\n--" + boundary + "\r\n";
|
||||
const close_delim = "\r\n--" + boundary + "--";
|
||||
|
||||
var contentType = 'text/x-markdown';
|
||||
var metadata = { 'title' : title, 'mimeType' : contentType, 'parents' : [ { 'kind' : 'drive#fileLink', 'id' : folderId } ] };
|
||||
|
||||
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({
|
||||
'path' : '/upload/drive/v2/files',
|
||||
'method' : 'POST',
|
||||
'params' : { 'uploadType' : 'multipart', },
|
||||
'headers' : { 'Content-Type' : 'multipart/mixed; boundary="'
|
||||
+ boundary + '"', }, 'body' : multipartRequestBody, });
|
||||
if (!callback) {
|
||||
callback = function(file) {
|
||||
console.log(file);
|
||||
};
|
||||
}
|
||||
request.execute(callback);
|
||||
};
|
||||
|
||||
return gdrive;
|
||||
})(jQuery);
|
||||
|
||||
(function($) {
|
||||
|
||||
$(function() {
|
||||
var converter = Markdown.getSanitizingConverter();
|
||||
var editor = new Markdown.Editor(converter);
|
||||
editor.run();
|
||||
$(window).resize(resize);
|
||||
resize();
|
||||
|
||||
if (typeof (Storage) !== "undefined") {
|
||||
fileManager.init();
|
||||
} else {
|
||||
showError("Web storage is not available");
|
||||
};
|
||||
});
|
||||
|
||||
function resize() {
|
||||
$("#wmd-input").width($(window).width() / 2 - 60).height(
|
||||
$(window).height() - 70);
|
||||
$("#wmd-preview").width($(window).width() / 2 - 60).height(
|
||||
$(window).height() - 100);
|
||||
}
|
||||
};
|
||||
|
||||
function showError(msg) {
|
||||
alert(msg);
|
||||
}
|
||||
})(jQuery);
|
||||
|
Loading…
Reference in New Issue
Block a user