Пример #1
0
        /// <summary>
        /// Main worker method reads binary from Broker and stores it in file-system
        /// </summary>
        /// <returns></returns>
        public bool ProcessRequest(HttpRequest request)
        {
            string urlPath = request.Url.AbsolutePath.Replace("/BinaryData", "");

            LoggerService.Debug("Start processing " + urlPath);
            Dimensions dimensions = null;

            urlPath = StripDimensions(urlPath, out dimensions);

            String   physicalPath      = request.PhysicalPath;
            string   cacheKey          = GetCacheKey(urlPath);
            DateTime?lastPublishedDate = CacheAgent.Load(cacheKey) as DateTime?;


            if (lastPublishedDate == null)
            {
                DateTime lpb = BinaryFactory.FindLastPublishedDate(urlPath);
                if (lpb != DateTime.MinValue.AddSeconds(1)) // this is the secret code for 'does not exist'
                {
                    lastPublishedDate = new DateTime?(lpb);
                    CacheAgent.Store(cacheKey, "Binary", lastPublishedDate);
                }
            }
            if (lastPublishedDate != null)
            {
                if (File.Exists(physicalPath))
                {
                    FileInfo fi = new FileInfo(physicalPath);
                    if (fi.Length > 0)
                    {
                        DateTime fileModifiedDate = File.GetLastWriteTime(physicalPath);
                        if (fileModifiedDate.CompareTo(lastPublishedDate) >= 0)
                        {
                            LoggerService.Debug("binary {0} is still up to date, no action required", urlPath);
                            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.
            IBinary binary = null;

            try
            {
                BinaryFactory.TryFindBinary(urlPath, out binary);
            }
            catch (BinaryNotFoundException)
            {
                LoggerService.Debug("Binary with url {0} not found", urlPath);
                // binary does not exist in Tridion, it should be removed from the local file system too
                if (File.Exists(physicalPath))
                {
                    DeleteFile(physicalPath);
                }
                return(false);
            }
            return(WriteBinaryToFile(binary, physicalPath, dimensions));
        }
        /// <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)
        {
            string localFilePath = GetFilePathFromUrl(urlPath, localization);

            using (new Tracer(urlPath, localization, localFilePath))
            {
                Dimensions dimensions;
                urlPath = StripDimensions(urlPath, out dimensions);

                string   cacheKey          = GetCacheKey(urlPath);
                DateTime?lastPublishedDate = CacheAgent.Load(cacheKey) as DateTime?;
                if (lastPublishedDate == null)
                {
                    IBinaryFactory binaryFactory = DD4TFactoryCache.GetBinaryFactory(localization);
                    try
                    {
                        DateTime lpb = binaryFactory.FindLastPublishedDate(urlPath);
                        if (lpb != DateTime.MinValue.AddSeconds(1)) // this is the secret code for 'does not exist'
                        {
                            lastPublishedDate = lpb;
                            CacheAgent.Store(cacheKey, "Binary", lastPublishedDate);
                        }
                    }
                    catch (NullReferenceException)
                    {
                        //Binary not found, this should return a min date, but theres a bug in DD4T where it throws a NRE
                        //DO NOTHING - binary removed later
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        if (File.Exists(localFilePath))
                        {
                            //if theres an error connecting, but we still have a version on disk, serve this
                            return(localFilePath);
                        }
                    }
                }

                if (lastPublishedDate != null)
                {
                    if (File.Exists(localFilePath))
                    {
                        if (localization.LastRefresh.CompareTo(lastPublishedDate) < 0)
                        {
                            //File has been modified since last application start but we don't care
                            Log.Debug("Binary with URL '{0}' is modified, but only since last application restart, so no action required", urlPath);
                            return(localFilePath);
                        }
                        FileInfo fi = new FileInfo(localFilePath);
                        if (fi.Length > 0)
                        {
                            DateTime fileModifiedDate = File.GetLastWriteTime(localFilePath);
                            if (fileModifiedDate.CompareTo(lastPublishedDate) >= 0)
                            {
                                Log.Debug("Binary with URL '{0}' is still up to date, no action required", urlPath);
                                return(localFilePath);
                            }
                        }
                    }
                }

                // 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.
                IBinary binary;
                try
                {
                    IBinaryFactory binaryFactory = DD4TFactoryCache.GetBinaryFactory(localization);
                    binaryFactory.TryFindBinary(urlPath, out binary);
                }
                catch (BinaryNotFoundException)
                {
                    // TryFindBinary throws an Exception if not found ?!
                    binary = null;
                }
                catch (Exception ex)
                {
                    throw new DxaException(string.Format("Error loading binary for URL '{0}'", urlPath), ex);
                }

                //For some reason DD4T sometimes returns a non-null binary with null binary data if it doesnt exist
                if (binary == null || binary.BinaryData == null)
                {
                    // Binary does not exist in Tridion, it should be removed from the local file system too
                    if (File.Exists(localFilePath))
                    {
                        CleanupLocalFile(localFilePath);
                    }
                    throw new DxaItemNotFoundException(urlPath, localization.LocalizationId);
                }

                WriteBinaryToFile(binary, localFilePath, dimensions);
                return(localFilePath);
            }
        }