Last active
August 25, 2020 08:22
-
-
Save gabrielhpugliese/3745801 to your computer and use it in GitHub Desktop.
Google Drive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CLIENT_ID = '454038495802.apps.googleusercontent.com'; | |
var SCOPES = 'https://www.googleapis.com/auth/drive'; | |
/** | |
* 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) { | |
// Access token has been successfully retrieved, requests can be sent to the API. | |
var filePicker = document.getElementById('filePicker'); | |
filePicker.style.visibility = ''; | |
filePicker.onchange = uploadFile; | |
} else { | |
// No access token could be retrieved, force the authorization flow. | |
gapi.auth.authorize( | |
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false}, | |
handleAuthResult); | |
} | |
} | |
/** | |
* 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) { | |
const boundary = '-------314159265358979323846'; | |
const delimiter = '\r\n--' + boundary + '\r\n'; | |
const close_delim = '\r\n--' + boundary + '--'; | |
var reader = new FileReader(); | |
reader.readAsBinaryString(fileData); | |
reader.onload = function(e) { | |
var contentType = fileData.type || 'application/octet-stream'; | |
var metadata = { | |
'title': fileData.name, | |
'mimeType': contentType | |
}; | |
var base64Data = btoa(reader.result); | |
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
I don't even remember if this is my code only. It's from 8 years ago.
Added it anyway
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, can you please license this code of yours?
Thanks.