/// <summary>
    /// Ensures the physical file.
    /// </summary>
    /// <param name="file">Output file</param>
    public bool EnsurePhysicalFile(CMSOutputMediaFile file)
    {
        if (file == null)
        {
            return false;
        }

        // Try to link to file system
        if (String.IsNullOrEmpty(file.Watermark) && (file.MediaFile != null) && (file.MediaFile.FileID > 0))
        {
            SiteInfo si = SiteInfoProvider.GetSiteInfo(file.MediaFile.FileSiteID);
            if (si != null)
            {
                bool generateThumbnails = ValidationHelper.GetBoolean(SettingsKeyInfoProvider.GetValue(si.SiteName + ".CMSGenerateThumbnails"), true);
                string filePath = null;
                string libraryFolder = Path.EnsureEndBackslash(MediaLibraryInfoProvider.GetMediaLibraryFolderPath(file.MediaFile.FileLibraryID));

                if (file.Resized && generateThumbnails)
                {
                    filePath = libraryFolder + MediaFileInfoProvider.EnsureThumbnailFile(file.MediaFile, file.SiteName, Width, Height, MaxSideSize, file.UsePreview);
                }
                else
                {
                    if (file.UsePreview)
                    {
                        // Get file path
                        string path = MediaFileInfoProvider.GetMediaFilePath(file.MediaFile.FileLibraryID, file.MediaFile.FilePath);
                        string pathDirectory = Path.GetDirectoryName(path);
                        string hiddenFolderPath = MediaLibraryHelper.GetMediaFileHiddenFolder(CurrentSiteName);
                        string folderPath = String.Format("{0}\\{1}", pathDirectory, hiddenFolderPath);

                        // Ensure hidden folder exists
                        DirectoryHelper.EnsureDiskPath(folderPath, pathDirectory);
                        // Get preview file
                        string[] files = Directory.GetFiles(folderPath, MediaLibraryHelper.GetPreviewFileName(file.MediaFile.FileName, file.MediaFile.FileExtension, ".*", CurrentSiteName));
                        if (files.Length > 0)
                        {
                            filePath = files[0];
                        }
                    }
                    else
                    {
                        filePath = libraryFolder + file.MediaFile.FilePath;
                    }
                }

                if (filePath != null)
                {
                    // Link to the physical file
                    file.PhysicalFile = filePath;
                    return true;
                }
            }
        }

        file.PhysicalFile = "";
        return false;
    }
    /// <summary>
    /// Sets the last modified and expires header to the response
    /// </summary>
    /// <param name="file">Output file data</param>
    private void SetTimeStamps(CMSOutputMediaFile file)
    {
        DateTime expires = DateTime.Now;

        // Send last modified header to allow client caching
        Response.Cache.SetLastModified(file.LastModified);

        Response.Cache.SetCacheability(HttpCacheability.Public);
        if (AllowClientCache)
        {
            expires = DateTime.Now.AddMinutes(ClientCacheMinutes);
        }

        Response.Cache.SetExpires(expires);
    }
    /// <summary>
    /// Sends the given file within response.
    /// </summary>
    /// <param name="file">File to send</param>
    protected void SendFile(CMSOutputMediaFile file)
    {
        // Clear response.
        CookieHelper.ClearResponseCookies();
        Response.Clear();

        // Set the revalidation
        SetRevalidation();

        // Send the file
        if (file != null)
        {
            #region "Security"

            // Check if user is allowed to see the library file content if required
            bool checkPermissions = SettingsKeyProvider.GetBoolValue(CMSContext.CurrentSiteName + ".CMSCheckMediaFilePermissions");
            if (checkPermissions)
            {
                // Check the library access for the current user and stop file processing if not allowed
                MediaLibraryInfo mli = MediaLibraryInfoProvider.GetMediaLibraryInfo(file.MediaFile.FileLibraryID);
                if (!MediaLibraryInfoProvider.IsUserAuthorizedPerLibrary(mli, "LibraryAccess"))
                {
                    URLHelper.Redirect(URLRewriter.AccessDeniedPageURL(CurrentSiteName));
                    return;
                }
            }

            #endregion

            // Prepare etag
            string etag = "";
            if (file.MediaFile != null)
            {
                etag += file.MediaFile.FileModifiedWhen.ToUniversalTime();
            }

            // Put etag into ""
            etag = String.Format("\"{0}\"", etag);

            // Client caching - only on the live site
            if (useClientCache && AllowCache && AllowClientCache && ETagsMatch(etag, file.LastModified))
            {
                // Set the file time stamps to allow client caching
                SetTimeStamps(file);

                RespondNotModified(etag, true);
                return;
            }

            // If physical file not present, try to load
            EnsurePhysicalFile(outputFile);

            // If the output data should be cached, return the output data
            bool cacheOutputData = false;
            if ((file.MediaFile != null) && (CacheMinutes > 0))
            {
                cacheOutputData = CacheHelper.CacheImageAllowed(CurrentSiteName, (int)file.MediaFile.FileSize);
            }

            // Ensure the file data if physical file not present
            if (!file.DataLoaded && (file.PhysicalFile == ""))
            {
                byte[] cachedData = GetCachedOutputData();
                if (file.EnsureData(cachedData))
                {
                    if ((cachedData == null) && cacheOutputData)
                    {
                        SaveOutputDataToCache(file.OutputData, GetOutputDataDependency(file.MediaFile));
                    }
                }
            }

            // Send the file
            if ((file.OutputData != null) || (file.PhysicalFile != ""))
            {
                // Setup the mime type - Fix the special types
                string mimetype = file.MimeType;
                string extension = file.FileExtension;
                switch (extension.ToLower())
                {
                    case ".flv":
                        mimetype = "video/x-flv";
                        break;
                }

                // Prepare response
                Response.ContentType = mimetype;
                SetDisposition(file.FileName + extension, extension);

                // Setup Etag property
                ETag = etag;

                // Set if resumable downloads should be supported
                AcceptRange = !IsExtensionExcludedFromRanges(extension);

                if (useClientCache && AllowCache)
                {
                    // Set the file time stamps to allow client caching
                    SetTimeStamps(file);

                    Response.Cache.SetETag(etag);
                }

                // Add the file data
                if ((file.PhysicalFile != "") && (file.OutputData == null))
                {
                    // Stream the file from the file system
                    file.OutputData = WriteFile(file.PhysicalFile, cacheOutputData);
                }
                else
                {
                    // Use output data of the file in memory if present
                    WriteBytes(file.OutputData);
                }
            }
            else
            {
                this.NotFound();
            }
        }
        else
        {
            this.NotFound();
        }

        CompleteRequest();
    }
    /// <summary>
    /// Processes the specified file.
    /// </summary>
    /// <param name="fileGuid">File guid</param>
    /// <param name="preview">Use preview</param>
    protected void ProcessFile(Guid fileGuid, bool preview)
    {
        // Get the file info if doesn't retrieved yet
        fileInfo = (fileInfo ?? MediaFileInfoProvider.GetMediaFileInfo(fileGuid, CurrentSiteName));
        if (fileInfo != null)
        {
            if (preview)
            {
                // Get file path
                string path = MediaFileInfoProvider.GetMediaFilePath(fileInfo.FileLibraryID, fileInfo.FilePath);
                string pathDirectory = Path.GetDirectoryName(path);
                string hiddenFolderPath = MediaLibraryHelper.GetMediaFileHiddenFolder(CurrentSiteName);
                string folderPath = DirectoryHelper.CombinePath(pathDirectory, hiddenFolderPath);

                // Ensure hidden folder exists
                DirectoryHelper.EnsureDiskPath(folderPath, pathDirectory);
                // Get preview file
                string[] files = Directory.GetFiles(folderPath, MediaLibraryHelper.GetPreviewFileName(fileInfo.FileName, fileInfo.FileExtension, ".*", CurrentSiteName));
                if (files.Length > 0)
                {
                    bool resizeImage = (ImageHelper.IsImage(Path.GetExtension(files[0])) && MediaFileInfoProvider.CanResizeImage(files[0], Width, Height, MaxSideSize));

                    // Get the data
                    if ((outputFile == null) || (outputFile.MediaFile == null))
                    {
                        outputFile = NewOutputFile(fileInfo, null);
                        outputFile.UsePreview = true;
                        outputFile.Width = Width;
                        outputFile.Height = Height;
                        outputFile.MaxSideSize = MaxSideSize;
                        outputFile.Resized = resizeImage;
                        outputFile.FileExtension = Path.GetExtension(files[0]);
                        outputFile.MimeType = MimeTypeHelper.GetMimetype(outputFile.FileExtension);
                    }
                }
            }
            else
            {
                bool resizeImage = (ImageHelper.IsImage(fileInfo.FileExtension) && MediaFileInfoProvider.CanResizeImage(fileInfo, Width, Height, MaxSideSize));

                // Get the data
                if ((outputFile == null) || (outputFile.MediaFile == null))
                {
                    outputFile = NewOutputFile(fileInfo, null);
                    outputFile.Width = Width;
                    outputFile.Height = Height;
                    outputFile.MaxSideSize = MaxSideSize;
                    outputFile.Resized = resizeImage;
                }
            }
        }
    }
    /// <summary>
    /// Processes the file.
    /// </summary>
    protected void ProcessFile()
    {
        outputFile = null;

        // Get file guid from querystring
        fileGuid = QueryHelper.GetGuid("fileguid", Guid.Empty);
        if (fileGuid != Guid.Empty)
        {
            ProcessFile(fileGuid, Preview);
        }
    }
    /// <summary>
    /// Gets the new output MediaFile object.
    /// </summary>
    /// <param name="mfi">Media file info</param>
    /// <param name="data">Output MediaFile data</param>
    public CMSOutputMediaFile NewOutputFile(MediaFileInfo mfi, byte[] data)
    {
        CMSOutputMediaFile mf = new CMSOutputMediaFile(mfi, data)
        {
            Watermark = Watermark,
            WatermarkPosition = WatermarkPosition
        };

        return mf;
    }
    /// <summary>
    /// Gets the new output MediaFile object.
    /// </summary>
    public CMSOutputMediaFile NewOutputFile()
    {
        CMSOutputMediaFile mf = new CMSOutputMediaFile
        {
            Watermark = Watermark,
            WatermarkPosition = WatermarkPosition
        };

        return mf;
    }
    /// <summary>
    /// Sets content type of the response based on file MIME type
    /// </summary>
    /// <param name="file">Output file</param>
    private void SetResponseContentType(CMSOutputMediaFile file)
    {
        string mimeType = file.MimeType;
        if (file.MediaFile != null)
        {
            string extension = file.MediaFile.FileExtension;
            switch (extension.ToLowerCSafe())
            {
                case ".flv":
                    // Correct MIME type
                    mimeType = "video/x-flv";
                    break;
            }
        }

        // Set content type
        Response.ContentType = mimeType;
    }
    /// <summary>
    /// Processes the file.
    /// </summary>
    protected void ProcessFile()
    {
        outputFile = null;

        // Get file guid from querystring
        fileGuid = QueryHelper.GetGuid("fileguid", Guid.Empty);
        if (fileGuid == Guid.Empty)
        {
            return;
        }

        // Get the file
        var file = MediaFileInfoProvider.GetMediaFileInfo(fileGuid, CurrentSiteName);
        if (file == null)
        {
            return;
        }

        if (Preview)
        {
            // Get file path
            string path = MediaFileInfoProvider.GetMediaFilePath(file.FileLibraryID, file.FilePath);
            string pathDirectory = Path.GetDirectoryName(path);
            string hiddenFolderPath = MediaLibraryHelper.GetMediaFileHiddenFolder(CurrentSiteName);
            string folderPath = DirectoryHelper.CombinePath(pathDirectory, hiddenFolderPath);

            // Ensure hidden folder exists
            DirectoryHelper.EnsureDiskPath(folderPath, pathDirectory);

            // Get preview file
            string[] files = Directory.GetFiles(folderPath, MediaLibraryHelper.GetPreviewFileName(file.FileName, file.FileExtension, ".*", CurrentSiteName));
            if (files.Length <= 0)
            {
                return;
            }

            bool resizeImage = (ImageHelper.IsImage(Path.GetExtension(files[0])) && MediaFileInfoProvider.CanResizeImage(files[0], Width, Height, MaxSideSize));

            // Get the data
            if ((outputFile == null) || (outputFile.MediaFile == null))
            {
                outputFile = NewOutputFile(file, null);
                outputFile.UsePreview = true;
                outputFile.Width = Width;
                outputFile.Height = Height;
                outputFile.MaxSideSize = MaxSideSize;
                outputFile.Resized = resizeImage;
                outputFile.FileExtension = Path.GetExtension(files[0]);
                outputFile.MimeType = MimeTypeHelper.GetMimetype(outputFile.FileExtension);
            }
        }
        else
        {
            bool resizeImage = (ImageHelper.IsImage(file.FileExtension) && MediaFileInfoProvider.CanResizeImage(file, Width, Height, MaxSideSize));

            // Get the data
            if ((outputFile == null) || (outputFile.MediaFile == null))
            {
                outputFile = NewOutputFile(file, null);
                outputFile.Width = Width;
                outputFile.Height = Height;
                outputFile.MaxSideSize = MaxSideSize;
                outputFile.Resized = resizeImage;
            }
        }
    }