Switch to using blobs for Imgur upload

main
Isaiah Odhner 2018-08-12 23:22:12 -04:00
parent 2127a20d18
commit d66f6ad245
2 changed files with 65 additions and 59 deletions

View File

@ -1,6 +1,6 @@
var $imgur_window; var $imgur_window;
function upload_to_imgur(){ function show_imgur_uploader(){
if($imgur_window){ if($imgur_window){
$imgur_window.close(); $imgur_window.close();
} }
@ -29,65 +29,69 @@ function upload_to_imgur(){
// include the selection in the saved image (by deselecting) // include the selection in the saved image (by deselecting)
deselect(); deselect();
// base64 encoding to send to Imgur API canvas.toBlob(function(blob){
// TODO: send a Blob instead (and sanity_check_blob first) sanity_check_blob(blob, function(){
var base64 = canvas.toDataURL().split(",")[1];
var payload = {
image: base64,
};
// send HTTP request to the Imgur image upload API var form_data = new FormData();
$.ajax({ form_data.append('image', blob);
type: "POST",
url: "https://api.imgur.com/3/image",
headers: {
"Authorization": "Client-ID 203da2f300125a1",
},
dataType: 'json', // of what's expected from the server, NOT what we're sending
data: payload, // what we're sending
beforeSend: function(){
$imgur_description.text("Loading...");
},
success: function(data){
if(!data.success){
$imgur_description.text("Failed to upload image :(");
return;
}
var url = data.data.link;
$imgur_description.text("");
$imgur_url.text(url);
$imgur_url.attr('href', url);
$imgur_window.$Button("Delete", function(){ // send HTTP request to the Imgur image upload API
$.ajax({ // TODO: progress bar for upload
type: "DELETE", $.ajax({
url: "https://api.imgur.com/3/image/" + data.data.deletehash, type: "POST",
headers: { url: "https://api.imgur.com/3/image",
"Authorization": "Client-ID 203da2f300125a1", headers: {
}, "Authorization": "Client-ID 203da2f300125a1",
dataType: 'json', // of what's expected from the server },
beforeSend: function(){ dataType: 'json', // of what's expected from the server, NOT what we're sending
$imgur_description.text("Loading..."); data: form_data, // what we're sending
}, processData: false, // don't try to process the form data (avoid "Illegal invocation")
success: function(data){ contentType: false, // don't send an incorrect Content-Type header please (avoid 500 error from Imgur)
if(data.success){ beforeSend: function(){
$imgur_url.text(''); $imgur_description.text("Loading...");
$imgur_url.attr('href', '#'); },
$imgur_description.text("Deleted successfully"); success: function(data){
}else{ if(!data.success){
$imgur_description.text("Failed to delete image :("); $imgur_description.text("Failed to upload image :(");
} return;
}, }
error: function(error){ var url = data.data.link;
$imgur_description.text("Error deleting image :("); $imgur_description.text("");
}, $imgur_url.text(url);
}); $imgur_url.attr('href', url);
});
}, $imgur_window.$Button("Delete", function(){
error: function(error){ $.ajax({
$imgur_description.text("Error uploading image :("); type: "DELETE",
}, url: "https://api.imgur.com/3/image/" + data.data.deletehash,
}) headers: {
"Authorization": "Client-ID 203da2f300125a1",
},
dataType: 'json', // of what's expected from the server
beforeSend: function(){
$imgur_description.text("Loading...");
},
success: function(data){
if(data.success){
$imgur_url.text('');
$imgur_url.attr('href', '#');
$imgur_description.text("Deleted successfully");
}else{
$imgur_description.text("Failed to delete image :(");
}
},
error: function(error){
$imgur_description.text("Error deleting image :(");
},
});
});
},
error: function(error){
$imgur_description.text("Error uploading image :(");
},
})
});
});
}); });
$imgur_window.$Button("Cancel", function(){ $imgur_window.$Button("Cancel", function(){
$imgur_window.close(); $imgur_window.close();

View File

@ -35,7 +35,9 @@ var menus = {
}, },
{ {
item: "&Upload To Imgur", item: "&Upload To Imgur",
action: upload_to_imgur, action: function(){
show_imgur_uploader();
},
description: "Uploads the active document to Imgur", description: "Uploads the active document to Imgur",
}, },
$MenuBar.DIVIDER, $MenuBar.DIVIDER,