/// <summary> /// Gets the cached local file for a given URL path. /// </summary> /// <param name="urlPath">The URL path.</param> /// <param name="localization">The Localization.</param> /// <returns>The path to the local file.</returns> internal string GetCachedFile(string urlPath, Localization localization) { IBinaryProvider provider = Provider; string baseDir = AppDomain.CurrentDomain.BaseDirectory; string localFilePath = $"{baseDir}/{urlPath}"; if (File.Exists(localFilePath)) { // If our resource exists on the filesystem we can assume static content that is // manually added to web application. return(localFilePath); } // Attempt cache location with fallback to retrieval from content service. localFilePath = $"{baseDir}/{localization.BinaryCacheFolder}/{urlPath}"; using (new Tracer(urlPath, localization, localFilePath)) { Dimensions dimensions; urlPath = StripDimensions(urlPath, out dimensions); if (File.Exists(localFilePath)) { if (IsCached(() => provider.GetBinaryLastPublishedDate(localization, urlPath), localFilePath, localization)) { return(localFilePath); } } var binary = provider.GetBinary(localization, urlPath); if (binary != null) { WriteBinaryToFile(binary.Item1, localFilePath, dimensions); } return(localFilePath); } }
/// <summary> /// Gets the cached local file for a given binary Id. /// </summary> /// <param name="binaryId">The binary Id.</param> /// <param name="localization">The Localization.</param> /// <returns>The path to the local file.</returns> internal string GetCachedFile(int binaryId, Localization localization, out MemoryStream memoryStream) { memoryStream = null; IBinaryProvider provider = Provider; string baseDir = AppDomain.CurrentDomain.BaseDirectory; string localFilePath = $"{baseDir}/{localization.BinaryCacheFolder}"; using (new Tracer(binaryId, localization, localFilePath)) { try { if (Directory.Exists(localFilePath)) { string[] files = Directory.GetFiles(localFilePath, $"{binaryId}*", SearchOption.TopDirectoryOnly); if (files.Length > 0) { localFilePath = files[0]; if (IsCached(() => provider.GetBinaryLastPublishedDate(localization, binaryId), localFilePath, localization)) { return(localFilePath); } } } } catch (Exception ex) { // Our binary cache folder probably doesn't exist. Log.Warn($"Failed to cache binary at {localFilePath}"); Log.Warn(ex.Message); } var data = provider.GetBinary(localization, binaryId); if (string.IsNullOrEmpty(Path.GetExtension(localFilePath))) { var ext = Path.GetExtension(data.Item2) ?? ""; localFilePath = $"{localFilePath}/{binaryId}{ext}"; } WriteBinaryToFile(data.Item1, localFilePath, null, out memoryStream); return(localFilePath); } }