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..7ef1d93 100644 --- a/src/main/java/io/papermc/bibliothek/controller/v2/DownloadController.java +++ b/src/main/java/io/papermc/bibliothek/controller/v2/DownloadController.java @@ -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; @@ -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; @@ -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; } diff --git a/src/main/java/io/papermc/bibliothek/util/HTTP.java b/src/main/java/io/papermc/bibliothek/util/HTTP.java index 0b8d267..d9a381f 100644 --- a/src/main/java/io/papermc/bibliothek/util/HTTP.java +++ b/src/main/java/io/papermc/bibliothek/util/HTTP.java @@ -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 { diff --git a/src/main/java/io/papermc/bibliothek/util/CustomFileNameMap.java b/src/main/java/io/papermc/bibliothek/util/MediaTypeMap.java similarity index 68% rename from src/main/java/io/papermc/bibliothek/util/CustomFileNameMap.java rename to src/main/java/io/papermc/bibliothek/util/MediaTypeMap.java index f2246a7..1c70b28 100644 --- a/src/main/java/io/papermc/bibliothek/util/CustomFileNameMap.java +++ b/src/main/java/io/papermc/bibliothek/util/MediaTypeMap.java @@ -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 @@ -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 customMap = new HashMap<>() {{ - put("mcpack", "application/zip"); - }}; +public class MediaTypeMap { + private final Map 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 ""; }