Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for providing download from a CDN instead of local storage #99

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
package io.papermc.bibliothek;

import io.papermc.bibliothek.configuration.AppConfiguration;
import io.papermc.bibliothek.configuration.StorageConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;

@EnableConfigurationProperties({
AppConfiguration.class
AppConfiguration.class,
StorageConfiguration.class
})
@SpringBootApplication
@ServletComponentScan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,15 @@
*/
package io.papermc.bibliothek.configuration;

import jakarta.validation.constraints.NotNull;
import java.net.URL;
import java.nio.file.Path;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

@ConfigurationProperties(prefix = "app")
@Validated
public class AppConfiguration {
private URL apiBaseUrl;
private String apiTitle;
private String apiVersion;
private @NotNull Path storagePath;

@SuppressWarnings("checkstyle:MethodName")
public URL getApiBaseUrl() {
return this.apiBaseUrl;
}

@SuppressWarnings("checkstyle:MethodName")
public void setApiBaseUrl(final URL apiBaseUrl) {
this.apiBaseUrl = apiBaseUrl;
}

@SuppressWarnings("checkstyle:MethodName")
public String getApiTitle() {
return this.apiTitle;
}

@SuppressWarnings("checkstyle:MethodName")
public void setApiTitle(final String apiTitle) {
this.apiTitle = apiTitle;
}

@SuppressWarnings("checkstyle:MethodName")
public String getApiVersion() {
return this.apiVersion;
}

@SuppressWarnings("checkstyle:MethodName")
public void setApiVersion(final String apiVersion) {
this.apiVersion = apiVersion;
}

@SuppressWarnings("checkstyle:MethodName")
public Path getStoragePath() {
return this.storagePath;
}

@SuppressWarnings("checkstyle:MethodName")
public void setStoragePath(final Path storagePath) {
this.storagePath = storagePath;
}
public record AppConfiguration(
URL apiBaseUrl,
String apiTitle,
String apiVersion
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ OpenAPI openAPI(final AppConfiguration configuration) {
final OpenAPI api = new OpenAPI();
api.info(
new Info()
.title(configuration.getApiTitle())
.version(configuration.getApiVersion())
.title(configuration.apiTitle())
.version(configuration.apiVersion())
);
final URL apiBaseUrl = configuration.getApiBaseUrl();
final URL apiBaseUrl = configuration.apiBaseUrl();
if (apiBaseUrl != null) {
api.servers(List.of(new Server().url(apiBaseUrl.toExternalForm())));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of bibliothek, licensed under the MIT License.
*
* Copyright (c) 2019-2024 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
* 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.configuration;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.nio.file.Path;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

@ConfigurationProperties(prefix = "app.storage")
@Validated
public record StorageConfiguration(
@NotNull Path cache,
@NotEmpty List<Source> sources
) {
public record Source(
String name,
Type type,
String value
) {
public enum Type {
LOCAL,
REMOTE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package io.papermc.bibliothek.controller.v2;

import io.papermc.bibliothek.configuration.AppConfiguration;
import io.papermc.bibliothek.database.model.Build;
import io.papermc.bibliothek.database.model.Project;
import io.papermc.bibliothek.database.model.Version;
Expand All @@ -35,6 +34,7 @@
import io.papermc.bibliothek.exception.DownloadNotFound;
import io.papermc.bibliothek.exception.ProjectNotFound;
import io.papermc.bibliothek.exception.VersionNotFound;
import io.papermc.bibliothek.service.DownloadService;
import io.papermc.bibliothek.util.HTTP;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -48,6 +48,7 @@
import java.time.Duration;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
Expand All @@ -64,22 +65,22 @@
@SuppressWarnings("checkstyle:FinalClass")
public class DownloadController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofDays(7));
private final AppConfiguration configuration;
private final ProjectCollection projects;
private final VersionCollection versions;
private final BuildCollection builds;
private final DownloadService service;

@Autowired
private DownloadController(
final AppConfiguration configuration,
final ProjectCollection projects,
final VersionCollection versions,
final BuildCollection builds
final BuildCollection builds,
final DownloadService service
) {
this.configuration = configuration;
this.projects = projects;
this.versions = versions;
this.builds = builds;
this.service = service;
}

@ApiResponse(
Expand Down Expand Up @@ -132,17 +133,12 @@ public ResponseEntity<?> download(
final Version version = this.versions.findByProjectAndName(project._id(), versionName).orElseThrow(VersionNotFound::new);
final Build build = this.builds.findByProjectAndVersionAndNumber(project._id(), version._id(), buildNumber).orElseThrow(BuildNotFound::new);

for (final Map.Entry<String, Build.Download> download : build.downloads().entrySet()) {
if (download.getValue().name().equals(downloadName)) {
for (final Map.Entry<String, Build.Download> entry : build.downloads().entrySet()) {
final Build.Download download = entry.getValue();
if (download.name().equals(downloadName)) {
try {
return new JavaArchive(
this.configuration.getStoragePath()
.resolve(project.name())
.resolve(version.name())
.resolve(String.valueOf(build.number()))
.resolve(download.getValue().name()),
CACHE
);
final Path path = this.service.resolve(project, version, build, download);
return JavaArchive.forPath(download, path, CACHE);
} catch (final IOException e) {
throw new DownloadFailed(e);
}
Expand All @@ -151,17 +147,23 @@ public ResponseEntity<?> download(
throw new DownloadNotFound();
}

private static class JavaArchive extends ResponseEntity<FileSystemResource> {
JavaArchive(final Path path, final CacheControl cache) throws IOException {
super(new FileSystemResource(path), headersFor(path, cache), HttpStatus.OK);
private static class JavaArchive extends ResponseEntity<AbstractResource> {
static JavaArchive forPath(final Build.Download download, final Path path, final CacheControl cache) throws IOException {
final FileSystemResource resource = new FileSystemResource(path);
final HttpHeaders headers = headersFor(download, cache);
headers.setLastModified(Files.getLastModifiedTime(path).toInstant());
return new JavaArchive(resource, headers);
}

private JavaArchive(final AbstractResource resource, final HttpHeaders headers) {
super(resource, headers, HttpStatus.OK);
}

private static HttpHeaders headersFor(final Path path, final CacheControl cache) throws IOException {
private static HttpHeaders headersFor(final Build.Download download, final CacheControl cache) {
final HttpHeaders headers = new HttpHeaders();
headers.setCacheControl(cache);
headers.setContentDisposition(HTTP.attachmentDisposition(path.getFileName()));
headers.setContentDisposition(HTTP.attachmentDisposition(download.name()));
headers.setContentType(HTTP.APPLICATION_JAVA_ARCHIVE);
headers.setLastModified(Files.getLastModifiedTime(path).toInstant());
return headers;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static VersionFamilyBuildsResponse from(final Project project, final VersionFami
}

@Schema
public static record VersionFamilyBuild(
public record VersionFamilyBuild(
@Schema(name = "version", pattern = Version.PATTERN, example = "1.18")
String version,
@Schema(name = "build", pattern = "\\d+", example = "10")
Expand Down
Loading