Skip to content

Commit

Permalink
fix: make sure to generate s3 links with the right platform, if jars …
Browse files Browse the repository at this point in the history
…share platforms
  • Loading branch information
MiniDigger committed Oct 19, 2024
1 parent 43a8559 commit f04a253
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand All @@ -18,6 +19,7 @@
@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)")
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -69,7 +70,7 @@ public ResponseEntity<?> downloadVersion(final String project, final String vers
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);
}
Expand All @@ -78,7 +79,7 @@ public ResponseEntity<?> downloadVersion(final String project, final String vers
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)) {
Expand Down

0 comments on commit f04a253

Please sign in to comment.