geforkt von Mirrors/FastAsyncWorldEdit
fix: imgur image resolution, image input stream reading (#2637)
Dieser Commit ist enthalten in:
Ursprung
63d5852b6c
Commit
68eb24a2f9
@ -40,6 +40,7 @@ import javax.annotation.Nullable;
|
|||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageReadParam;
|
import javax.imageio.ImageReadParam;
|
||||||
import javax.imageio.ImageReader;
|
import javax.imageio.ImageReader;
|
||||||
|
import javax.imageio.stream.ImageInputStream;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
@ -58,6 +59,9 @@ import java.net.URI;
|
|||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.channels.Channels;
|
import java.nio.channels.Channels;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.nio.channels.ReadableByteChannel;
|
import java.nio.channels.ReadableByteChannel;
|
||||||
@ -94,6 +98,10 @@ import static java.lang.System.arraycopy;
|
|||||||
public class MainUtil {
|
public class MainUtil {
|
||||||
|
|
||||||
private static final Logger LOGGER = LogManagerCompat.getLogger();
|
private static final Logger LOGGER = LogManagerCompat.getLogger();
|
||||||
|
private static final String CURL_USER_AGENT = "curl/8.1.1";
|
||||||
|
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
|
||||||
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||||
|
.build();
|
||||||
|
|
||||||
public static List<String> filter(String prefix, List<String> suggestions) {
|
public static List<String> filter(String prefix, List<String> suggestions) {
|
||||||
if (prefix.isEmpty()) {
|
if (prefix.isEmpty()) {
|
||||||
@ -523,25 +531,52 @@ public class MainUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedImage readImage(InputStream stream) throws IOException {
|
public static BufferedImage readImage(InputStream stream) throws IOException {
|
||||||
Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
|
final ImageInputStream imageStream = ImageIO.createImageInputStream(stream);
|
||||||
|
if (imageStream == null) {
|
||||||
|
throw new IOException("Can't find suitable ImageInputStream");
|
||||||
|
}
|
||||||
|
Iterator<ImageReader> iter = ImageIO.getImageReaders(imageStream);
|
||||||
if (!iter.hasNext()) {
|
if (!iter.hasNext()) {
|
||||||
throw new IOException("Could not get image reader from stream.");
|
throw new IOException("Could not get image reader from stream.");
|
||||||
}
|
}
|
||||||
ImageReader reader = iter.next();
|
ImageReader reader = iter.next();
|
||||||
ImageReadParam param = reader.getDefaultReadParam();
|
ImageReadParam param = reader.getDefaultReadParam();
|
||||||
reader.setInput(stream, true, true);
|
reader.setInput(imageStream, true, true);
|
||||||
BufferedImage bi;
|
BufferedImage bi;
|
||||||
try {
|
try {
|
||||||
bi = reader.read(0, param);
|
bi = reader.read(0, param);
|
||||||
} finally {
|
} finally {
|
||||||
reader.dispose();
|
reader.dispose();
|
||||||
stream.close();
|
stream.close();
|
||||||
|
imageStream.close();
|
||||||
}
|
}
|
||||||
return MainUtil.toRGB(bi);
|
return MainUtil.toRGB(bi);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedImage readImage(URL url) throws IOException {
|
public static BufferedImage readImage(URL url) throws IOException {
|
||||||
return readImage(url.openStream());
|
try {
|
||||||
|
final URI uri = url.toURI();
|
||||||
|
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri).GET();
|
||||||
|
|
||||||
|
if (uri.getHost().equalsIgnoreCase("i.imgur.com")) {
|
||||||
|
requestBuilder = requestBuilder.setHeader("User-Agent", CURL_USER_AGENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
final HttpResponse<InputStream> response = HTTP_CLIENT.send(
|
||||||
|
requestBuilder.build(),
|
||||||
|
HttpResponse.BodyHandlers.ofInputStream()
|
||||||
|
);
|
||||||
|
try (final InputStream body = response.body()) {
|
||||||
|
if (response.statusCode() > 299) {
|
||||||
|
throw new IOException("Expected 2xx as response code, but received " + response.statusCode());
|
||||||
|
}
|
||||||
|
return readImage(body);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new IOException("request was interrupted", e);
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw new IOException("failed to parse url to uri reference", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedImage readImage(File file) throws IOException {
|
public static BufferedImage readImage(File file) throws IOException {
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren