Increase timeout, check for errors just in case

This commit is contained in:
Nassim Jahnke 2023-03-24 12:00:31 +01:00
parent 2b3ef31335
commit 693a7310f1
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
3 changed files with 29 additions and 10 deletions

View File

@ -120,9 +120,10 @@ public class AdminController extends HangarComponent {
@PostMapping(path = "/updateHashes")
@PermissionRequired(NamedPermission.MANUAL_VALUE_CHANGES)
@RateLimit(overdraft = 1, refillSeconds = RateLimit.MAX_REFILL_DELAY, refillTokens = 1)
public void updateHashes() {
@ResponseBody
public List<String> updateHashes() {
try {
this.versionService.updateFileHashes();
return this.versionService.updateFileHashes();
} catch (final IOException e) {
throw new RuntimeException(e);
}

View File

@ -25,8 +25,8 @@ import io.papermc.hangar.service.internal.visibility.ProjectVisibilityService;
import io.papermc.hangar.util.CryptoUtils;
import io.papermc.hangar.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -62,22 +62,39 @@ public class VersionService extends HangarComponent {
}
@Transactional
public void updateFileHashes() throws IOException {
public List<String> updateFileHashes() throws IOException {
final List<String> errors = new ArrayList<>();
for (final ProjectVersionDownloadTable downloadTable : this.projectVersionDownloadsDAO.getDownloads()) {
if (downloadTable.getHash() == null) {
if (downloadTable.getHash() == null || downloadTable.getFileName() == null) {
continue;
}
final ProjectVersionTable versionTable = this.projectVersionsDAO.getProjectVersionTable(downloadTable.getVersionId());
final ProjectTable projectTable = this.projectsDAO.getById(versionTable.getProjectId());
final List<ProjectVersionPlatformDownloadTable> platformDownloads = this.projectVersionDownloadsDAO.getPlatformDownloads(versionTable.getVersionId(), downloadTable.getId());
final Platform platform = platformDownloads.get(0).getPlatform();
if (platformDownloads.isEmpty()) {
errors.add("No platform downloads for " + projectTable.getOwnerName() + "/" + projectTable.getSlug() + "/" + versionTable.getVersionString() + "/" + downloadTable.getFileName());
continue;
}
final byte[] bytes = this.fileService.bytes(this.projectFiles.getVersionDir(projectTable.getOwnerName(), projectTable.getSlug(),
versionTable.getVersionString(), platform, downloadTable.getFileName()));
final Platform platform = platformDownloads.get(0).getPlatform();
final String versionPath = this.projectFiles.getVersionDir(projectTable.getOwnerName(), projectTable.getSlug(),
versionTable.getVersionString(), platform, downloadTable.getFileName());
if (!this.fileService.exists(versionPath)) {
errors.add("File does not exist at path " + versionPath);
continue;
}
final byte[] bytes = this.fileService.bytes(versionPath);
final String hash = CryptoUtils.sha256ToHex(bytes);
if (hash == null) {
errors.add("Could not generate hash for " + versionPath);
continue;
}
this.projectVersionDownloadsDAO.updateHash(downloadTable.getId(), hash);
}
return errors;
}
public @Nullable ProjectVersionTable getProjectVersionTable(final Long versionId) {

View File

@ -95,7 +95,8 @@ async function saveRoles() {
async function updateHashes() {
loading.value = true;
try {
await useInternalApi("admin/updateHashes", "post");
const errors = await useInternalApi("admin/updateHashes", "post", undefined, { timeout: 30000 });
console.log(errors);
notification.success("Updated hashes!");
} catch (e: any) {
loading.value = false;
@ -174,7 +175,7 @@ async function updateHashes() {
<PageTitle>Update file hashes</PageTitle>
Update file hashes stored in db from md5 to sha256.
<br />
<Button button-type="red" @click="updateHashes" :disabled="loading">Run</Button>
<Button button-type="red" :disabled="loading" @click="updateHashes">Run</Button>
</Card>
</div>
</template>