Esempio n. 1
0
        public IHttpActionResult GetBinaryStreamByUrl(int publicationId, string extension, string url)
        {
            Logger.Debug("GetBinaryStreamByUrl  publicationId={0}, url={1}, extension={2}", publicationId, url, extension);

            BinaryProvider.PublicationId = publicationId;
            var binary = BinaryProvider.GetBinaryStreamByUrl(url.GetUrl(extension));

            if (binary == null)
            {
                NotFound();
            }

            return(Ok <Stream>(binary));
        }
Esempio n. 2
0
        public bool TryFindBinary(string url, out IBinary binary)
        {
            binary = new Binary();

            if (LoadBinariesAsStream)
            {
                binary.BinaryStream = BinaryProvider.GetBinaryStreamByUrl(url);
                if (binary.BinaryStream == null)
                {
                    return(false);
                }
            }
            else
            {
                binary.BinaryData = BinaryProvider.GetBinaryByUrl(url);
                if (binary.BinaryData == null || binary.BinaryData.Length == 0)
                {
                    return(false);
                }
            }

            return(true);
        }
        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;
            }
        }