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