Convert indention to spaces
This commit is contained in:
parent
f2953d1190
commit
3b4f91249f
157
js/imgur.js
157
js/imgur.js
|
@ -5,115 +5,114 @@ var weechat = angular.module('weechat');
|
||||||
|
|
||||||
weechat.factory('imgur', ['$rootScope', function($rootScope) {
|
weechat.factory('imgur', ['$rootScope', function($rootScope) {
|
||||||
|
|
||||||
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
|
// New file reader
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
|
|
||||||
// When image is read
|
// When image is read
|
||||||
reader.onload = function (event) {
|
reader.onload = function (event) {
|
||||||
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
|
// Read image as data url
|
||||||
reader.readAsDataURL(image);
|
reader.readAsDataURL(image);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Upload image to imgur from base64
|
// Upload image to imgur from base64
|
||||||
var upload = function( base64img, callback ) {
|
var upload = function( base64img, callback ) {
|
||||||
|
// Set client ID (Glowing Bear)
|
||||||
|
var clientId = "164efef8979cd4b";
|
||||||
|
|
||||||
// Set client ID (Glowing Bear)
|
// Progress bar DOM elment
|
||||||
var clientId = "164efef8979cd4b";
|
var progressBar = document.getElementById("imgur-upload-progress");
|
||||||
|
progressBar.style.width = '0';
|
||||||
|
|
||||||
// Progress bar DOM elment
|
// Create new form data
|
||||||
var progressBar = document.getElementById("imgur-upload-progress");
|
var fd = new FormData();
|
||||||
progressBar.style.width = '0';
|
fd.append("image", base64img); // Append the file
|
||||||
|
fd.append("type", "base64"); // Set image type to base64
|
||||||
|
|
||||||
// Create new form data
|
// Create new XMLHttpRequest
|
||||||
var fd = new FormData();
|
var xhttp = new XMLHttpRequest();
|
||||||
fd.append("image", base64img); // Append the file
|
|
||||||
fd.append("type", "base64"); // Set image type to base64
|
|
||||||
|
|
||||||
// Create new XMLHttpRequest
|
// Post request to imgur api
|
||||||
var xhttp = new XMLHttpRequest();
|
xhttp.open("POST", "https://api.imgur.com/3/image", true);
|
||||||
|
|
||||||
// Post request to imgur api
|
// Set headers
|
||||||
xhttp.open("POST", "https://api.imgur.com/3/image", true);
|
xhttp.setRequestHeader("Authorization", "Client-ID " + clientId);
|
||||||
|
xhttp.setRequestHeader("Accept", "application/json");
|
||||||
|
|
||||||
// Set headers
|
// Handler for response
|
||||||
xhttp.setRequestHeader("Authorization", "Client-ID " + clientId);
|
xhttp.onload = function() {
|
||||||
xhttp.setRequestHeader("Accept", "application/json");
|
|
||||||
|
|
||||||
// Handler for response
|
progressBar.style.display = 'none';
|
||||||
xhttp.onload = function() {
|
|
||||||
|
|
||||||
progressBar.style.display = 'none';
|
// Check state and response status
|
||||||
|
if(xhttp.status === 200) {
|
||||||
|
|
||||||
// Check state and response status
|
// Get response text
|
||||||
if(xhttp.status === 200) {
|
var response = JSON.parse(xhttp.responseText);
|
||||||
|
|
||||||
// Get response text
|
// Send link as message
|
||||||
var response = JSON.parse(xhttp.responseText);
|
if( response.data && response.data.link ) {
|
||||||
|
|
||||||
// Send link as message
|
if (callback && typeof(callback) === "function") {
|
||||||
if( response.data && response.data.link ) {
|
callback(response.data.link);
|
||||||
|
}
|
||||||
|
|
||||||
if (callback && typeof(callback) === "function") {
|
} else {
|
||||||
callback(response.data.link);
|
showErrorMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
showErrorMsg();
|
showErrorMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
};
|
||||||
showErrorMsg();
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
if( "upload" in xhttp ) {
|
||||||
|
|
||||||
if( "upload" in xhttp ) {
|
// Set progress
|
||||||
|
xhttp.upload.onprogress = function (event) {
|
||||||
|
|
||||||
// Set progress
|
// Check if we can compute progress
|
||||||
xhttp.upload.onprogress = function (event) {
|
if (event.lengthComputable) {
|
||||||
|
// Complete in percent
|
||||||
|
var complete = (event.loaded / event.total * 100 | 0);
|
||||||
|
|
||||||
// Check if we can compute progress
|
// Set progress bar width
|
||||||
if (event.lengthComputable) {
|
progressBar.style.display = 'block';
|
||||||
// Complete in percent
|
progressBar.style.width = complete + '%';
|
||||||
var complete = (event.loaded / event.total * 100 | 0);
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Set progress bar width
|
}
|
||||||
progressBar.style.display = 'block';
|
|
||||||
progressBar.style.width = complete + '%';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
// Send request with form data
|
||||||
|
xhttp.send(fd);
|
||||||
|
|
||||||
// Send request with form data
|
};
|
||||||
xhttp.send(fd);
|
|
||||||
|
|
||||||
};
|
var showErrorMsg = function() {
|
||||||
|
// Show error msg
|
||||||
|
$rootScope.uploadError = true;
|
||||||
|
$rootScope.$apply();
|
||||||
|
|
||||||
var showErrorMsg = function() {
|
// Hide after 5 seconds
|
||||||
// Show error msg
|
setTimeout(function(){
|
||||||
$rootScope.uploadError = true;
|
// Hide error msg
|
||||||
$rootScope.$apply();
|
$rootScope.uploadError = false;
|
||||||
|
$rootScope.$apply();
|
||||||
|
}, 5000);
|
||||||
|
};
|
||||||
|
|
||||||
// Hide after 5 seconds
|
return {
|
||||||
setTimeout(function(){
|
|
||||||
// Hide error msg
|
|
||||||
$rootScope.uploadError = false;
|
|
||||||
$rootScope.$apply();
|
|
||||||
}, 5000);
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
process: process
|
process: process
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue