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

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

        // Setup the client cache
        Response.Cache.SetCacheability(HttpCacheability.Public);
        if (AllowClientCache)
        {
            expires = DateTime.Now.AddMinutes(ClientCacheMinutes);
        }

        Response.Cache.SetExpires(expires);
    }
    /// <summary>
    /// Processes the specified file.
    /// </summary>
    /// <param name="fileGuid">File guid</param>
    protected void ProcessFile(Guid fileGuid)
    {
        // Get the file
        ForumAttachmentInfo fileInfo = ForumAttachmentInfoProvider.GetForumAttachmentInfoWithoutBinary(fileGuid, CMSContext.CurrentSiteName);

        if (fileInfo != null)
        {
            #region "Security"

            // Indicates whether current user is granted to see this attachment
            bool attachmentAllowed = false;

            // Get forum
            ForumInfo fi = ForumInfoProvider.GetForumInfo(fileInfo.AttachmentForumID);
            if (fi != null)
            {
                // Check acess
                if (ForumViewer.CheckPermission("AccessToForum", SecurityHelper.GetSecurityAccessEnum(fi.ForumAccess, 6), fi.ForumGroupID, fi.ForumID))
                {
                    attachmentAllowed = true;
                }
            }

            // If attachment is not allowed for current user, redirect to the access denied page
            if (!attachmentAllowed)
            {
                URLHelper.Redirect(URLRewriter.AccessDeniedPageURL(CurrentSiteName));
            }

            #endregion


            bool resizeImage = (ImageHelper.IsMimeImage(fileInfo.AttachmentMimeType) &&
                                ForumAttachmentInfoProvider.CanResizeImage(fileInfo, Width, Height, MaxSideSize));

            // Get the data
            if ((outputFile == null) || (outputFile.ForumAttachment == null))
            {
                outputFile             = new CMSOutputForumAttachment(fileInfo, fileInfo.AttachmentBinary);
                outputFile.Width       = Width;
                outputFile.Height      = Height;
                outputFile.MaxSideSize = MaxSideSize;
                outputFile.Resized     = resizeImage;
            }
        }
    }
    /// <summary>
    /// Sets content type of the response based on file MIME type
    /// </summary>
    /// <param name="file">Output file</param>
    private void SetResponseContentType(CMSOutputForumAttachment file)
    {
        string mimeType = file.MimeType;

        if (file.ForumAttachment != null)
        {
            string extension = file.ForumAttachment.AttachmentFileExtension;
            switch (extension.ToLowerCSafe())
            {
            case ".flv":
                // Correct MIME type
                mimeType = "video/x-flv";
                break;
            }
        }

        // Set content type
        Response.ContentType = mimeType;
    }
