image caching

Jul 2, 2013 at 7:23 AM
Hi, I have combined tinymce 4, elfinder 2 wih your connector for editing pages. I see all images downloaded through a connector, I worry about are these images cachable on client side.

Eldar
Coordinator
Jul 2, 2013 at 9:43 AM
Edited Jul 2, 2013 at 9:44 AM
Hi, Eldar.
If images downloaded throuth connector and their urls looks like: http://localhost:8080/connector?cmd=file&target=v2_XHVudGl0bGVkIGZvbGRlclxHcmFwaHMgaW4gUFBULnBuZw2 that clients cache is disabled, because at this moment in DownloadResult not impemented caching.
In this case you have several options:
1) Provide a public url to root (second argument in Root constructor). In this case image will be download througth this public url and cache will be enabled (like for Content folder).
2) You can modify DownloadResult like this:

        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;
            HttpRequestBase request = context.HttpContext.Request;
            if (!HttpCacheHelper.IsFileFromCache(File, request, response))
            {

                string fileName;
                string fileNameEncoded = HttpUtility.UrlEncode(File.Name);

                if (context.HttpContext.Request.UserAgent.Contains("MSIE")) // IE < 9 do not support RFC 6266 (RFC 2231/RFC 5987)
                {
                    fileName = "filename=\"" + fileNameEncoded + "\"";
                }
                else
                {
                    fileName = "filename*=UTF-8\'\'" + fileNameEncoded; // RFC 6266 (RFC 2231/RFC 5987)
                }
                string mime;
                string disposition;
                if (IsDownload)
                {
                    mime = "application/octet-stream";
                    disposition = "attachment; " + fileName;
                }
                else
                {
                    mime = Helper.GetMimeType(File);
                    disposition = (mime.Contains("image") || mime.Contains("text") || mime == "application/x-shockwave-flash" ? "inline; " : "attachment; ") + fileName;
                }

                response.ContentType = mime;
                response.AppendHeader("Content-Disposition", disposition);
                response.AppendHeader("Content-Location", File.Name);
                response.AppendHeader("Content-Transfer-Encoding", "binary");
                response.AppendHeader("Content-Length", File.Length.ToString());
                response.WriteFile(File.FullName);
                response.End();
                response.Flush();
            }
            else
            {
                response.ContentType = IsDownload ? "application/octet-stream" : Helper.GetMimeType(File);
                response.End();
            }
        }
But in this case I can not guarantee a proper caching, especially if property IsDownload is sets. This method needs to test.

Regards, Evgeny
Jul 2, 2013 at 11:18 AM
Thank you for a reply,

I have tried the first case. This is what i wanted.
I have made two changes for test project and elfinder return to tinymce a path to my virtual directory:
in FilesController.cs I replace
Alias = "My documents",
by
Alias = "/Files",
and in tinymce -elfinder integration javascript to use item path instead item url

I will try to deploy and test the second case when the other stuff will be ready.
Sep 3, 2013 at 3:31 PM
Change property Url in Root.cs
/// <summary>
        /// Get or sets url that points to path directory (also called 'root URL'). 
        /// </summary>
        //public string Url { get; set; }
        public string Url { get { return _url; } set { _url = value; } }
and pass public in second parameter of root
void Init(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            key = "News";
        }
        key = @"~/Content/fileuploader/" + key;
        if (_connector == null)
        {
            FileSystemDriver driver = new FileSystemDriver();
            DirectoryInfo thumbsStorage = new DirectoryInfo(Server.MapPath(key));

            driver.AddRoot(new Root(new DirectoryInfo(Server.MapPath(key)), key.Substring(1) + "/")
            {
                Alias = "My documents",                    
                ThumbnailsStorage = thumbsStorage,
                MaxUploadSizeInMb = 2.2,
                ThumbnailsUrl = "Thumbnails/"                  
            });
            _connector = new Connector(driver);
        }
    }
OR using direct set property URL

void Init(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            key = "News";
        }
        key = @"~/Content/fileuploader/" + key;
        if (_connector == null)
        {
            FileSystemDriver driver = new FileSystemDriver();
            DirectoryInfo thumbsStorage = new DirectoryInfo(Server.MapPath(key));

            driver.AddRoot(new Root(new DirectoryInfo(Server.MapPath(key)), key.Substring(1) + "/")
            {
                Alias = "My documents",                    
                ThumbnailsStorage = thumbsStorage,
                MaxUploadSizeInMb = 2.2,
                ThumbnailsUrl = "Thumbnails/"
                ,Url =ResolveServerUrl(VirtualPathUtility.ToAbsolute(key),false)+"/"
            });
            _connector = new Connector(driver);
        }
    }