umbraco-community / UmbracoFileSystemProviders.Azure

:cloud: An Azure Blob Storage IFileSystem provider for Umbraco
96 stars 67 forks source link

FileSystemVirtualFile doesn't set etag and last modified from Azure on cache header #170

Open DominicFarrington opened 4 years ago

DominicFarrington commented 4 years ago

FileSystemVirtualFile has the following code:

        /// <summary>
        /// When overridden in a derived class, returns a read-only stream to the virtual resource.
        /// </summary>
        /// <returns>
        /// A read-only stream to the virtual file.
        /// </returns>
        public override Stream Open()
        {
            // Set the response headers here. It's a bit hacky.
            if (HttpContext.Current != null)
            {
                HttpCachePolicy cache = HttpContext.Current.Response.Cache;
                cache.SetCacheability(HttpCacheability.Public);
                cache.VaryByHeaders["Accept-Encoding"] = true;

                // Add Accept-Ranges header to fix videos not playing on Safari
                HttpContext.Current.Response.AppendHeader("Accept-Ranges", "bytes");

                IFileSystem azureBlobFileSystem = FileSystemProviderManager.Current.GetUnderlyingFileSystemProvider("media");
                int maxDays = ((AzureBlobFileSystem)azureBlobFileSystem).FileSystem.MaxDays;

                cache.SetExpires(DateTime.Now.ToUniversalTime().AddDays(maxDays));
                cache.SetMaxAge(new TimeSpan(maxDays, 0, 0, 0));
                cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            }

            return this.stream();
        }

This doesn't call

cache.SetETag(...); cache.SetLastModified(..);

Last modified appears to already be available in AzureBlobFileSystem could be an easy upgrade. ETag would be a bonus!

Would help with CDN hosting of media files. Encountered the issue on Umbraco 7.15.1