diff --git a/README.md b/README.md index e2325caa..dabf4b9d 100644 --- a/README.md +++ b/README.md @@ -169,11 +169,14 @@ For extend main functionality Cloud Commander use next modules: - [FancyBox] [FancyBoxURL] - [jQuery-contextMenu] [jQuery-contextMenuURL] - [jquery.terminal] [jquery.terminalURL] +- [dropbox-js] [dropbox-jsURL] [CodeMirrorURL]: https://github.com/marijnh/CodeMirror "CodeMirror" [FancyBoxURL]: https://github.com/fancyapps/fancyBox "FancyBox" [jQuery-contextMenuURL]: https://github.com/medialize/jQuery-contextMenu "jQuery-contextMenu" [jquery.terminalURL]: https://github.com/jcubic/jquery.terminal "jquery.terminal" +[githubURL]: https://github.com/michael/github +[dropbox-jsURL]: https://github.com/dropbox/dropbox-js Contributing --------------- diff --git a/lib/client/storage/_dropbox.js b/lib/client/storage/_dropbox.js index 5532e1a9..bedf6c3d 100644 --- a/lib/client/storage/_dropbox.js +++ b/lib/client/storage/_dropbox.js @@ -1,55 +1,113 @@ -var CloudCommander, DOM, Dropbox; -/* module for work with github */ +var CloudCommander, Util, DOM, gapi; (function(){ "use strict"; - var cloudcmd = CloudCommander, - CHOOSER_API = 'https://www.dropbox.com/static/api/1/dropbox.js', - CLIENT_ID, - DropBoxStore = {}, - options = { - linkType: "direct", - success: function(files) { - console.log("Here's the file link:" + files[0].link); - }, - cancel: function() { - console.log('Chose something'); - } - }; + var cloudcmd = CloudCommander, + GDrive = {}; - /* PRIVATE FUNCTIONS */ - /** - * function loads dropbox.js - */ - function load(){ - console.time('dropbox load'); - - cloudcmd.getConfig(function(pConfig){ - var lElement = DOM.anyload({ - src : CHOOSER_API, - not_append : true, - id : 'dropboxjs', - func : DropBoxStore.choose + function authorize(pData){ + /* https://developers.google.com/drive/credentials */ + Util.setTimeout({ + func : function(pCallBack){ + var lCLIENT_ID = '255175681917.apps.googleusercontent.com', + lSCOPES = 'https://www.googleapis.com/auth/drive', + lParams = { + 'client_id' : lCLIENT_ID, + 'scope' : lSCOPES, + 'immediate' : true + }; + + gapi.auth.authorize(lParams, pCallBack); + }, - }); - - var lDropBoxId = pConfig.dropbox_chooser_key; - lElement.setAttribute('data-app-key', lDropBoxId); - document.body.appendChild(lElement); - - console.timeEnd('dropbox load'); + callback : function(pAuthResult){ + var lRet; + if (pAuthResult && !pAuthResult.error){ + uploadFile(pData); + + lRet = true; + } + return lRet; + } }); } - DropBoxStore.choose = function(){ - Dropbox.choose(options); + function load(pData){ + var lUrl = 'https://apis.google.com/js/client.js'; + + DOM.jsload(lUrl, function(){ + authorize(pData); + }); + } + + /** + * Start the file upload. + * + * @param {Object} evt Arguments from the file selector. + */ + function uploadFile(pData) { + gapi.client.load('drive', 'v2', function() { + GDrive.uploadFile(pData); + }); + } + + /** + * Insert new file. + * + * @param {File} fileData {name, data} File object to read data from. + * @param {Function} callback Function to call when the request is complete. + */ + GDrive.uploadFile = function(pData, callback) { + var lData = pData.data, + lName = pData.name, + boundary = '-------314159265358979323846', + delimiter = "\r\n--" + boundary + "\r\n", + close_delim = "\r\n--" + boundary + "--", + + contentType = pData.type || 'application/octet-stream', + metadata = { + 'title' : lName, + 'mimeType' : contentType + }, + + base64Data = btoa(lData), + + 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); }; - DropBoxStore.init = function(){ - load(); + + GDrive.init = function(pData){ + load(pData); }; - cloudcmd.DropBox = DropBoxStore; -})(); + cloudcmd.GDrive = GDrive; +})(); \ No newline at end of file diff --git a/modules.json b/modules.json index 02aad66f..30766d4f 100644 --- a/modules.json +++ b/modules.json @@ -4,6 +4,7 @@ "viewer", "storage/_github", "storage/_dropbox", + "storage/_dropbox_chooser", "storage/_gdrive", "terminal" ] \ No newline at end of file