mirror of
https://github.com/zDevelopers/ImageOnMap.git
synced 2024-06-20 11:44:43 +02:00
Code cleanup, and adding more extensions
This commit is contained in:
parent
0040beeabf
commit
e894fb4f35
|
@ -61,63 +61,93 @@ import java.util.concurrent.Future;
|
|||
@WorkerAttributes(name = "Image Renderer", queriesMainThread = true)
|
||||
public class ImageRendererExecutor extends Worker
|
||||
{
|
||||
private static URLConnection connecting(URL url)throws IOException{
|
||||
final URLConnection connection = url.openConnection();
|
||||
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
|
||||
connection.connect();
|
||||
|
||||
if (connection instanceof HttpURLConnection)
|
||||
{
|
||||
final HttpURLConnection httpConnection = (HttpURLConnection) connection;
|
||||
final int httpCode = httpConnection.getResponseCode();
|
||||
|
||||
if ((httpCode / 100) != 2)
|
||||
{
|
||||
throw new IOException(I.t("HTTP error: {0} {1}", httpCode, httpConnection.getResponseMessage()));
|
||||
}
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
private enum extension{
|
||||
png, jpg, jpeg, gif
|
||||
}
|
||||
|
||||
|
||||
static public void render(final URL url, final ImageUtils.ScalingType scaling, final UUID playerUUID, final int width, final int height, WorkerCallback<ImageMap> callback)
|
||||
{
|
||||
submitQuery(new WorkerRunnable<ImageMap>()
|
||||
{
|
||||
@Override
|
||||
public ImageMap run() throws Throwable
|
||||
{
|
||||
URL url2=null;
|
||||
if(url.toString().contains("https://imgur.com/")){
|
||||
if(url.toString().contains("gallery/")){
|
||||
throw new IOException("We do not support imgur gallery yet, please use direct link to image instead (right click on the picture you want to use then select copy link:) ");
|
||||
public ImageMap run() throws Throwable {
|
||||
|
||||
BufferedImage image=null;
|
||||
//If the link is an imgur one
|
||||
if (url.toString().contains("https://imgur.com/")) {
|
||||
|
||||
//Not handled, can't with the hash only access the image in i.imgur.com/<hash>.<extension>
|
||||
|
||||
|
||||
if (url.toString().contains("gallery/")) {
|
||||
throw new IOException("We do not support imgur gallery yet, please use direct link to image instead. Right click on the picture you want to use then select copy picture link:) ");
|
||||
}
|
||||
|
||||
for (extension ext : extension.values()) {
|
||||
String newLink = "https://i.imgur.com/" + url.toString().split("https://imgur.com/")[1] + "." + ext.toString();
|
||||
URL url2 = new URL(newLink);
|
||||
|
||||
//Try connecting
|
||||
URLConnection connection = connecting(url2);
|
||||
|
||||
final InputStream stream = connection.getInputStream();
|
||||
|
||||
image = ImageIO.read(stream);
|
||||
|
||||
//valid image
|
||||
if (image != null) break;
|
||||
|
||||
}
|
||||
String newLink="https://i.imgur.com/"+url.toString().split("https://imgur.com/")[1]+".jpg";
|
||||
url2=new URL(newLink);
|
||||
|
||||
|
||||
}
|
||||
//If not an Imgur link
|
||||
else {
|
||||
|
||||
|
||||
//Try connecting
|
||||
URLConnection connection = connecting(url);
|
||||
|
||||
final InputStream stream = connection.getInputStream();
|
||||
|
||||
image = ImageIO.read(stream);
|
||||
|
||||
if(url2==null)
|
||||
url2=url;
|
||||
final URLConnection connection = url2.openConnection();
|
||||
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
|
||||
connection.connect();
|
||||
|
||||
if (connection instanceof HttpURLConnection)
|
||||
{
|
||||
final HttpURLConnection httpConnection = (HttpURLConnection) connection;
|
||||
final int httpCode = httpConnection.getResponseCode();
|
||||
|
||||
if ((httpCode / 100) != 2)
|
||||
{
|
||||
throw new IOException(I.t("HTTP error: {0} {1}", httpCode, httpConnection.getResponseMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
final InputStream stream = connection.getInputStream();
|
||||
|
||||
final BufferedImage image = ImageIO.read(stream);
|
||||
|
||||
if (image == null) throw new IOException(I.t("The given URL is not a valid image"));
|
||||
// Limits are in place and the player does NOT have rights to avoid them.
|
||||
if ((PluginConfiguration.LIMIT_SIZE_X.get() > 0 || PluginConfiguration.LIMIT_SIZE_Y.get() > 0) && !Permissions.BYPASS_SIZE.grantedTo(Bukkit.getPlayer(playerUUID)))
|
||||
{
|
||||
if (PluginConfiguration.LIMIT_SIZE_X.get() > 0)
|
||||
{
|
||||
if ((PluginConfiguration.LIMIT_SIZE_X.get() > 0 || PluginConfiguration.LIMIT_SIZE_Y.get() > 0) && !Permissions.BYPASS_SIZE.grantedTo(Bukkit.getPlayer(playerUUID))) {
|
||||
if (PluginConfiguration.LIMIT_SIZE_X.get() > 0) {
|
||||
if (image.getWidth() > PluginConfiguration.LIMIT_SIZE_X.get())
|
||||
throw new IOException(I.t("The image is too wide!"));
|
||||
}
|
||||
if (PluginConfiguration.LIMIT_SIZE_Y.get() > 0)
|
||||
{
|
||||
if (PluginConfiguration.LIMIT_SIZE_Y.get() > 0) {
|
||||
if (image.getHeight() > PluginConfiguration.LIMIT_SIZE_Y.get())
|
||||
throw new IOException(I.t("The image is too tall!"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (scaling != ImageUtils.ScalingType.NONE && height <= 1 && width <= 1)
|
||||
{
|
||||
if (scaling != ImageUtils.ScalingType.NONE && height <= 1 && width <= 1) {
|
||||
return renderSingle(scaling.resize(image, ImageMap.WIDTH, ImageMap.HEIGHT), playerUUID);
|
||||
}
|
||||
final BufferedImage resizedImage = scaling.resize(image, ImageMap.WIDTH * width, ImageMap.HEIGHT * height);
|
||||
|
@ -127,6 +157,7 @@ public class ImageRendererExecutor extends Worker
|
|||
}, callback);
|
||||
}
|
||||
|
||||
|
||||
static private ImageMap renderSingle(final BufferedImage image, final UUID playerUUID) throws Throwable
|
||||
{
|
||||
MapManager.checkMapLimit(1, playerUUID);
|
||||
|
|
Loading…
Reference in New Issue
Block a user