Skip to content

Commit

Permalink
Recognize content disposition now
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Aug 22, 2024
1 parent 808379c commit 32c1b27
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,12 @@ configurations {
dependencies {
// Modified by TeaCon
jarJar implementation('org.teacon:urlpattern:1.0.1')
jarJar implementation('org.teacon:content-disposition:1.0.0')
jarJar implementation('io.github.darkxanter:webp-imageio:0.3.2')
jarJar implementation('org.apache.httpcomponents:httpclient-cache:4.5.13')
// Modified by TeaCon
additionalRuntimeClasspath 'org.teacon:urlpattern:1.0.1'
additionalRuntimeClasspath 'org.teacon:content-disposition:1.0.0'
additionalRuntimeClasspath 'io.github.darkxanter:webp-imageio:0.3.2'
additionalRuntimeClasspath 'org.apache.httpcomponents:httpclient-cache:4.5.13'
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/teacon/slides/cache/CacheStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.teacon.slides.SlideShow;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
Expand All @@ -50,7 +49,7 @@
@ParametersAreNonnullByDefault
final class CacheStorage implements HttpCacheStorage {

private static final Logger LOGGER = LogManager.getLogger(SlideShow.class);
private static final Logger LOGGER = LogManager.getLogger("SlideShow");
private static final Marker MARKER = MarkerManager.getMarker("Downloader");

private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/teacon/slides/cache/ImageCache.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.teacon.slides.cache;

import com.google.common.net.HttpHeaders;
import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.cache.HttpCacheContext;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand All @@ -17,7 +17,7 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.teacon.slides.SlideShow;
import org.teacon.content_disposition.ContentDisposition;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -29,6 +29,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

Expand All @@ -37,7 +38,7 @@
@ParametersAreNonnullByDefault
public final class ImageCache {

private static final Logger LOGGER = LogManager.getLogger(SlideShow.class);
private static final Logger LOGGER = LogManager.getLogger("SlideShow");
private static final Marker MARKER = MarkerManager.getMarker("Cache");

private static final Path LOCAL_CACHE_PATH = Paths.get("slideshow");
Expand Down Expand Up @@ -85,9 +86,18 @@ public CompletableFuture<Map.Entry<String, byte[]>> getResource(@Nonnull URI loc
final HttpCacheContext context = HttpCacheContext.create();
try (CloseableHttpResponse response = createResponse(location, context, online)) {
try {
Optional<ContentDisposition> dispositionOptional;
try {
dispositionOptional = Optional.ofNullable(response
.getFirstHeader(HttpHeaders.CONTENT_DISPOSITION))
.map(Header::getValue).map(ContentDisposition::parse);
} catch (IllegalArgumentException e) {
dispositionOptional = Optional.empty();
}
ContentType type = ContentType.getLenient(response.getEntity());
byte[] bytes = IOUtils.toByteArray(response.getEntity().getContent());
return Map.entry(FilenameAllocation.allocateHttpRespName(location, bytes, type), bytes);
return Map.entry(dispositionOptional.flatMap(ContentDisposition::getFilename)
.orElseGet(() -> FilenameAllocation.allocateHttpRespName(location, bytes, type)), bytes);
} catch (IOException e) {
if (online) {
LOGGER.warn(MARKER, "Failed to read bytes from remote source.", e);
Expand Down

0 comments on commit 32c1b27

Please sign in to comment.