Skip to content

Commit

Permalink
modrinth: sanitize modpack file's path (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg authored Oct 10, 2023
1 parent aa062a7 commit 0f86ef1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
16 changes: 14 additions & 2 deletions src/main/java/me/itzg/helpers/modrinth/ModrinthPackInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ private Flux<Path> processModpackFiles(ModpackIndex modpackIndex) {
)
.publishOn(Schedulers.boundedElastic())
.flatMap(modpackFile -> {
final String modpackFilePath = sanitizeModFilePath(modpackFile.getPath());
final Path outFilePath =
this.outputDirectory.resolve(modpackFile.getPath());
this.outputDirectory.resolve(modpackFilePath);
try {
//noinspection BlockingMethodInNonBlockingContext
Files.createDirectories(outFilePath.getParent());
Expand All @@ -126,11 +127,22 @@ private Flux<Path> processModpackFiles(ModpackIndex modpackIndex) {
outFilePath,
modpackFile.getDownloads().get(0),
(uri, file, contentSizeBytes) ->
log.info("Downloaded {}", modpackFile.getPath())
log.info("Downloaded {}", modpackFilePath)
);
});
}

private String sanitizeModFilePath(String path) {
// Using only backslash delimiters and not forward slashes?
// (mixed usage will assume backslashes were purposeful)
if (path.contains("\\") && !path.contains("/")) {
return path.replace("\\", "/");
}
else {
return path;
}
}

@SuppressWarnings("SameParameterValue")
private Stream<Path> extractOverrides(String... overridesDirs) {
try (ZipFile zipFileReader = new ZipFile(zipFile.toFile())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void downloadsAndInstallsModrinthModpack(
String expectedFileData = "some test data";
String relativeFilePath = "test_file";
ModpackFile testFile = createHostedModpackFile(
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl());

ModpackIndex index = createBasicModpackIndex();
index.getFiles().add(testFile);
Expand All @@ -74,7 +74,7 @@ void downloadsAndInstallsModrinthModpack_versionNumberAndAnyLoader(
String expectedFileData = "some test data";
String relativeFilePath = "test_file";
ModpackFile testFile = createHostedModpackFile(
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl());

ModpackIndex index = createBasicModpackIndex();
index.getFiles().add(testFile);
Expand Down Expand Up @@ -161,7 +161,7 @@ void removesFilesNoLongerNeedeByUpdatedModpack(
String expectedFileData = "some test data";
String relativeFilePath = "test_file";
ModpackFile testFile = createHostedModpackFile(
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl());

ModpackIndex index = createBasicModpackIndex();
index.getFiles().add(testFile);
Expand Down Expand Up @@ -201,7 +201,7 @@ void downloadsAndInstallsGenericModpacksOverHttp(
String relativeFilePath = "test_file";
String modpackDownloadPath = "/files/modpacks/test_modpack-1.0.0.mrpack";
ModpackFile testFile = createHostedModpackFile(
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl());

ModpackIndex index = createBasicModpackIndex();
index.getFiles().add(testFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,44 @@ void installDownloadsDependentFilesToInstallation(

ModpackIndex index = createBasicModpackIndex();
index.getFiles().add(createHostedModpackFile(
relativeFilePath, expectedFileData, wm.getHttpBaseUrl()));
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl()));

Files.write(modpackPath, createModrinthPack(index));

ModrinthPackInstaller installerUT = new ModrinthPackInstaller(
apiClient, fetchOpts, modpackPath, tempDir, resultsFile, false);

final Installation installation = installerUT.processModpack(sharedFetch).block();

assertThat(installation).isNotNull();
List<Path> installedFiles = installation.getFiles();

assertThat(expectedFilePath).isRegularFile();
assertThat(expectedFilePath).content()
.isEqualTo(expectedFileData);
assertThat(installedFiles.size()).isEqualTo(1);
assertThat(installedFiles.get(0)).isEqualTo(expectedFilePath);
}
}

@Test
void sanitizesModFilePath(
WireMockRuntimeInfo wm, @TempDir Path tempDir
) throws IOException, URISyntaxException
{
Options fetchOpts = new SharedFetchArgs().options();
try (SharedFetch sharedFetch = Fetch.sharedFetch("install-modrinth-modpack", fetchOpts)) {
ModrinthApiClient apiClient = new ModrinthApiClient(
wm.getHttpBaseUrl(), "install-modrinth-modpack", fetchOpts);

String expectedFileData = "some test data";
Path expectedFilePath = tempDir.resolve("mods/mod.jar");
Path resultsFile = tempDir.resolve("results");
Path modpackPath = tempDir.resolve("test.mrpack");

ModpackIndex index = createBasicModpackIndex();
index.getFiles().add(createHostedModpackFile(
"mods\\mod.jar", "mods/mod.jar", expectedFileData, wm.getHttpBaseUrl()));

Files.write(modpackPath, createModrinthPack(index));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ static byte[] createModrinthPack(ModpackIndex index) throws IOException {
}

static ModpackIndex.ModpackFile createHostedModpackFile(
String relativeFileLocation, String data, String wmBaseUrl
String relativeFileLocation, String urlSubpath, String data, String wmBaseUrl
) throws URISyntaxException
{
stubFor(get("/files/" + relativeFileLocation)
stubFor(get("/files/" + urlSubpath)
.willReturn(ok().withBody(data)));

ModpackIndex.ModpackFile modpackFile = new ModpackIndex.ModpackFile()
.setDownloads(new ArrayList<>())
.setPath(relativeFileLocation);
modpackFile.getDownloads().add(
new URI(wmBaseUrl + "/files/" + relativeFileLocation));
new URI(wmBaseUrl + "/files/" + urlSubpath));

return modpackFile;
}
Expand Down

0 comments on commit 0f86ef1

Please sign in to comment.