Commit e02fee32 authored by Jérémy Lacoche's avatar Jérémy Lacoche
Browse files

File download safe multiple download at the same time, prevent images from the...

File download safe multiple download at the same time, prevent images from the same url to be downloaded several times
parent 25a53c50
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -22,9 +22,10 @@ public class WorldAnalysisARFoundationHelper
            string folder = Application.persistentDataPath;
#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);
    }
+13 −3
Original line number Diff line number Diff line
@@ -19,6 +19,10 @@ public class WorldAnalysisARFoundationModuleImage : WorldAnalysisARFoundationMod
    /// </summary>
    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>
    private Dictionary<string,TrackableInfo> m_trackedImages = new Dictionary<string,TrackableInfo>();
@@ -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;
    }
@@ -171,10 +176,14 @@ public class WorldAnalysisARFoundationModuleImage : WorldAnalysisARFoundationMod
        }

        if (!found)
        {
            // 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 
        {
            //Load texture and add it to arfoundation library
@@ -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);
    }