Skip to content

Commit 67e08f2

Browse files
committed
fix: follow redirects in direct download URLs
1 parent ccd47b0 commit 67e08f2

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = 2.0.0
1+
version = 2.0.1

src/main/java/net/modgarden/gardenbot/GardenBot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
public class GardenBot {
2222
public static final Logger LOG = LoggerFactory.getLogger("GardenBot");
23-
public static final String VERSION = "2.0.0"; // TODO: Automatically update this from gradle.properties.
23+
public static final String VERSION = "2.0.1"; // TODO: Automatically update this from gradle.properties.
2424
public static final Gson GSON = new GsonBuilder()
2525
.registerTypeAdapter(NullableWrapper.class, new NullableWrapper.Serializer())
2626
.create();

src/main/java/net/modgarden/gardenbot/util/FileUtils.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.modgarden.gardenbot.util;
22

33
import net.modgarden.gardenbot.GardenBot;
4+
import org.jetbrains.annotations.Nullable;
45

56
import java.io.File;
67
import java.io.IOException;
@@ -11,24 +12,38 @@
1112
import java.nio.file.Files;
1213
import java.nio.file.Path;
1314
import java.nio.file.Paths;
15+
import java.util.List;
16+
import java.util.Optional;
1417

1518
/// Copied from the backend.
1619
public class FileUtils {
1720
private static final String USER_AGENT = "ModGardenEvent/gardenbot/" + GardenBot.VERSION + " (modgarden.net)";
1821

1922
public static File download(URI uri) throws IOException, InterruptedException {
23+
return download(uri, null);
24+
}
25+
26+
public static File download(URI uri, @Nullable String fileName) throws IOException, InterruptedException {
2027
HttpRequest request = HttpRequest.newBuilder()
2128
.header("User-Agent", USER_AGENT)
2229
.uri(uri)
2330
.build();
24-
String fileName = getFileName(uri);
31+
32+
if (fileName == null) {
33+
fileName = getFileName(uri);
34+
}
2535

2636
Path temporaryFolder = Path.of("./.tmp")
2737
.resolve(fileName);
2838

2939
Files.createDirectories(temporaryFolder.getParent());
3040

3141
HttpResponse<Path> response = GardenBot.HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofFile(temporaryFolder));
42+
Optional<String> location = response.headers().firstValue("Location");
43+
44+
if (response.statusCode() - 300 > 1 && response.statusCode() - 300 < 100 && location.isPresent()) {
45+
return download(URI.create(location.get()), fileName);
46+
}
3247

3348
return response.body().toFile();
3449
}
@@ -46,7 +61,7 @@ public static boolean isJar(File file) {
4661
return false;
4762
}
4863

49-
public static void cleanupTmpFolder(File temporaryFile) {
64+
public static void cleanupTmpFolder(File temporaryFile) {
5065
try {
5166
Path temporaryFilePath = temporaryFile.toPath();
5267
if (Files.deleteIfExists(temporaryFilePath)) {

0 commit comments

Comments
 (0)