From 11b025ec9a819f13e2d644bd64536fd79cf654b6 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 16 Feb 2024 23:35:54 +0000 Subject: [PATCH] Revert "Add dynamic content type for more than .jar downloads" This reverts commit be237526b808d2e3feec5994bcee25ecb838f6a7. --- .../controller/v2/DownloadController.java | 13 ++--- .../bibliothek/util/CustomFileNameMap.java | 49 ------------------- .../java/io/papermc/bibliothek/util/HTTP.java | 3 ++ 3 files changed, 8 insertions(+), 57 deletions(-) delete mode 100644 src/main/java/io/papermc/bibliothek/util/CustomFileNameMap.java 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 25ff252..665dd40 100644 --- a/src/main/java/io/papermc/bibliothek/controller/v2/DownloadController.java +++ b/src/main/java/io/papermc/bibliothek/controller/v2/DownloadController.java @@ -35,7 +35,6 @@ 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.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -54,7 +53,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; @@ -66,7 +64,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 CustomFileNameMap FILE_NAME_MAP = new CustomFileNameMap(); private final AppConfiguration configuration; private final ProjectCollection projects; private final VersionCollection versions; @@ -142,7 +139,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 + HTTP.APPLICATION_JAVA_ARCHIVE_VALUE } ) @Operation(summary = "Downloads the given file from a build's data.") @@ -181,7 +178,7 @@ public ResponseEntity downloadSpecific( } try { - return new DownloadFile( + return new JavaArchive( this.configuration.getStoragePath() .resolve(project.name()) .resolve(version.name()) @@ -194,8 +191,8 @@ public ResponseEntity downloadSpecific( } } - private static class DownloadFile extends ResponseEntity { - DownloadFile(final Path path, final CacheControl cache) throws IOException { + private static class JavaArchive extends ResponseEntity { + JavaArchive(final Path path, final CacheControl cache) throws IOException { super(new FileSystemResource(path), headersFor(path, cache), HttpStatus.OK); } @@ -203,7 +200,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(HTTP.APPLICATION_JAVA_ARCHIVE); headers.setLastModified(Files.getLastModifiedTime(path).toInstant()); return headers; } diff --git a/src/main/java/io/papermc/bibliothek/util/CustomFileNameMap.java b/src/main/java/io/papermc/bibliothek/util/CustomFileNameMap.java deleted file mode 100644 index f2246a7..0000000 --- a/src/main/java/io/papermc/bibliothek/util/CustomFileNameMap.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * This file is part of bibliothek, licensed under the MIT License. - * - * Copyright (c) 2024 GeyserMC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package io.papermc.bibliothek.util; - -import java.net.FileNameMap; -import java.net.URLConnection; -import java.util.HashMap; -import java.util.Map; - -public class CustomFileNameMap implements FileNameMap { - private final FileNameMap internalMap = URLConnection.getFileNameMap(); - - private final Map customMap = new HashMap<>() {{ - put("mcpack", "application/zip"); - }}; - - public String getContentTypeFor(String fileName) { - return customMap.getOrDefault(getExtension(fileName), internalMap.getContentTypeFor(fileName)); - } - - private String getExtension(String fileName) { - int index = fileName.lastIndexOf('.'); - if (index == -1) { - return ""; - } - return fileName.substring(index + 1); - } -} diff --git a/src/main/java/io/papermc/bibliothek/util/HTTP.java b/src/main/java/io/papermc/bibliothek/util/HTTP.java index 0b8d267..0aa4300 100644 --- a/src/main/java/io/papermc/bibliothek/util/HTTP.java +++ b/src/main/java/io/papermc/bibliothek/util/HTTP.java @@ -34,6 +34,9 @@ import org.springframework.http.ResponseEntity; public final class HTTP { + public static final String APPLICATION_JAVA_ARCHIVE_VALUE = "application/java-archive"; + public static final MediaType APPLICATION_JAVA_ARCHIVE = new MediaType("application", "java-archive"); + private HTTP() { }