public IHttpActionResult GetBinaryMetaByUrl(int publicationId, string extension, string url) { Logger.Debug("GetBinaryMetaByUrl publicationId={0}, url={1}, extension={2}", publicationId, url, extension); BinaryProvider.PublicationId = publicationId; IBinaryMeta binaryMeta = BinaryProvider.GetBinaryMetaByUrl(url.GetUrl(extension)) as IBinaryMeta; if (binaryMeta == null) { return(NotFound()); } Logger.Debug($"about to return binarymeta {binaryMeta.Id}"); return(Ok(binaryMeta)); }
public IBinaryMeta FindBinaryMeta(string url) { string cacheKey = CacheKeyFactory.GenerateKey(url); IBinaryMeta binaryMeta = CacheAgent.Load(cacheKey) as IBinaryMeta; if (binaryMeta != null) { return(binaryMeta); } binaryMeta = BinaryProvider.GetBinaryMetaByUrl(url); if (binaryMeta == null) // TODO: cache null result { return(null); } CacheAgent.Store(cacheKey, "Binary", binaryMeta, new List <string> { binaryMeta.Id }); return(binaryMeta); }
private void CopyBinaryMetaToBinary(IBinaryMeta binaryMeta, IBinary binary) { ((Binary)binary).Id = binaryMeta.Id; ((Binary)binary).VariantId = binaryMeta.VariantId; ((Binary)binary).LastPublishedDate = binaryMeta.LastPublishedDate; }
private bool TryFindBinary(string url, string localPath, bool retrieveData, out IBinary binary) { string physicalPath = localPath ?? Path.Combine(Configuration.BinaryFileSystemCachePath, Path.GetFileName(url)); LoggerService.Debug($"Using physical path {physicalPath}"); binary = new Binary(); Dimensions dimensions = null; string urlWithoutDimensions = StripDimensions(url, out dimensions); if (LoadBinariesAsStream) { LoggerService.Information("retrieving binaries as a stream is obsolete; support will be dropped in future versions of DD4T"); binary.BinaryStream = BinaryProvider.GetBinaryStreamByUrl(urlWithoutDimensions); if (binary.BinaryStream == null) { return(false); } } string cacheKey = CacheKeyFactory.GenerateKey(urlWithoutDimensions); try { IBinaryMeta binaryMeta = CacheAgent.Load(cacheKey) as IBinaryMeta; bool metaWasInCache = true; if (binaryMeta == null) { metaWasInCache = false; binaryMeta = BinaryProvider.GetBinaryMetaByUrl(urlWithoutDimensions); if (binaryMeta == null) { throw new BinaryNotFoundException(); } CacheAgent.Store(cacheKey, "Binary", binaryMeta, new List <string> { binaryMeta.Id }); } if (FileExistsAndIsNotEmpty(physicalPath)) { if (binaryMeta.HasLastPublishedDate || metaWasInCache) { DateTime fileModifiedDate = File.GetLastWriteTime(physicalPath); if (fileModifiedDate.CompareTo(binaryMeta.LastPublishedDate) >= 0) { LoggerService.Debug("binary {0} is still up to date, no action required", urlWithoutDimensions); // TODO: load bytes from file system into binary if (retrieveData) { FillBinaryFromLocalFS(binary, physicalPath); } CopyBinaryMetaToBinary(binaryMeta, binary); return(true); } } } // the normal situation (where a binary is still in Tridion and it is present on the file system already and it is up to date) is now covered // Let's handle the exception situations. byte[] bytes = BinaryProvider.GetBinaryByUrl(urlWithoutDimensions); if (bytes == null || bytes.Length == 0) { throw new BinaryNotFoundException(); } bool fileIsCreated = WriteBinaryToFile(bytes, physicalPath, dimensions); if (!fileIsCreated) { LoggerService.Warning($"file '{physicalPath}' could not be created, binary {binary.Id} cannot be returned"); return(false); } if (retrieveData) { if (dimensions == null) { binary.BinaryData = bytes; } else { FillBinaryFromLocalFS(binary, physicalPath); } } CopyBinaryMetaToBinary(binaryMeta, binary); return(true); } catch (BinaryNotFoundException) { LoggerService.Debug("Binary with url {0} not found", urlWithoutDimensions); // binary does not exist in Tridion, it should be removed from the local file system too DeleteFile(physicalPath); return(false); } catch (Exception e) { LoggerService.Warning($"Caught unexpected exception while retrieving binary with url {urlWithoutDimensions} (requested url: {url}. Error message: {e.Message}\r\n{e.StackTrace}"); // in case of error, the binary should be removed from the local file system too DeleteFile(physicalPath); throw e; } }