Exemplo n.º 1
0
 private void RetrieveFileData(HttpContext context, string filePath, string container)
 {
     MediaFileModel resultModel = new MediaFileModel();
     // only send request to imageprocessor if querystring exists; can exclude other parameters if needed
     if (context.Request.RawUrl.Contains("?"))
     {
         resultModel = MediaHelper.GetMediaFile(filePath, container, context.Request.RawUrl);
     }
     else
     {
         resultModel = MediaHelper.GetMediaFile(filePath, container);
     }
     if (resultModel.RedirectToAzureStorage)
     {
         context.Response.Redirect(filePath.Replace($"/{container}", $"{ConfigurationManager.AppSettings["BlobStorage"]}/{container}"), true);
     }
     var myTimeSpan = new TimeSpan(7, 0, 0, 0);
     context.Response.Cache.SetCacheability(HttpCacheability.Public);
     context.Response.Cache.SetValidUntilExpires(true);
     context.Response.Cache.SetMaxAge(myTimeSpan);
     context.Response.Cache.SetLastModified(resultModel.LastModifiedDate);
     context.Response.Cache.SetETag(resultModel.ETag.Replace("\\\"", ""));
     context.Response.AddHeader("Content-MD5", resultModel.ContentMd5);
     context.Response.ContentType = resultModel.ContentType;
     // replicate properties returned by blob storage
     context.Response.AddHeader("Server", "Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0");
     context.Response.AddHeader("x-ms-request-id", Guid.NewGuid().ToString());
     context.Response.AddHeader("x-ms-version", "2009-09-19");
     context.Response.AddHeader("x-ms-lease-status", "unlocked");
     context.Response.AddHeader("x-ms-blob-type", "BlockBlob");
     context.Response.OutputStream.Write(resultModel.ImageData, 0, resultModel.ImageData.Length);
     context.Response.AddHeader("Content-Length", resultModel.ImageData.Length.ToString());
     context.Response.Flush();
     context.Response.End();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieve media content directly from blob storage.
        /// </summary>
        /// <param name="filePath">Relative file path</param>
        /// <param name="container">Container to retrieve media file from</param>
        /// <returns>Media file using the MediaFileModel</returns>
        public static MediaFileModel GetMediaFile(string filePath, string container)
        {
            var myModel = new MediaFileModel();

            var storageAccount = Account;
            // validate the container has been defined
            if (string.IsNullOrWhiteSpace(container))
                throw new ArgumentNullException("Container is missing; this is required.");
            var blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference(container);

            // validate the filepath isn't empty
            if (string.IsNullOrWhiteSpace(filePath))
                throw new ArgumentNullException("File path is missing; this is required.");
            var mediaFile = blobContainer.GetBlockBlobReference(filePath.ToLower().Replace($"/{container}/", string.Empty));

            // validate the file exists; if not, return 404
            if (mediaFile == null || !mediaFile.Exists())
            {
                myModel.RedirectToAzureStorage = true;
                return myModel;
            }
            var myStream = new MemoryStream();
            mediaFile.DownloadToStream(myStream);
            myModel.ImageData = myStream.ToArray();
            myModel.ContentType = mediaFile.Properties.ContentType;
            myModel.RedirectToAzureStorage = false;
            myModel.LastModifiedDate = mediaFile.Properties.LastModified?.DateTime ?? DateTime.Now;
            myModel.ETag = mediaFile.Properties.ETag;
            myModel.ContentMd5 = mediaFile.Properties.ContentMD5;
            return myModel;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Retrieve media file from Azure Storage and additionally call image processor to retrieve the dynamic image based on the raw url.
        /// </summary>
        /// <param name="filePath">The relative filepath to the file.</param>
        /// <param name="container">Container to retrieve media file from.</param>
        /// <param name="rawUrl">The url to the file including the query string.</param>
        /// <returns>Media file using the MediaFileModel</returns>
        public static MediaFileModel GetMediaFile(string filePath, string container, string rawUrl)
        {
            var myModel = new MediaFileModel();

            myModel = GetMediaFile(filePath, container);
            // if RedirectToAzureStorage was set, we couldn't retrieve the media file, so doubtful the remote request will work either.
            if (myModel.RedirectToAzureStorage) return myModel;

            // call remote.axd to the defined Azure CDN and pass in the blob storage location.
            var webc = new WebClient();
            myModel.ImageData =
                webc.DownloadData(
                    $"{ConfigurationManager.AppSettings["AzureCDN"]}/remote.axd/{ConfigurationManager.AppSettings["BlobStorageRequest"]}{rawUrl}");
            return myModel;
        }