From f04a253f58ff399fddc2a79352ca717e99aef067 Mon Sep 17 00:00:00 2001 From: MiniDigger | Martin Date: Sat, 19 Oct 2024 15:57:51 +0200 Subject: [PATCH] fix: make sure to generate s3 links with the right platform, if jars share platforms --- .../downloads/ProjectVersionDownloadsDAO.java | 22 +++++++++++++------ .../ProjectVersionDownloadTable.java | 2 +- ...ojectVersionDownloadTableWithPlatform.java | 17 ++++++++++++++ .../internal/versions/DownloadService.java | 5 +++-- 4 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 backend/src/main/java/io/papermc/hangar/model/db/versions/downloads/ProjectVersionDownloadTableWithPlatform.java diff --git a/backend/src/main/java/io/papermc/hangar/db/dao/internal/table/versions/downloads/ProjectVersionDownloadsDAO.java b/backend/src/main/java/io/papermc/hangar/db/dao/internal/table/versions/downloads/ProjectVersionDownloadsDAO.java index 306642cd..65861842 100644 --- a/backend/src/main/java/io/papermc/hangar/db/dao/internal/table/versions/downloads/ProjectVersionDownloadsDAO.java +++ b/backend/src/main/java/io/papermc/hangar/db/dao/internal/table/versions/downloads/ProjectVersionDownloadsDAO.java @@ -2,6 +2,7 @@ package io.papermc.hangar.db.dao.internal.table.versions.downloads; import io.papermc.hangar.model.common.Platform; import io.papermc.hangar.model.db.versions.downloads.ProjectVersionDownloadTable; +import io.papermc.hangar.model.db.versions.downloads.ProjectVersionDownloadTableWithPlatform; import io.papermc.hangar.model.db.versions.downloads.ProjectVersionPlatformDownloadTable; import java.util.Collection; import java.util.List; @@ -18,6 +19,7 @@ import org.jdbi.v3.sqlobject.statement.SqlUpdate; @JdbiRepository @RegisterConstructorMapper(ProjectVersionPlatformDownloadTable.class) @RegisterConstructorMapper(ProjectVersionDownloadTable.class) +@RegisterConstructorMapper(ProjectVersionDownloadTableWithPlatform.class) public interface ProjectVersionDownloadsDAO { @SqlBatch("INSERT INTO project_version_platform_downloads (version_id, platform, download_id) VALUES (:versionId, :platform, :downloadId)") @@ -53,15 +55,21 @@ public interface ProjectVersionDownloadsDAO { @SqlQuery("SELECT * FROM project_version_downloads WHERE version_id = :versionId AND id = :downloadId") ProjectVersionDownloadTable getDownload(long versionId, long downloadId); + // we need to find the first version platform download for the given download ID, since only that is being uploaded to object storage + // see comment in VersionFactory#processPendingVersionFile @SqlQuery(""" - SELECT pvd.* + SELECT pvd.*, + (SELECT platform + FROM project_version_platform_downloads first + WHERE first.download_id = pvpd.download_id + LIMIT 1) AS platform FROM project_version_downloads pvd - JOIN project_versions pv ON pv.id = pvd.version_id - JOIN projects p ON pv.project_id = p.id JOIN project_version_platform_downloads pvpd ON pvd.id = pvpd.download_id - WHERE p.id = :projectId - AND pv.version_string = :versionString - AND pvpd.platform = :platform + WHERE pvpd.version_id = (SELECT id + FROM project_versions pv + WHERE pv.project_id = :projectId + AND pv.version_string = :versionString) + AND pvpd.platform = :platform; """) - ProjectVersionDownloadTable getDownloadByPlatform(long projectId, String versionString, @EnumByOrdinal Platform platform); + ProjectVersionDownloadTableWithPlatform getDownloadByPlatform(long projectId, String versionString, @EnumByOrdinal Platform platform); } diff --git a/backend/src/main/java/io/papermc/hangar/model/db/versions/downloads/ProjectVersionDownloadTable.java b/backend/src/main/java/io/papermc/hangar/model/db/versions/downloads/ProjectVersionDownloadTable.java index ff947510..cb103830 100644 --- a/backend/src/main/java/io/papermc/hangar/model/db/versions/downloads/ProjectVersionDownloadTable.java +++ b/backend/src/main/java/io/papermc/hangar/model/db/versions/downloads/ProjectVersionDownloadTable.java @@ -3,7 +3,7 @@ package io.papermc.hangar.model.db.versions.downloads; import io.papermc.hangar.model.db.Table; import org.jdbi.v3.core.mapper.reflect.JdbiConstructor; -public final class ProjectVersionDownloadTable extends Table { +public class ProjectVersionDownloadTable extends Table { private final long versionId; private final Long fileSize; diff --git a/backend/src/main/java/io/papermc/hangar/model/db/versions/downloads/ProjectVersionDownloadTableWithPlatform.java b/backend/src/main/java/io/papermc/hangar/model/db/versions/downloads/ProjectVersionDownloadTableWithPlatform.java new file mode 100644 index 00000000..5293c184 --- /dev/null +++ b/backend/src/main/java/io/papermc/hangar/model/db/versions/downloads/ProjectVersionDownloadTableWithPlatform.java @@ -0,0 +1,17 @@ +package io.papermc.hangar.model.db.versions.downloads; + +import io.papermc.hangar.model.common.Platform; + +public class ProjectVersionDownloadTableWithPlatform extends ProjectVersionDownloadTable { + + private final Platform platform; + + public ProjectVersionDownloadTableWithPlatform(final long id, final long versionId, final Long fileSize, final String hash, final String fileName, final String externalUrl, final Platform platform) { + super(id, versionId, fileSize, hash, fileName, externalUrl); + this.platform = platform; + } + + public Platform getPlatform() { + return this.platform; + } +} diff --git a/backend/src/main/java/io/papermc/hangar/service/internal/versions/DownloadService.java b/backend/src/main/java/io/papermc/hangar/service/internal/versions/DownloadService.java index c9abecd9..39e7e199 100644 --- a/backend/src/main/java/io/papermc/hangar/service/internal/versions/DownloadService.java +++ b/backend/src/main/java/io/papermc/hangar/service/internal/versions/DownloadService.java @@ -8,6 +8,7 @@ import io.papermc.hangar.model.api.project.version.PlatformVersionDownload; import io.papermc.hangar.model.common.Platform; import io.papermc.hangar.model.db.projects.ProjectTable; import io.papermc.hangar.model.db.versions.downloads.ProjectVersionDownloadTable; +import io.papermc.hangar.model.db.versions.downloads.ProjectVersionDownloadTableWithPlatform; import io.papermc.hangar.model.db.versions.downloads.ProjectVersionPlatformDownloadTable; import io.papermc.hangar.service.internal.file.FileService; import io.papermc.hangar.service.internal.file.S3FileService; @@ -69,7 +70,7 @@ public class DownloadService extends HangarComponent { throw new HangarApiException(HttpStatus.NOT_FOUND); } - final ProjectVersionDownloadTable download = this.downloadsDAO.getDownloadByPlatform(projectTable.getProjectId(), versionString, platform); + final ProjectVersionDownloadTableWithPlatform download = this.downloadsDAO.getDownloadByPlatform(projectTable.getProjectId(), versionString, platform); if (download == null) { throw new HangarApiException(HttpStatus.NOT_FOUND); } @@ -78,7 +79,7 @@ public class DownloadService extends HangarComponent { if (StringUtils.hasText(download.getExternalUrl())) { return ResponseEntity.status(301).header("Location", download.getExternalUrl()).build(); } else if (this.fileService instanceof S3FileService){ - return ResponseEntity.status(301).header("Location", this.fileService.getVersionDownloadUrl(ownerName, projectTable.getName(), versionString, platform, download.getFileName())).build(); + return ResponseEntity.status(301).header("Location", this.fileService.getVersionDownloadUrl(ownerName, projectTable.getName(), versionString, download.getPlatform(), download.getFileName())).build(); } else { final String path = this.projectFiles.getVersionDir(ownerName, projectTable.getName(), versionString, platform, download.getFileName()); if (!this.fileService.exists(path)) {