Skip to content

Instantly share code, notes, and snippets.

@Heavyblade
Created April 25, 2020 15:52
Show Gist options
  • Save Heavyblade/091c512a4bef15b02dafe3bb24bce11d to your computer and use it in GitHub Desktop.
Save Heavyblade/091c512a4bef15b02dafe3bb24bce11d to your computer and use it in GitHub Desktop.
googledrive.js
function fileUpload( file, parent_id, callback ) {
var fileToUp = readFile( file ),
contentType = $.extensions[file.split(".").pop()] || "application/octet-stream";
if ( fileToUp.name !== "empty" ) {
$.ajax({
type: "POST",
url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
headers: { "Authorization": ("Bearer " + theApp.globalVarToString("vStorageDat/DRIVE_AUTH_TOKEN")),
"Content-Type": "application/json",
"X-Upload-Content-Type": contentType,
"X-Upload-Content-Length": fileToUp.array.length
},
data: {name: fileToUp.name, parents: [parent_id]},
success: function(data, codigo, headers) {
var uploadUrl = headers.Location;
$.ajax({
type: "POST",
url: uploadUrl,
headers: { "Authorization": ("Bearer " + theApp.globalVarToString("vStorageDat/DRIVE_AUTH_TOKEN")),
"Content-Type": contentType,
"Content-Length": fileToUp.array.length
},
body: fileToUp.array,
responseType: "json",
timeout: (5*60),
success: function(data, codigo) {
if ( typeof(callback) == "function" ) { callback(data); }
},
error: function(data, code) {
alert("Code: " + code + "\n\n" + JSON.stringify(data))
theApp.setGlobalVar("vStorageDat/UPLOAD_WORKING", 0);
}
});
},
error: function(error, code) {
if ( code == 401 ) { refreshToken(fileUpload, [file, parent_id, callback]); return; }
alert("Error '" + error + "' code: " + code);
theApp.setGlobalVar("vStorageDat/UPLOAD_WORKING", 0);
}
});
} else {
aler("Error: el archivo '" + file + "' no se encuentra");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment