From 69afd5295540b29d632c1dc31ca42a3f09248625 Mon Sep 17 00:00:00 2001
From: Cgarcia <Cgarcia@10.20.7.15>
Date: Wed, 5 Jul 2023 09:20:24 -0700
Subject: [PATCH] improve the engines download

---
 .../downloading/DeepLearningVersionDownloader.java  | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/bioimageanalysis/icy/deeplearning/versionmanager/downloading/DeepLearningVersionDownloader.java b/src/main/java/org/bioimageanalysis/icy/deeplearning/versionmanager/downloading/DeepLearningVersionDownloader.java
index c6f5626..e55cfc8 100644
--- a/src/main/java/org/bioimageanalysis/icy/deeplearning/versionmanager/downloading/DeepLearningVersionDownloader.java
+++ b/src/main/java/org/bioimageanalysis/icy/deeplearning/versionmanager/downloading/DeepLearningVersionDownloader.java
@@ -253,7 +253,6 @@ public class DeepLearningVersionDownloader
     private static Path downloadFile(String urlString, Path targetDirectory, ProgressListener progressListener)
             throws IOException
     {
-
         URL url = new URL(urlString);
         Path localFilePath = toLocalVersionFile(targetDirectory, urlString);
         Files.deleteIfExists(localFilePath);
@@ -261,8 +260,16 @@ public class DeepLearningVersionDownloader
         try
         {
             HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection());
-            long completeFileSize = httpConnection.getContentLength();
-
+            while (httpConnection.getResponseCode() == 302) {
+            	url = new URL(httpConnection.getHeaderField("Location"));
+            	httpConnection = (HttpURLConnection) (url.openConnection());
+            }
+            long completeFileSize = httpConnection.getContentLengthLong();
+            if (completeFileSize == -1) {
+            	progressListener.notifyProgress(1, 1);
+            	return downloadFile(urlString, targetDirectory);
+            }
+            	completeFileSize = 1;
             try (BufferedInputStream in = new BufferedInputStream(httpConnection.getInputStream()))
             {
                 FileOutputStream fos = new FileOutputStream(localFilePath.toFile());
-- 
GitLab