private LocalPath CacheFileLocally()

in dotnet/Utils/RichPath.cs [362:403]


        private LocalPath CacheFileLocally()
        {
            string storedEtag = null;
            if (File.Exists(CachedFileEtagPath))
            {
                storedEtag = File.ReadAllText(CachedFileEtagPath);
            }

            var tmpPath = CachedFilePath + Guid.NewGuid();
            try
            {
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(CachedFilePath));
                // Download blob to a tmp file, to avoid overwriting a potentially existing copy:
                var blobReference = blobContainerClient.GetBlobReference(Path);
                blobReference.DownloadToFile(
                    tmpPath,
                    FileMode.Create,
                    new AccessCondition { IfNoneMatchETag = storedEtag });
                // If download succeeded (no exception), remove old file if it exists and put new file in place:
                if (File.Exists(CachedFilePath)) {
                    File.Delete(CachedFilePath);
                }
                File.Move(tmpPath, CachedFilePath);
                File.WriteAllText(CachedFileEtagPath, blobReference.Properties.ETag);
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode != 304) // 304 ~ Not modified, i.e., we are fine
                {
                    throw;
                }
            }
            finally
            { 
                if (File.Exists(tmpPath))
                {
                    File.Delete(tmpPath);
                }
            }

            return new LocalPath(CachedFilePath);
        }