Merge pull request #1128 from glowing-bear/simplify-imgur

Imgur module cleanup
codeql
Lorenz Hübschle-Schneider 5 years ago committed by GitHub
commit 65bc4faaf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 91
      js/imgur.js

@ -6,11 +6,9 @@ var weechat = angular.module('weechat');
weechat.factory('imgur', ['$rootScope', 'settings', function($rootScope, settings) { weechat.factory('imgur', ['$rootScope', 'settings', function($rootScope, settings) {
var process = function(image, callback) { var process = function(image, callback) {
// Is it an image? // Is it an image?
if (!image || !image.type.match(/image.*/)) return; if (!image || !image.type.match(/image.*/)) return;
// New file reader
var reader = new FileReader(); var reader = new FileReader();
// When image is read // When image is read
@ -18,100 +16,77 @@ weechat.factory('imgur', ['$rootScope', 'settings', function($rootScope, setting
var image = event.target.result.split(',')[1]; var image = event.target.result.split(',')[1];
upload(image, callback); upload(image, callback);
}; };
// Read image as data url
reader.readAsDataURL(image); reader.readAsDataURL(image);
}; };
// Upload image to imgur from base64 var authenticate = function(xhr) {
var upload = function( base64img, callback ) {
// API authorization, either via Client ID (anonymous) or access token // API authorization, either via Client ID (anonymous) or access token
// (add to user's imgur account), see also: // (add to user's imgur account), see also:
// https://github.com/glowing-bear/glowing-bear/wiki/Getting-an-imgur-token-&-album-hash // https://github.com/glowing-bear/glowing-bear/wiki/Getting-an-imgur-token-&-album-hash
var accessToken = "164efef8979cd4b"; var accessToken = "164efef8979cd4b";
var isClientID = true;
// Check whether the user has provided an access token // Check whether the user has configured a bearer token, if so, use it
if (settings.iToken.length > 37){ // to add the image to the user's account
accessToken = settings.iToken; if (settings.iToken.length >= 38){
isClientID = false; xhr.setRequestHeader("Authorization", "Bearer " + settings.iToken);
} else {
xhr.setRequestHeader("Authorization", "Client-ID " + accessToken);
} }
};
// Upload image to imgur from base64
var upload = function( base64img, callback ) {
// Progress bars container // Progress bars container
var progressBars = document.getElementById("imgur-upload-progress"), var progressBars = document.getElementById("imgur-upload-progress"),
currentProgressBar = document.createElement("div"); currentProgressBar = document.createElement("div");
// Set progress bar attributes
currentProgressBar.className='imgur-progress-bar'; currentProgressBar.className='imgur-progress-bar';
currentProgressBar.style.width = '0'; currentProgressBar.style.width = '0';
// Append progress bar
progressBars.appendChild(currentProgressBar); progressBars.appendChild(currentProgressBar);
// Create new form data // Assemble the form data for the upload
var fd = new FormData(); var fd = new FormData();
fd.append("image", base64img); // Append the file fd.append("image", base64img);
fd.append("type", "base64"); // Set image type to base64 fd.append("type", "base64");
// Add the image to the provided album if configured to do so // Add the image to the provided album if configured to do so
if (!isClientID && settings.iAlb.length >= 6) { if (settings.iToken.length >= 38 && settings.iAlb.length >= 6) {
fd.append("album", settings.iAlb); fd.append("album", settings.iAlb);
} }
// Create new XMLHttpRequest
var xhttp = new XMLHttpRequest();
// Post request to imgur api // Post request to imgur api
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "https://api.imgur.com/3/image", true); xhttp.open("POST", "https://api.imgur.com/3/image", true);
authenticate(xhttp);
// Set headers
if (isClientID) {
xhttp.setRequestHeader("Authorization", "Client-ID " + accessToken);
} else {
xhttp.setRequestHeader("Authorization", "Bearer " + accessToken);
}
xhttp.setRequestHeader("Accept", "application/json"); xhttp.setRequestHeader("Accept", "application/json");
// Handler for response // Handler for response
xhttp.onload = function() { xhttp.onload = function() {
// Remove progress bar // Remove progress bar
currentProgressBar.parentNode.removeChild(currentProgressBar); progressBars.removeChild(currentProgressBar);
// Check state and response status // Check state and response status
if(xhttp.status === 200) { if (xhttp.status === 200) {
// Get response text
var response = JSON.parse(xhttp.responseText); var response = JSON.parse(xhttp.responseText);
// Send link as message // Send link as message
if( response.data && response.data.link ) { if (response.data && response.data.link) {
if (callback && typeof(callback) === "function") { if (callback && typeof(callback) === "function") {
callback(response.data.link.replace(/^http:/, "https:"), response.data.deletehash); callback(response.data.link.replace(/^http:/, "https:"), response.data.deletehash);
} }
} else { } else {
showErrorMsg(); showErrorMsg();
} }
} else { } else {
showErrorMsg(); showErrorMsg();
} }
}; };
if( "upload" in xhttp ) { if ("upload" in xhttp) {
// Set progress // Update the progress bar if we can compute progress
xhttp.upload.onprogress = function (event) { xhttp.upload.onprogress = function (event) {
// Check if we can compute progress
if (event.lengthComputable) { if (event.lengthComputable) {
// Complete in percent
var complete = (event.loaded / event.total * 100 | 0); var complete = (event.loaded / event.total * 100 | 0);
// Set progress bar width
currentProgressBar.style.width = complete + '%'; currentProgressBar.style.width = complete + '%';
} }
}; };
@ -122,37 +97,17 @@ weechat.factory('imgur', ['$rootScope', 'settings', function($rootScope, setting
// Delete an image from imgur with the deletion link // Delete an image from imgur with the deletion link
var deleteImage = function( deletehash, callback ) { var deleteImage = function( deletehash, callback ) {
// API authorization, either via Client ID (anonymous) or access token
// (add to user's imgur account), see also:
// https://github.com/glowing-bear/glowing-bear/wiki/Getting-an-imgur-token-&-album-hash
var accessToken = "164efef8979cd4b";
var isClientID = true;
// Check whether the user has provided an access token
if (settings.iToken.length > 37){
accessToken = settings.iToken;
isClientID = false;
}
// Create new XMLHttpRequest
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
// Post request to imgur api // Post request to imgur api
xhttp.open("DELETE", "https://api.imgur.com/3/image/" + deletehash, true); xhttp.open("DELETE", "https://api.imgur.com/3/image/" + deletehash, true);
authenticate(xhttp);
// Set headers
if (isClientID) {
xhttp.setRequestHeader("Authorization", "Client-ID " + accessToken);
} else {
xhttp.setRequestHeader("Authorization", "Bearer " + accessToken);
}
xhttp.setRequestHeader("Accept", "application/json"); xhttp.setRequestHeader("Accept", "application/json");
// Handler for response // Handler for response
xhttp.onload = function() { xhttp.onload = function() {
// Check state and response status // Check state and response status
if(xhttp.status === 200) { if (xhttp.status === 200) {
callback(deletehash); callback(deletehash);
} else { } else {
showErrorMsg(); showErrorMsg();

Loading…
Cancel
Save