diff --git a/src/main/java/io/papermc/bibliothek/controller/v2/DownloadController.java b/src/main/java/io/papermc/bibliothek/controller/v2/DownloadController.java index aa61787..a0969be 100644 --- a/src/main/java/io/papermc/bibliothek/controller/v2/DownloadController.java +++ b/src/main/java/io/papermc/bibliothek/controller/v2/DownloadController.java @@ -36,18 +36,18 @@ import io.papermc.bibliothek.exception.ProjectNotFound; import io.papermc.bibliothek.exception.VersionNotFound; import io.papermc.bibliothek.util.HTTP; -import io.papermc.bibliothek.util.MediaTypeMap; +import io.papermc.bibliothek.util.MediaTypes; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.headers.Header; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Positive; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; -import jakarta.validation.constraints.Positive; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.http.CacheControl; @@ -55,7 +55,6 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.util.MimeTypeUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -67,7 +66,6 @@ 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 MediaTypeMap FILE_NAME_MAP = new MediaTypeMap(); private final AppConfiguration configuration; private final ProjectCollection projects; private final VersionCollection versions; @@ -143,7 +141,7 @@ public ResponseEntity downloadLatest( value = "/v2/projects/{project:[a-z]+}/versions/{version:" + Version.PATTERN + "}/builds/{build:\\d+}/downloads/{download:[a-z]+}", produces = { MediaType.APPLICATION_JSON_VALUE, - MimeTypeUtils.ALL_VALUE + MediaType.ALL_VALUE } ) @Operation(summary = "Downloads the given file from a build's data.") @@ -204,7 +202,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(FILE_NAME_MAP.mediaTypeFor(path.getFileName().toString())); + headers.setContentType(MediaTypes.fromFileName(path.getFileName().toString())); headers.setLastModified(Files.getLastModifiedTime(path).toInstant()); return headers; } diff --git a/src/main/java/io/papermc/bibliothek/controller/v2/VersionBuildController.java b/src/main/java/io/papermc/bibliothek/controller/v2/VersionBuildController.java index 871e79d..ebeb94b 100644 --- a/src/main/java/io/papermc/bibliothek/controller/v2/VersionBuildController.java +++ b/src/main/java/io/papermc/bibliothek/controller/v2/VersionBuildController.java @@ -39,11 +39,11 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Positive; import java.time.Duration; import java.time.Instant; import java.util.List; import java.util.Map; -import jakarta.validation.constraints.Positive; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.CacheControl; import org.springframework.http.MediaType; diff --git a/src/main/java/io/papermc/bibliothek/util/MediaTypeMap.java b/src/main/java/io/papermc/bibliothek/util/MediaTypes.java similarity index 63% rename from src/main/java/io/papermc/bibliothek/util/MediaTypeMap.java rename to src/main/java/io/papermc/bibliothek/util/MediaTypes.java index 17b6ea6..ef04049 100644 --- a/src/main/java/io/papermc/bibliothek/util/MediaTypeMap.java +++ b/src/main/java/io/papermc/bibliothek/util/MediaTypes.java @@ -23,27 +23,30 @@ */ package io.papermc.bibliothek.util; -import java.util.HashMap; -import java.util.Map; +import org.jetbrains.annotations.Nullable; import org.springframework.http.MediaType; import org.springframework.http.MediaTypeFactory; -public class MediaTypeMap { - private final Map customMap = new HashMap<>() { - { - put("mcpack", MediaType.parseMediaType("application/zip")); - } - }; +public final class MediaTypes { + + public static final String APPLICATION_ZIP_VALUE = "application/zip"; + public static final MediaType APPLICATION_ZIP = MediaType.parseMediaType(APPLICATION_ZIP_VALUE); - public MediaType mediaTypeFor(final String fileName) { - return this.customMap.getOrDefault(this.fileExtension(fileName), MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM)); + private MediaTypes() { } - private String fileExtension(final String fileName) { - final int index = fileName.lastIndexOf('.'); - if (index == -1) { - return ""; + public static @Nullable MediaType fromFileName(final String name) { + final int index = name.lastIndexOf('.'); + if (index != -1) { + return fromFileExtension(name.substring(index + 1)); } - return fileName.substring(index + 1); + return null; + } + + public static @Nullable MediaType fromFileExtension(final String extension) { + return switch (extension) { + case "mcpack" -> APPLICATION_ZIP; + default -> MediaTypeFactory.getMediaType("." + extension).orElse(MediaType.APPLICATION_OCTET_STREAM); + }; } }