Skip to content

Commit

Permalink
Fix dynamic download content types
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Feb 18, 2024
1 parent 842b3be commit 7525cc0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import io.papermc.bibliothek.exception.DownloadNotFound;
import io.papermc.bibliothek.exception.ProjectNotFound;
import io.papermc.bibliothek.exception.VersionNotFound;
import io.papermc.bibliothek.util.CustomFileNameMap;
import io.papermc.bibliothek.util.HTTP;
import io.papermc.bibliothek.util.MediaTypeMap;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.headers.Header;
Expand Down Expand Up @@ -66,7 +66,7 @@
public class DownloadController {
private static final CacheControl CACHE_LATEST = HTTP.sMaxAgePublicCache(Duration.ofMinutes(1));
private static final CacheControl CACHE_SPECIFIC = HTTP.sMaxAgePublicCache(Duration.ofDays(14));
private static final CustomFileNameMap FILE_NAME_MAP = new CustomFileNameMap();
private static final MediaTypeMap FILE_NAME_MAP = new MediaTypeMap();
private final AppConfiguration configuration;
private final ProjectCollection projects;
private final VersionCollection versions;
Expand Down Expand Up @@ -203,7 +203,7 @@ private static HttpHeaders headersFor(final Path path, final CacheControl cache)
final HttpHeaders headers = new HttpHeaders();
headers.setCacheControl(cache);
headers.setContentDisposition(HTTP.attachmentDisposition(path.getFileName()));
headers.setContentType(MediaType.valueOf(FILE_NAME_MAP.getContentTypeFor(path.getFileName().toString())));
headers.setContentType(FILE_NAME_MAP.mediaTypeFor(path.getFileName().toString()));
headers.setLastModified(Files.getLastModifiedTime(path).toInstant());
return headers;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/papermc/bibliothek/util/HTTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

public final class HTTP {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of bibliothek, licensed under the MIT License.
*
* Copyright (c) 2024 GeyserMC
* Copyright (c) 2019-2023 PaperMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,24 +23,24 @@
*/
package io.papermc.bibliothek.util;

import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;

public class CustomFileNameMap implements FileNameMap {
private final FileNameMap internalMap = URLConnection.getFileNameMap();

private final Map<String, String> customMap = new HashMap<>() {{
put("mcpack", "application/zip");
}};
public class MediaTypeMap {
private final Map<String, MediaType> customMap = new HashMap<>() {
{
put("mcpack", MediaType.parseMediaType("application/zip"));
}
};

public String getContentTypeFor(String fileName) {
return customMap.getOrDefault(getExtension(fileName), internalMap.getContentTypeFor(fileName));
public MediaType mediaTypeFor(final String fileName) {
return this.customMap.getOrDefault(this.fileExtension(fileName), MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM));
}

private String getExtension(String fileName) {
int index = fileName.lastIndexOf('.');
private String fileExtension(final String fileName) {
final int index = fileName.lastIndexOf('.');
if (index == -1) {
return "";
}
Expand Down

0 comments on commit 7525cc0

Please sign in to comment.