Example #4
0
    /// <summary>
    /// Ensures the physical file.
    /// </summary>
    /// <param name="file">Output file</param>
    public bool EnsurePhysicalFile(CMSOutputForumAttachment file)
    {
        if (file == null)
        {
            return(false);
        }

        // Try to link to file system
        if ((file.ForumAttachment != null) && (file.ForumAttachment.AttachmentID > 0) && ForumAttachmentInfoProvider.StoreFilesInFileSystem(CurrentSiteName))
        {
            string filePath = ForumAttachmentInfoProvider.EnsureAttachmentPhysicalFile(file.ForumAttachment, CurrentSiteName);
            if (filePath != null)
            {
                if (file.Resized)
                {
                    // If resized, ensure the thumbnail file
                    if (ForumAttachmentInfoProvider.GenerateThumbnails(CurrentSiteName))
                    {
                        filePath = ForumAttachmentInfoProvider.EnsureThumbnailFile(file.ForumAttachment, this.Width, this.Height, this.MaxSideSize);
                        if (filePath != null)
                        {
                            // Link to the physical file
                            file.PhysicalFile = filePath;
                            return(true);
                        }
                    }
                }
                else
                {
                    // Link to the physical file
                    file.PhysicalFile = filePath;
                    return(false);
                }
            }
        }


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

        // Try to link to file system
        if ((file.ForumAttachment != null) && (file.ForumAttachment.AttachmentID > 0) && ForumAttachmentInfoProvider.StoreFilesInFileSystem(CurrentSiteName))
        {
            string filePath = ForumAttachmentInfoProvider.EnsureAttachmentPhysicalFile(file.ForumAttachment, CurrentSiteName);
            if (filePath != null)
            {
                if (file.Resized)
                {
                    // If resized, ensure the thumbnail file
                    if (ForumAttachmentInfoProvider.GenerateThumbnails(CurrentSiteName))
                    {
                        filePath = ForumAttachmentInfoProvider.EnsureThumbnailFile(file.ForumAttachment, Width, Height, MaxSideSize);
                        if (filePath != null)
                        {
                            // Link to the physical file
                            file.PhysicalFile = filePath;
                            return true;
                        }
                    }
                }
                else
                {
                    // Link to the physical file
                    file.PhysicalFile = filePath;
                    return false;
                }
            }
        }

        file.PhysicalFile = "";
        return false;
    }
    /// <summary>
    /// Processes the file.
    /// </summary>
    protected void ProcessFile()
    {
        if (fileGuid == Guid.Empty)
        {
            return;
        }

        // Get the file
        ForumAttachmentInfo fileInfo = ForumAttachmentInfoProvider.GetForumAttachmentInfoWithoutBinary(fileGuid, SiteContext.CurrentSiteName);

        if (fileInfo == null)
        {
            return;
        }

        // Check forum access
        var forum = ForumInfoProvider.GetForumInfo(fileInfo.AttachmentForumID);

        if ((forum == null) || !ForumViewer.CheckPermission("AccessToForum", SecurityHelper.GetSecurityAccessEnum(forum.ForumAccess, 6), forum.ForumGroupID, forum.ForumID, CurrentUser))
        {
            // If attachment is not allowed for current user, redirect to the access denied page
            URLHelper.Redirect(PageSecurityHelper.AccessDeniedPageURL(CurrentSiteName));
        }

        bool resizeImage = (ImageHelper.IsMimeImage(fileInfo.AttachmentMimeType) && ForumAttachmentInfoProvider.CanResizeImage(fileInfo, Width, Height, MaxSideSize));

        // Get the data
        if ((outputFile == null) || (outputFile.ForumAttachment == null))
        {
            outputFile             = new CMSOutputForumAttachment(fileInfo, fileInfo.AttachmentBinary);
            outputFile.Width       = Width;
            outputFile.Height      = Height;
            outputFile.MaxSideSize = MaxSideSize;
            outputFile.Resized     = resizeImage;
        }
    }
    /// <summary>
    /// Sets content type of the response based on file MIME type
    /// </summary>
    /// <param name="file">Output file</param>
    private void SetResponseContentType(CMSOutputForumAttachment file)
    {
        string mimeType = file.MimeType;
        if (file.ForumAttachment != null)
        {
            string extension = file.ForumAttachment.AttachmentFileExtension;
            switch (extension.ToLowerCSafe())
            {
                case ".flv":
                    // Correct MIME type
                    mimeType = "video/x-flv";
                    break;
            }
        }

        // Set content type
        Response.ContentType = mimeType;
    }
    /// <summary>
    /// Sends the given file within response.
    /// </summary>
    /// <param name="file">File to send</param>
    protected void SendFile(CMSOutputForumAttachment file)
    {
        // Clear response.
        CookieHelper.ClearResponseCookies();
        Response.Clear();

        // Set the revalidation
        SetRevalidation();

        if (file != null)
        {
            // Redirect if the file should be redirected
            if (file.RedirectTo != "")
            {
                URLHelper.RedirectPermanent(file.RedirectTo, CurrentSiteName);
            }

            // Prepare etag
            string etag = file.LastModified.ToString();

            // Client caching - only on the live site
            if (useClientCache && AllowCache && ETagsMatch(etag, file.LastModified))
            {
                // Set correct response content type
                SetResponseContentType(file);

                SetTimeStamps(file.LastModified);

                RespondNotModified(etag);
                return;
            }

            // If physical file not present, try to load
            if (file.PhysicalFile == null)
            {
                EnsurePhysicalFile(outputFile);
            }

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

            // Send the file
            if ((file.OutputData != null) || (file.PhysicalFile != ""))
            {
                // Set correct response content type
                SetResponseContentType(file);

                string extension = file.ForumAttachment.AttachmentFileExtension;
                SetDisposition(file.ForumAttachment.AttachmentFileName, extension);

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

                if (useClientCache && AllowCache)
                {
                    SetTimeStamps(file.LastModified);

                    Response.Cache.SetETag(etag);
                }
                else
                {
                    SetCacheability();
                }

                // Add the file data
                if ((file.PhysicalFile != "") && (file.OutputData == null))
                {
                    if (!File.Exists(file.PhysicalFile))
                    {
                        // File doesn't exist
                        NotFound();
                    }
                    else
                    {
                        // If the output data should be cached, return the output data
                        bool cacheOutputData = false;
                        if (file.ForumAttachment != null)
                        {
                            cacheOutputData = CacheHelper.CacheImageAllowed(CurrentSiteName, file.ForumAttachment.AttachmentFileSize);
                        }

                        // 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
            {
                NotFound();
            }
        }
        else
        {
            NotFound();
        }

        CompleteRequest();
    }
    /// <summary>
    /// Processes the file.
    /// </summary>
    protected void ProcessFile()
    {
        if (fileGuid == Guid.Empty)
        {
            return;
        }

        // Get the file
        ForumAttachmentInfo fileInfo = ForumAttachmentInfoProvider.GetForumAttachmentInfoWithoutBinary(fileGuid, SiteContext.CurrentSiteName);
        if (fileInfo == null)
        {
            return;
        }

        // Check forum access
        var forum = ForumInfoProvider.GetForumInfo(fileInfo.AttachmentForumID);
        if ((forum == null) || !ForumViewer.CheckPermission("AccessToForum", SecurityHelper.GetSecurityAccessEnum(forum.ForumAccess, 6), forum.ForumGroupID, forum.ForumID, CurrentUser))
        {
            // If attachment is not allowed for current user, redirect to the access denied page
            URLHelper.Redirect(PageSecurityHelper.AccessDeniedPageURL(CurrentSiteName));
        }

        bool resizeImage = (ImageHelper.IsMimeImage(fileInfo.AttachmentMimeType) && ForumAttachmentInfoProvider.CanResizeImage(fileInfo, Width, Height, MaxSideSize));

        // Get the data
        if ((outputFile == null) || (outputFile.ForumAttachment == null))
        {
            outputFile = new CMSOutputForumAttachment(fileInfo, fileInfo.AttachmentBinary);
            outputFile.Width = Width;
            outputFile.Height = Height;
            outputFile.MaxSideSize = MaxSideSize;
            outputFile.Resized = resizeImage;
        }
    }
    /// <summary>
    /// Sends the given file within response.
    /// </summary>
    /// <param name="file">File to send</param>
    protected void SendFile(CMSOutputForumAttachment file)
    {
        // Clear response.
        CookieHelper.ClearResponseCookies();
        Response.Clear();

        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

        if (file != null)
        {
            // Redirect if the file should be redirected
            if (file.RedirectTo != "")
            {
                URLHelper.Redirect(file.RedirectTo, true, CurrentSiteName);
            }

            // Prepare etag
            string etag = file.LastModified.ToString();

            // Client caching - only on the live site
            if (useClientCache && AllowCache && CacheHelper.CacheImageEnabled(CurrentSiteName) && ETagsMatch(etag, file.LastModified))
            {
                RespondNotModified(etag, true);
                return;
            }

            // If physical file not present, try to load
            if (file.PhysicalFile == null)
            {
                EnsurePhysicalFile(outputFile);
            }

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

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

                // Prepare response
                Response.ContentType = mimetype;
                SetDisposition(file.ForumAttachment.AttachmentFileName, extension);

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

                if (useClientCache && AllowCache && (CacheHelper.CacheImageEnabled(CurrentSiteName)))
                {
                    DateTime expires = DateTime.Now;

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

                    Response.Cache.SetCacheability(HttpCacheability.Public);
                    if (DocumentBase.AllowClientCache() && DocumentBase.UseFullClientCache)
                    {
                        expires = DateTime.Now.AddMinutes(CacheHelper.CacheImageMinutes(CurrentSiteName));
                    }

                    Response.Cache.SetExpires(expires);
                    Response.Cache.SetETag(etag);
                }

                // Add the file data
                if ((file.PhysicalFile != "") && (file.OutputData == null))
                {
                    // If the output data should be cached, return the output data
                    bool cacheOutputData = false;
                    if (file.ForumAttachment != null)
                    {
                        cacheOutputData = CacheHelper.CacheImageAllowed(CurrentSiteName, file.ForumAttachment.AttachmentFileSize);
                    }

                    // 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
            {
                NotFound();
            }
        }
        else
        {
            NotFound();
        }

        CompleteRequest();
        //RequestHelper.EndResponse();
    }
    /// <summary>
    /// Processes the specified file.
    /// </summary>
    /// <param name="fileGuid">File guid</param>
    protected void ProcessFile(Guid fileGuid)
    {
        // Get the file
        ForumAttachmentInfo fileInfo = ForumAttachmentInfoProvider.GetForumAttachmentInfoWithoutBinary(fileGuid, CMSContext.CurrentSiteName);
        if (fileInfo != null)
        {
            #region "Security"

            // Indicates whether current user is granted to see this attachment
            bool attachmentAllowed = false;

            // Get forum
            ForumInfo fi = ForumInfoProvider.GetForumInfo(fileInfo.AttachmentForumID);
            if (fi != null)
            {
                // Check acess
                if (ForumViewer.CheckPermission("AccessToForum", SecurityHelper.GetSecurityAccessEnum(fi.ForumAccess, 6), fi.ForumGroupID, fi.ForumID))
                {
                    attachmentAllowed = true;
                }
            }

            // If attachment is not allowed for current user, redirect to the access denied page
            if (!attachmentAllowed)
            {
                URLHelper.Redirect(URLRewriter.AccessDeniedPageURL(CurrentSiteName));
            }

            #endregion

            bool resizeImage = (ImageHelper.IsMimeImage(fileInfo.AttachmentMimeType) &&
            ForumAttachmentInfoProvider.CanResizeImage(fileInfo, this.Width, this.Height, this.MaxSideSize));

            // Get the data
            if ((outputFile == null) || (outputFile.ForumAttachment == null))
            {
                outputFile = new CMSOutputForumAttachment(fileInfo, fileInfo.AttachmentBinary);
                outputFile.Width = this.Width;
                outputFile.Height = this.Height;
                outputFile.MaxSideSize = this.MaxSideSize;
                outputFile.Resized = resizeImage;
            }
        }
    }
Example #12
0
    /// <summary>
    /// Sends the given file within response.
    /// </summary>
    /// <param name="file">File to send</param>
    protected void SendFile(CMSOutputForumAttachment file)
    {
        // Clear response.
        CookieHelper.ClearResponseCookies();
        Response.Clear();

        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

        if (file != null)
        {
            // Redirect if the file should be redirected
            if (file.RedirectTo != "")
            {
                URLHelper.Redirect(file.RedirectTo, true, CurrentSiteName);
            }

            // Prepare etag
            string etag = file.LastModified.ToString();

            // Client caching - only on the live site
            if (useClientCache && AllowCache && CacheHelper.CacheImageEnabled(CurrentSiteName) && ETagsMatch(etag, file.LastModified))
            {
                RespondNotModified(etag, true);
                return;
            }

            // If physical file not present, try to load
            if (file.PhysicalFile == null)
            {
                EnsurePhysicalFile(outputFile);
            }

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

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

                // Prepare response
                Response.ContentType = mimetype;
                SetDisposition(file.ForumAttachment.AttachmentFileName, extension);

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

                if (useClientCache && AllowCache && (CacheHelper.CacheImageEnabled(CurrentSiteName)))
                {
                    DateTime expires = DateTime.Now;

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

                    Response.Cache.SetCacheability(HttpCacheability.Public);
                    if (DocumentBase.AllowClientCache() && DocumentBase.UseFullClientCache)
                    {
                        expires = DateTime.Now.AddMinutes(CacheHelper.CacheImageMinutes(CurrentSiteName));
                    }

                    Response.Cache.SetExpires(expires);
                    Response.Cache.SetETag(etag);
                }

                // Add the file data
                if ((file.PhysicalFile != "") && (file.OutputData == null))
                {
                    // If the output data should be cached, return the output data
                    bool cacheOutputData = false;
                    if (file.ForumAttachment != null)
                    {
                        cacheOutputData = CacheHelper.CacheImageAllowed(CurrentSiteName, file.ForumAttachment.AttachmentFileSize);
                    }

                    // 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
            {
                NotFound();
            }
        }
        else
        {
            NotFound();
        }

        CompleteRequest();
        //RequestHelper.EndResponse();
    }
    /// <summary>
    /// Sets the last modified and expires header to the response
    /// </summary>
    /// <param name="file">Output file data</param>
    private void SetTimeStamps(CMSOutputForumAttachment file)
    {
        DateTime expires = DateTime.Now;

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

        // Setup the client cache
        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(CMSOutputForumAttachment file)
    {
        // Clear response.
        CookieHelper.ClearResponseCookies();
        Response.Clear();

        // Set the revalidation
        SetRevalidation();

        if (file != null)
        {
            // Redirect if the file should be redirected
            if (file.RedirectTo != "")
            {
                URLHelper.RedirectPermanent(file.RedirectTo, CurrentSiteName);
            }

            // Prepare etag
            string etag = file.LastModified.ToString();

            // Client caching - only on the live site
            if (useClientCache && AllowCache && ETagsMatch(etag, file.LastModified))
            {
                // Set correct response content type
                SetResponseContentType(file);

                SetTimeStamps(file.LastModified);

                RespondNotModified(etag);
                return;
            }

            // If physical file not present, try to load
            if (file.PhysicalFile == null)
            {
                EnsurePhysicalFile(outputFile);
            }

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

            // Send the file
            if ((file.OutputData != null) || (file.PhysicalFile != ""))
            {
                // Set correct response content type
                SetResponseContentType(file);

                string extension = file.ForumAttachment.AttachmentFileExtension;
                SetDisposition(file.ForumAttachment.AttachmentFileName, extension);

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

                if (useClientCache && AllowCache)
                {
                    SetTimeStamps(file.LastModified);

                    Response.Cache.SetETag(etag);
                }
                else
                {
                    SetCacheability();
                }

                // Add the file data
                if ((file.PhysicalFile != "") && (file.OutputData == null))
                {
                    if (!File.Exists(file.PhysicalFile))
                    {
                        // File doesn't exist
                        NotFound();
                    }
                    else
                    {
                        // If the output data should be cached, return the output data
                        bool cacheOutputData = false;
                        if (file.ForumAttachment != null)
                        {
                            cacheOutputData = CacheHelper.CacheImageAllowed(CurrentSiteName, file.ForumAttachment.AttachmentFileSize);
                        }

                        // 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
            {
                NotFound();
            }
        }
        else
        {
            NotFound();
        }

        CompleteRequest();
    }