Skip to content

Commit

Permalink
Merge remote-tracking branch 'PaperMC/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Feb 18, 2024
2 parents e6735d7 + b57ea18 commit a7cd3aa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,25 @@
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;
import org.springframework.http.HttpHeaders;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, MediaType> 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);
};
}
}

0 comments on commit a7cd3aa

Please sign in to comment.