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

        SetRevalidation();

        if ((file != null) && file.IsValid)
        {
            // Prepare etag in ""
            string etag = "\"" + file.SharePointFilePath + "\"";

            // 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 the output data should be cached, return the output data
            bool cacheOutputData = false;
            if (file.OutputData != null)
            {
                cacheOutputData = CacheHelper.CacheImageAllowed(CurrentSiteName, file.OutputData.Length);
            }

            // Ensure the file data
            if (!file.DataLoaded)
            {
                byte[] cachedData = GetCachedOutputData();

                // Ensure data are retrieved from SharePoint
                if (file.EnsureData(cachedData))
                {
                    if ((cachedData == null) && cacheOutputData)
                    {
                        SaveOutputDataToCache(file.OutputData, null);
                    }
                }
            }

            // Send the file
            if (file.OutputData != null)
            {
                byte[] data;

                // Check if the request is for partial data (Range HTTP header)
                long[,] rangePosition = GetRange(file.OutputData.Length, HttpContext.Current);

                // Send all file contens
                if (rangePosition.GetUpperBound(0) == 0)
                {
                    // Set correct response content type
                    SetResponseContentType(file);

                    // get file name without the path
                    string fileName = Path.GetFileName(file.SharePointFilePath);

                    SetDisposition(fileName, file.FileExtension);

                    // Setup Etag property
                    ETag = etag;

                    // Set if resumable downloads should be supported
                    AcceptRange = !IsExtensionExcludedFromRanges(file.FileExtension);

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

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

                    data = file.OutputData;
                }
                // Send partial contens
                else
                {
                    data = new byte[file.OutputData.Length - rangePosition[0, ResponseDataSender.RANGE_START]];

                    // Get part of file
                    Array.Copy(file.OutputData, rangePosition[0, ResponseDataSender.RANGE_START], data, 0, data.Length);
                }

                // Use output data of the file in memory if present
                WriteBytes(data);
            }
            else
            {
                NotFound();
            }
        }
        else
        {
            NotFound();
        }

        CompleteRequest();
    }
Пример #2
0
    /// <summary>
    /// Sends the given file within response.
    /// </summary>
    /// <param name="file">File to send</param>
    protected void SendFile(CMSOutputSharePointFile file)
    {
        // Clear response.
        CookieHelper.ClearResponseCookies();
        Response.Clear();

        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

        if ((file != null) && file.IsValid)
        {
            // Prepare etag in ""
            string etag = "\"" + file.SharePointFilePath + "\"";

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

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

            // Ensure the file data
            if (!file.DataLoaded)
            {
                byte[] cachedData = GetCachedOutputData();

                // Ensure data are retrieved from SharePoint
                if (file.EnsureData(cachedData))
                {
                    if ((cachedData == null) && cacheOutputData)
                    {
                        SaveOutputDataToCache(file.OutputData, null);
                    }
                }
            }

            // Send the file
            if (file.OutputData != null)
            {
                byte[] data = null;

                // Check if the request is for partial data (Range HTTP header)
                long[,] rangePosition = GetRange(file.OutputData.Length, HttpContext.Current);

                // Send all file contens
                if (rangePosition.GetUpperBound(0) == 0)
                {
                    // Setup the mime type - Fix the special types
                    string mimetype = file.MimeType;
                    switch (file.FileExtension.ToLower())
                    {
                    case ".flv":
                        mimetype = "video/x-flv";
                        break;
                    }

                    // Prepare response
                    Response.ContentType = mimetype;

                    // get file name without the path
                    string fileName = Path.GetFileName(file.SharePointFilePath);

                    SetDisposition(fileName, file.FileExtension);

                    // Setup Etag property
                    ETag = etag;

                    // Set if resumable downloads should be supported
                    AcceptRange = !IsExtensionExcludedFromRanges(file.FileExtension);

                    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);
                    }

                    data = file.OutputData;
                }
                // Send partial contens
                else
                {
                    data = new byte[file.OutputData.Length - rangePosition[0, RANGE_START]];

                    // Get part of file
                    Array.Copy(file.OutputData, rangePosition[0, RANGE_START], data, 0, data.Length);
                }

                // Use output data of the file in memory if present
                WriteBytes(data);
            }
            else
            {
                NotFound();
            }
        }
        else
        {
            NotFound();
        }

        CompleteRequest();
    }