11package net .modgarden .gardenbot .util ;
22
33import net .modgarden .gardenbot .GardenBot ;
4+ import org .jetbrains .annotations .Nullable ;
45
56import java .io .File ;
67import java .io .IOException ;
1112import java .nio .file .Files ;
1213import java .nio .file .Path ;
1314import java .nio .file .Paths ;
15+ import java .util .List ;
16+ import java .util .Optional ;
1417
1518/// Copied from the backend.
1619public 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