|
1 | 1 | package org.teacon.slides.cache;
|
2 | 2 |
|
| 3 | +import com.google.common.net.HttpHeaders; |
3 | 4 | import net.minecraft.FieldsAreNonnullByDefault;
|
4 | 5 | import net.minecraft.MethodsReturnNonnullByDefault;
|
5 | 6 | import org.apache.commons.io.IOUtils;
|
6 | 7 | import org.apache.http.Header;
|
7 |
| -import org.apache.http.HttpHeaders; |
8 | 8 | import org.apache.http.client.ClientProtocolException;
|
9 | 9 | import org.apache.http.client.cache.HttpCacheContext;
|
10 | 10 | import org.apache.http.client.methods.CloseableHttpResponse;
|
|
17 | 17 | import org.apache.logging.log4j.Logger;
|
18 | 18 | import org.apache.logging.log4j.Marker;
|
19 | 19 | import org.apache.logging.log4j.MarkerManager;
|
20 |
| -import org.teacon.slides.SlideShow; |
| 20 | +import org.teacon.content_disposition.ContentDisposition; |
21 | 21 |
|
22 | 22 | import javax.annotation.Nonnull;
|
23 | 23 | import javax.annotation.Nullable;
|
|
29 | 29 | import java.nio.file.Path;
|
30 | 30 | import java.nio.file.Paths;
|
31 | 31 | import java.util.Map;
|
| 32 | +import java.util.Optional; |
32 | 33 | import java.util.concurrent.CompletableFuture;
|
33 | 34 | import java.util.concurrent.CompletionException;
|
34 | 35 |
|
|
37 | 38 | @ParametersAreNonnullByDefault
|
38 | 39 | public final class ImageCache {
|
39 | 40 |
|
40 |
| - private static final Logger LOGGER = LogManager.getLogger(SlideShow.class); |
| 41 | + private static final Logger LOGGER = LogManager.getLogger("SlideShow"); |
41 | 42 | private static final Marker MARKER = MarkerManager.getMarker("Cache");
|
42 | 43 |
|
43 | 44 | private static final Path LOCAL_CACHE_PATH = Paths.get("slideshow");
|
@@ -85,9 +86,18 @@ public CompletableFuture<Map.Entry<String, byte[]>> getResource(@Nonnull URI loc
|
85 | 86 | final HttpCacheContext context = HttpCacheContext.create();
|
86 | 87 | try (CloseableHttpResponse response = createResponse(location, context, online)) {
|
87 | 88 | try {
|
| 89 | + Optional<ContentDisposition> dispositionOptional; |
| 90 | + try { |
| 91 | + dispositionOptional = Optional.ofNullable(response |
| 92 | + .getFirstHeader(HttpHeaders.CONTENT_DISPOSITION)) |
| 93 | + .map(Header::getValue).map(ContentDisposition::parse); |
| 94 | + } catch (IllegalArgumentException e) { |
| 95 | + dispositionOptional = Optional.empty(); |
| 96 | + } |
88 | 97 | ContentType type = ContentType.getLenient(response.getEntity());
|
89 | 98 | byte[] bytes = IOUtils.toByteArray(response.getEntity().getContent());
|
90 |
| - return Map.entry(FilenameAllocation.allocateHttpRespName(location, bytes, type), bytes); |
| 99 | + return Map.entry(dispositionOptional.flatMap(ContentDisposition::getFilename) |
| 100 | + .orElseGet(() -> FilenameAllocation.allocateHttpRespName(location, bytes, type)), bytes); |
91 | 101 | } catch (IOException e) {
|
92 | 102 | if (online) {
|
93 | 103 | LOGGER.warn(MARKER, "Failed to read bytes from remote source.", e);
|
|
0 commit comments