glowing-bear/js/imgur.js

129 lines
3.4 KiB
JavaScript
Raw Normal View History

2015-10-09 13:59:34 +02:00
(function() {
'use strict';
var weechat = angular.module('weechat');
weechat.factory('imgur', ['$rootScope', function($rootScope) {
2015-11-07 12:37:46 +01:00
var process = function(image, callback) {
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Is it an image?
if (!image || !image.type.match(/image.*/)) return;
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// New file reader
var reader = new FileReader();
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// When image is read
reader.onload = function (event) {
var image = event.target.result.split(',')[1];
upload(image, callback);
};
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Read image as data url
reader.readAsDataURL(image);
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
};
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Upload image to imgur from base64
var upload = function( base64img, callback ) {
// Set client ID (Glowing Bear)
var clientId = "164efef8979cd4b";
2015-10-12 21:09:13 +02:00
// Progress bars container
var progressBars = document.getElementById("imgur-upload-progress"),
currentProgressBar = document.createElement("div");
// Set progress bar attributes
currentProgressBar.className='imgur-progress-bar';
currentProgressBar.style.width = '0';
// Append progress bar
progressBars.appendChild(currentProgressBar);
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Create new form data
var fd = new FormData();
fd.append("image", base64img); // Append the file
fd.append("type", "base64"); // Set image type to base64
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Create new XMLHttpRequest
var xhttp = new XMLHttpRequest();
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Post request to imgur api
xhttp.open("POST", "https://api.imgur.com/3/image", true);
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Set headers
xhttp.setRequestHeader("Authorization", "Client-ID " + clientId);
xhttp.setRequestHeader("Accept", "application/json");
2015-11-07 12:37:46 +01:00
// Handler for response
xhttp.onload = function() {
2015-10-09 13:59:34 +02:00
2015-10-12 21:09:13 +02:00
// Remove progress bar
currentProgressBar.parentNode.removeChild(currentProgressBar);
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Check state and response status
if(xhttp.status === 200) {
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Get response text
var response = JSON.parse(xhttp.responseText);
2015-11-07 12:37:46 +01:00
// Send link as message
if( response.data && response.data.link ) {
2015-11-07 12:37:46 +01:00
if (callback && typeof(callback) === "function") {
callback(response.data.link);
}
2015-11-07 12:37:46 +01:00
} else {
showErrorMsg();
}
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
} else {
showErrorMsg();
}
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
};
2015-11-07 12:37:46 +01:00
if( "upload" in xhttp ) {
2015-11-07 12:37:46 +01:00
// Set progress
xhttp.upload.onprogress = function (event) {
2015-11-07 12:37:46 +01:00
// Check if we can compute progress
if (event.lengthComputable) {
// Complete in percent
var complete = (event.loaded / event.total * 100 | 0);
2015-11-07 12:37:46 +01:00
// Set progress bar width
2015-10-12 21:09:13 +02:00
currentProgressBar.style.width = complete + '%';
2015-11-07 12:37:46 +01:00
}
};
2015-11-07 12:37:46 +01:00
}
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
// Send request with form data
xhttp.send(fd);
2015-10-09 13:59:34 +02:00
2015-11-07 12:37:46 +01:00
};
2015-11-07 12:37:46 +01:00
var showErrorMsg = function() {
// Show error msg
$rootScope.uploadError = true;
$rootScope.$apply();
// Hide after 5 seconds
setTimeout(function(){
// Hide error msg
$rootScope.uploadError = false;
$rootScope.$apply();
}, 5000);
};
2015-11-07 12:37:46 +01:00
return {
2015-10-09 13:59:34 +02:00
process: process
};
}]);
})();