From e02fee326ba02a276394577f8e5b6e2d7ef42665 Mon Sep 17 00:00:00 2001
From: jlacoche <jeremy.lacoche@orange.com>
Date: Wed, 26 Jun 2024 09:48:02 +0200
Subject: [PATCH] File download safe multiple download at the same time,
 prevent images from the same url to be downloaded several times

---
 .../Scripts/WorldAnalysisARFoundationHelper.cs   | 11 ++++++-----
 .../WorldAnalysisARFoundationModuleImage.cs      | 16 +++++++++++++---
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/Runtime/Scripts/WorldAnalysisARFoundationHelper.cs b/Runtime/Scripts/WorldAnalysisARFoundationHelper.cs
index d1b128c..f3bb177 100644
--- a/Runtime/Scripts/WorldAnalysisARFoundationHelper.cs
+++ b/Runtime/Scripts/WorldAnalysisARFoundationHelper.cs
@@ -18,13 +18,14 @@ public class WorldAnalysisARFoundationHelper
         string fileName = "";
         #if UNITY_EDITOR
             string folder = Application.streamingAssetsPath;
-        #else 
+#else
             string folder = Application.persistentDataPath;
-        #endif
-        
+#endif
+
+        string uniqueFileName = "/Temp"+ System.Guid.NewGuid()+".data";
         using (var client = new WebClient())
         {
-            await client.DownloadFileTaskAsync(toLoad, folder + "/Temp.data");
+            await client.DownloadFileTaskAsync(toLoad, folder + uniqueFileName);
             string header_contentDisposition = client.ResponseHeaders["content-disposition"];
             if (header_contentDisposition == null)
             {
@@ -40,7 +41,7 @@ public class WorldAnalysisARFoundationHelper
             {
                 File.Delete(filePath);
             }
-            File.Move(folder + "/Temp.data", filePath);
+            File.Move(folder + uniqueFileName, filePath);
         }
         return new KeyValuePair<string, string>(filePath , fileName);
     }
diff --git a/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs b/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs
index 60c1865..bba58d8 100644
--- a/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs
+++ b/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs
@@ -17,7 +17,11 @@ public class WorldAnalysisARFoundationModuleImage : WorldAnalysisARFoundationMod
     /// <summary>
     /// Name of all images that have been added to the library
     /// </summary>
-    private List<string> m_trackedImageInLibrary; 
+    private List<string> m_trackedImageInLibrary;
+    /// <summary>
+    /// All url of images that have allready been downloaded
+    /// </summary>
+    private List<string> m_allDownloadedImages;
     /// <summary>
     /// List of tracked images with tracking infos
     /// </summary>
@@ -35,6 +39,7 @@ public class WorldAnalysisARFoundationModuleImage : WorldAnalysisARFoundationMod
         XROrigin origin = UnityEngine.Object.FindAnyObjectByType<XROrigin>();
         m_trackedImageManager = origin.gameObject.AddComponent<ARTrackedImageManager>();
         m_trackedImageInLibrary = new List<string>();
+        m_allDownloadedImages = new List<string>();
         m_trackedImageManager.trackedImagePrefab = (GameObject)Resources.Load("ARFImageTrackingPrefab");
         m_trackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
     }
@@ -172,8 +177,12 @@ public class WorldAnalysisARFoundationModuleImage : WorldAnalysisARFoundationMod
 
         if (!found)
         {
-            // Here we don't check if url exists and still return true: could be improve
-            LoadTextureFromURL(url, fileName, imageWidthInMeters);
+            // Do not download the same image twice
+            if (!m_allDownloadedImages.Contains(url))
+            {
+                // Here we don't check if url exists and still return true: could be improve
+                LoadTextureFromURL(url, fileName, imageWidthInMeters);
+            }
         }
         else 
         {
@@ -206,6 +215,7 @@ public class WorldAnalysisARFoundationModuleImage : WorldAnalysisARFoundationMod
     public async void LoadTextureFromURL(string url, string fileName, float imageWidthInMeters)
     {
         Debug.Log("Download image from url "+ url);
+        m_allDownloadedImages.Add(url);
         KeyValuePair<string , string> downloaded = await WorldAnalysisARFoundationHelper.DownloadFileHTTP(url);
         LoadTextureFromMemory(downloaded.Key, fileName, imageWidthInMeters);
     }
-- 
GitLab