Skip to content

Commit d84186d

Browse files
authored
cf: check downloads repo before attempting download of mod in modpack (#449)
1 parent a0b8521 commit d84186d

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

src/main/java/me/itzg/helpers/curseforge/CurseForgeApiClient.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,19 @@ public Mono<CurseForgeFile> getModFileInfo(
218218
}
219219
return e;
220220
})
221-
.map(GetModFileResponse::getData);
221+
.map(GetModFileResponse::getData)
222+
.checkpoint();
222223
}
223224

224225
public Mono<Path> download(CurseForgeFile cfFile, Path outputFile, FileDownloadStatusHandler handler) {
225-
return preparedFetch.fetch(
226-
cfFile.getDownloadUrl() != null ?
227-
normalizeDownloadUrl(cfFile.getDownloadUrl())
228-
: downloadFallbackUriBuilder.resolve(
229-
"/v1/mods/{modId}/files/{fileId}/download",
230-
cfFile.getModId(), cfFile.getId()
231-
)
232-
)
226+
final URI uriToDownload = cfFile.getDownloadUrl() != null ?
227+
normalizeDownloadUrl(cfFile.getDownloadUrl())
228+
: downloadFallbackUriBuilder.resolve(
229+
"/v1/mods/{modId}/files/{fileId}/download",
230+
cfFile.getModId(), cfFile.getId()
231+
);
232+
log.trace("Downloading cfFile={} from normalizedUrl={}", cfFile, uriToDownload);
233+
return preparedFetch.fetch(uriToDownload)
233234
.toFile(outputFile)
234235
.skipExisting(true)
235236
.handleStatus(handler)

src/main/java/me/itzg/helpers/curseforge/CurseForgeInstaller.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import me.itzg.helpers.json.ObjectMappers;
5757
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
5858
import org.apache.commons.compress.archivers.zip.ZipFile;
59+
import org.jetbrains.annotations.Nullable;
5960
import reactor.core.publisher.Flux;
6061
import reactor.core.publisher.Mono;
6162
import reactor.core.scheduler.Schedulers;
@@ -435,17 +436,19 @@ private Path locateWorldZipInRepo(String fileName) {
435436
return locateFileIn(fileName, downloadsRepo, downloadsRepo.resolve(REPO_SUBDIR_WORLDS));
436437
}
437438

438-
private static Path locateFileIn(String fileName, Path... dirs) {
439+
private static Path locateFileIn(String fileName, @Nullable Path... dirs) {
439440
for (Path dir : dirs) {
440-
final Path resolved = dir.resolve(fileName);
441-
if (Files.exists(resolved)) {
442-
return resolved;
443-
}
441+
if (dir != null) {
442+
final Path resolved = dir.resolve(fileName);
443+
if (Files.exists(resolved)) {
444+
return resolved;
445+
}
444446

445-
// When downloading, the browser may replace spaces with +'s
446-
final Path altResolved = dir.resolve(fileName.replace(' ', '+'));
447-
if (Files.exists(altResolved)) {
448-
return altResolved;
447+
// When downloading, the browser may replace spaces with +'s
448+
final Path altResolved = dir.resolve(fileName.replace(' ', '+'));
449+
if (Files.exists(altResolved)) {
450+
return altResolved;
451+
}
449452
}
450453
}
451454
return null;
@@ -581,6 +584,7 @@ private ModPackResults processModpack(InstallContext context,
581584
excludeIncludeIds.getForceIncludeIds(),
582585
context.categoryInfo
583586
)
587+
.checkpoint()
584588
)
585589
.collectList()
586590
.block();
@@ -741,6 +745,7 @@ else if (category.getSlug().equals("worlds")) {
741745
final Mono<ResolveResult> resolvedFileMono =
742746
Mono.defer(() ->
743747
downloadOrResolveFile(context, modInfo, isWorld, outputDir, cfFile)
748+
.checkpoint()
744749
)
745750
// retry the deferred part above if one of the expected failure cases
746751
.retryWhen(
@@ -795,7 +800,10 @@ private Mono<ResolveResult> downloadOrResolveFile(InstallContext context, CurseF
795800

796801
// Will try to locate an existing file by alternate names that browser might create,
797802
// but only for non-world files of the modpack
798-
final Path locatedFile = !isWorld ? locateFileIn(cfFile.getFileName(), outputDir) : null;
803+
final Path locatedFile = !isWorld ? locateFileIn(cfFile.getFileName(),
804+
outputDir,
805+
downloadsRepo
806+
) : null;
799807

800808
if (locatedFile != null) {
801809
log.info("Mod file {} already exists", locatedFile);

src/main/java/me/itzg/helpers/curseforge/FileHashVerifier.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.EnumMap;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.stream.Collectors;
89
import lombok.extern.slf4j.Slf4j;
910
import me.itzg.helpers.curseforge.model.FileHash;
1011
import me.itzg.helpers.curseforge.model.HashAlgo;
@@ -24,6 +25,10 @@ public class FileHashVerifier {
2425
}
2526

2627
public static Mono<Path> verify(Path file, List<FileHash> hashes) {
28+
if (hashes.isEmpty()) {
29+
return Mono.just(file);
30+
}
31+
2732
for (final FileHash hash : hashes) {
2833
final ChecksumAlgo checksumAlgo = algos.get(hash.getAlgo());
2934
if (checksumAlgo != null) {
@@ -42,6 +47,10 @@ public static Mono<Path> verify(Path file, List<FileHash> hashes) {
4247
}
4348
}
4449

45-
return Mono.error(new IllegalArgumentException("Unable to find compatible checksum algorithm"));
50+
return Mono.error(new IllegalArgumentException("Unable to find compatible checksum algorithm from " +
51+
hashes.stream()
52+
.map(FileHash::getAlgo)
53+
.collect(Collectors.toList())
54+
));
4655
}
4756
}

0 commit comments

Comments
 (0)