mirror of
https://github.com/SydMontague/ImageMaps.git
synced 2025-02-16 12:03:27 +01:00
Set proper User-Agent when downloading images
This way CDNs don't block the request
This commit is contained in:
parent
108812785e
commit
afea802cd2
@ -1,5 +1,12 @@
|
||||
package net.craftcitizen.imagemaps;
|
||||
|
||||
import de.craftlancer.core.LambdaRunnable;
|
||||
import de.craftlancer.core.util.MessageLevel;
|
||||
import de.craftlancer.core.util.MessageUtil;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -9,66 +16,59 @@ import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import de.craftlancer.core.LambdaRunnable;
|
||||
import de.craftlancer.core.util.MessageLevel;
|
||||
import de.craftlancer.core.util.MessageUtil;
|
||||
|
||||
public class ImageMapDownloadCommand extends ImageMapSubCommand {
|
||||
|
||||
|
||||
public ImageMapDownloadCommand(ImageMaps plugin) {
|
||||
super("imagemaps.download", plugin, true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String execute(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (!checkSender(sender)) {
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "You can't run this command.");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (args.length < 3) {
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "You must specify a file name and a download link.");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
String filename = args[1];
|
||||
String url = args[2];
|
||||
|
||||
|
||||
if (filename.contains("/") || filename.contains("\\") || filename.contains(":")) {
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Filename contains illegal character.");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
new LambdaRunnable(() -> download(sender, url, filename)).runTaskAsynchronously(plugin);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private void download(CommandSender sender, String input, String filename) {
|
||||
try {
|
||||
URL srcURL = new URL(input);
|
||||
|
||||
|
||||
if (!srcURL.getProtocol().startsWith("http")) {
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Download URL is not valid.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
URLConnection connection = srcURL.openConnection();
|
||||
|
||||
|
||||
if (!(connection instanceof HttpURLConnection)) {
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Download URL is not valid.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
connection.setRequestProperty("User-Agent", "ImageMap/0");
|
||||
|
||||
if (((HttpURLConnection) connection).getResponseCode() != 200) {
|
||||
MessageUtil.sendMessage(getPlugin(),
|
||||
sender,
|
||||
MessageLevel.WARNING,
|
||||
String.format("Download failed, HTTP Error code %d.", ((HttpURLConnection) connection).getResponseCode()));
|
||||
sender,
|
||||
MessageLevel.WARNING,
|
||||
String.format("Download failed, HTTP Error code %d.", ((HttpURLConnection) connection).getResponseCode()));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -88,16 +88,14 @@ public class ImageMapDownloadCommand extends ImageMapSubCommand {
|
||||
ImageIO.write(iamge, "PNG", new File(plugin.getDataFolder(), "images" + File.separatorChar + filename));
|
||||
}
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, "Download complete.");
|
||||
}
|
||||
catch (MalformedURLException ex) {
|
||||
} catch (MalformedURLException ex) {
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Malformatted URL");
|
||||
}
|
||||
catch (IOException ex) {
|
||||
} catch (IOException ex) {
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.ERROR, "An IO Exception happened, see server log");
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void help(CommandSender sender) {
|
||||
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, "Downloads an image from an URL.");
|
||||
|
Loading…
Reference in New Issue
Block a user