+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / physical upload of files in Images and documents

physical upload of files in Images and documents

53 posts, 0 answered
  1. Nauman
    Nauman avatar
    35 posts
    Registered:
    28 Jul 2008
    19 May 2009
    Link to this post
    Hi

    Is there any way in the images and documents module to upload files physically on server and track all the information in documents module instead of uploading file in database, as you know some files are too big in size and database size grows extremely large.

    Thanks
    Nauman Ahmed
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    19 May 2009
    Link to this post
    Hello Nauman,

    You could try the following.


    1. In global.asax file you should subscribe for ContentManager executing event. The using Sitefinity API check the provider and when an image is uploaded save it to specific path.

    void Application_Start(object sender, EventArgs e)  
        { 
            Telerik.Cms.Engine.ContentManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(ContentManager_Executing); 
     
        } 
     
        void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e) 
        { 
            if (sender is Telerik.Libraries.Data.DefaultProvider) 
            { 
                Telerik.Libraries.Data.DefaultProvider provider = sender as Telerik.Libraries.Data.DefaultProvider; 
                if (e.CommandName == "CreateContent"
                { 
                    Telerik.Cms.Engine.IContent libraryItem = e.CommandArguments as Telerik.Cms.Engine.IContent; 
                    if (libraryItem != null ) 
                    { 
                        string directoryPath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name); 
                        if (!System.IO.Directory.Exists(directoryPath)) 
                        { 
                            System.IO.Directory.CreateDirectory(directoryPath); 
                        } 
                        string storePath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name + "/" + libraryItem.ID + ".img"); 
                        using ( System.IO.FileStream fileStream =  
                            new System.IO.FileStream(storePath, System.IO.FileMode.CreateNew)) 
                        { 
                            byte[] data = (byte[])libraryItem.Content; 
                            fileStream.Write(data, 0, (int)data.Length); 
                        } 
                        libraryItem.Content = new byte[0]; 
                    } 
                } 
            } 
        } 
         


    2. Override Telerik.Cms.Engine.ContentHttpHandler.  and save the item in the database with  zero byte.

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using Telerik.Utilities; 
    using System.IO; 
     
    namespace Telerik.Cms.Engine 
        /// <summary> 
        /// Summary description for CustomCmsHttpHandler 
        /// </summary> 
        public class CustomCmsHttpHandler : ContentHttpHandler 
        { 
            public CustomCmsHttpHandler() 
            { 
     
            } 
     
            protected override object ProcessContentItem(IContent content, HttpRequest request) 
            { 
                object data = content.Content; 
                if (content.ProviderName == "Libraries"
                { 
                    string storePath = HttpContext.Current.Server.MapPath("~/Files/" + content.ProviderName + "/" + content.ID + ".img"); 
                    if (File.Exists(storePath)) 
                    { 
                        using (System.IO.FileStream fileStream = 
                            new System.IO.FileStream(storePath, System.IO.FileMode.Open)) 
                        { 
                            byte[] temp = new byte[fileStream.Length]; 
                            fileStream.Read(temp, 0, (int)fileStream.Length); 
                            data = temp; 
                        } 
                    } 
                } 
                if (data is byte[]) 
                { 
                    if (!String.IsNullOrEmpty(request.QueryString["width"]) && 
                        !String.IsNullOrEmpty(request.QueryString["height"])) 
                    { 
                        try 
                        { 
                            data = this.ResizeImage((byte[])data, content.MimeType, request); 
                        } 
                        catch (Exception ex) 
                        { 
                            Log.Exception(ex); 
                        } 
                    } 
                } 
                return data; 
            } 
        } 

    3. Then in your web.config file replace the default one with your custom Handler where it is used.

    Sample:

    replace the default 
     
    <add verb="GET" path="*.sflb" type="Telerik.Cms.Engine.ContentHttpHandler, Telerik.Cms.Engine"/> 
     
    with our custom 
    <add verb="GET" path="*.sflb" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/> 
     


    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Nauman
    Nauman avatar
    35 posts
    Registered:
    28 Jul 2008
    19 May 2009
    Link to this post
    Hi Ivan Dimitrov

    I was really existed to get quick reply and when i  implement it, i got some problems

    In Sitefinity 3.6 it works well, it created a library folder inside the files folder and save my images files.

    Issues

    1) It convert all custom file e.g. pdg, swf  to image format

    11)I can not able to delete any files from CMS

    So  update the Sitefinity 3.6 to 3.6SP1 ver 3_6_1927

    After updating i can not able to save the file on the disk, Sitefinity is again saving file in database

    In Sitefinity 3.5 i am getting this error
    CS0115: 'Telerik.Cms.Engine.CustomCmsHttpHandler.ProcessContentItem(Telerik.Cms.Engine.IContent, System.Web.HttpRequest)': no suitable method found to override
    Line 20:         protected override object ProcessContentItem(IContent content, HttpRequest request) 
    Line 21:         { 
    Line 22:             object data = content.Content; 

    Please guid me.

    Thanks
    Nauman Ahmed


  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    19 May 2009
    Link to this post
    Hello Nauman,

    Here are the answers:

    1) The sample code demonstrates saving on image files only. You can extend the code subscribing for ContentManager.Executed event.
    Then you can create a string where you get the content Extension as a MetaData - here you get the file without its extension  ( on Executing is too earlier for this) and set the extension.

    string newPath = storePath.Substring(0,storePath.LastIndexOf(".")) + content.GetMetaData("Extension"); 
    System.IO.File.Move(storePath, newPath); 

    2. I am not able to reproduce the problem regarding deleting the images with the last official release.
    3. The implementation for 3.5 is different - there is no parameter request.

    Here is modified code for 3.5

            protected override void ProcessContentItem(IContent content) 
            { 
                string metaData = content.GetMetaData("Name"as string
                if (string.IsNullOrEmpty(metaData)) 
                { 
                    metaData = content.GetMetaData("Title"as string
                } 
                string extension = content.GetMetaData("Extension"as string
                this.FillGeneralInfo(content.MimeType, content.DateModified, metaData, extension); 
                if ((!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["width"]) && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["height"])) && (content.Content is byte[])) 
                { 
                    try 
                    { 
                        byte[] temp = (byte[])content.Content; 
                        if (content.ProviderName == "Libraries"
                        { 
     
                            string storePath = HttpContext.Current.Server.MapPath("~/Files/" + content.ProviderName + "/" + content.ID + extension); 
                            if (File.Exists(storePath)) 
                            { 
                                using (System.IO.FileStream fileStream = 
                                    new System.IO.FileStream(storePath, System.IO.FileMode.Open)) 
                                { 
                                    temp = new byte[fileStream.Length]; 
                                    fileStream.Read(temp, 0, (int)fileStream.Length); 
     
                                } 
                            } 
                        } 
                        Image img = Image.FromStream(new MemoryStream((byte[])temp)); 
                        HttpContext.Current.Response.BinaryWrite(this.ProcessImage(img, content.MimeType)); 
                    } 
                    catch 
                    { 
                        HttpContext.Current.Response.BinaryWrite((byte[])content.Content); 
                    } 
                } 
                else if (content.Content is byte[]) 
                { 
                    HttpContext.Current.Response.BinaryWrite((byte[])content.Content); 
                } 
                else 
                { 
                    HttpContext.Current.Response.Write(content.Content); 
                } 
     
     
            } 


    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Nauman
    Nauman avatar
    35 posts
    Registered:
    28 Jul 2008
    21 May 2009
    Link to this post
    Hi Ivan Dimitrov

    Thanks for your reply and i implemented the given code on Sitefinity 3.5 and it works perfectly

    In Sitefinity 3.5 if you delete the any document in cms it does not delete physically and same it does not delete any record from database table
    i checked these two tables
    db.sf_CmsContentThumbnail and db.sf_GCMetaData


    In Sitefinity 3.6  SP1 build 1927, i tried in couple of projects your first post solution never worked.


    In Sitefinity 3.6 Sp2 build 1936, it worked and i can save the file on the disk in right format but i am  not able to download the file

    Can you please provide me the modified code for this method
    protected override object ProcessContentItem(IContent content, HttpRequest request)
    {
    // modified code pls :)
    }
    which will cover all type of files  as you send me for the 3.5, as  i can not able to modified it :(

    Another question, is there any possible way in cms where i chosse opiton
    e.g save  i) file in databse  ii)Save file on disk

    Many Thanks
    Nauman Ahmed
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    21 May 2009
    Link to this post
    Hello Nauman,

    Here is the sample code:


            protected override object ProcessContentItem(IContent content, HttpRequest request) 
            { 
                object data = content.Content; 
                if (content.ProviderName == "Libraries"
                { 
                    string storePath = HttpContext.Current.Server.MapPath("~/Files/" + content.ProviderName + "/" + content.ID + content.GetMetaData("Extension")); 
                    if (File.Exists(storePath)) 
                    { 
                        using (System.IO.FileStream fileStream = 
                            new System.IO.FileStream(storePath, System.IO.FileMode.Open)) 
                        { 
                            byte[] temp = new byte[fileStream.Length]; 
                            fileStream.Read(temp, 0, (int)fileStream.Length); 
                            data = temp; 
                        } 
                    } 
                } 
                if (data is byte[]) 
                { 
                    if (!String.IsNullOrEmpty(request.QueryString["width"]) && 
                        !String.IsNullOrEmpty(request.QueryString["height"])) 
                    { 
                        try 
                        { 
                            data = this.ResizeImage((byte[])data, content.MimeType, request); 
                        } 
                        catch (Exception ex) 
                        { 
                            Log.Exception(ex); 
                        } 
                    } 
                } 
                return data; 
            } 

    You can made some changes in the global.asax file. Here we use System.IO.File.Move to rename the file.

     void ContentManager_Executed(object sender, Telerik.ExecutedEventArgs e) 
        { 
            if (HttpContext.Current.Items.Contains("savedImage")) 
            { 
                Telerik.Cms.Engine.IContent content =  HttpContext.Current.Items["contentItem"as Telerik.Cms.Engine.IContent; 
                string storePath = HttpContext.Current.Items["savedImage"as string ; 
                string newPath = storePath.Substring(0,storePath.LastIndexOf(".")) + content.GetMetaData("Extension"); 
                System.IO.File.Move(storePath, newPath); 
                HttpContext.Current.Items.Remove("savedImage"); 
                HttpContext.Current.Items.Remove("contentItem"); 
            } 
        } 
     
        void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e) 
        { 
            if (sender is Telerik.Libraries.Data.DefaultProvider) 
            { 
                Telerik.Libraries.Data.DefaultProvider provider = sender as Telerik.Libraries.Data.DefaultProvider; 
                if (e.CommandName == "CreateContent"
                { 
                    Telerik.Cms.Engine.IContent libraryItem = e.CommandArguments as Telerik.Cms.Engine.IContent; 
                    if (libraryItem != null ) 
                    { 
                        string directoryPath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name); 
                        if (!System.IO.Directory.Exists(directoryPath)) 
                        { 
                            System.IO.Directory.CreateDirectory(directoryPath); 
                        } 
                        string storePath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name + "/" + libraryItem.ID + ".img"); 
                        using ( System.IO.FileStream fileStream =  
                            new System.IO.FileStream(storePath, System.IO.FileMode.CreateNew)) 
                        { 
                            byte[] data = (byte[])libraryItem.Content; 
                            fileStream.Write(data, 0, (int)data.Length); 
                            HttpContext.Current.Items["savedImage"] = storePath; 
                            HttpContext.Current.Items["contentItem"] = libraryItem; 
                        } 
                        libraryItem.Content = new byte[0]; 
                       
                    } 
                } 
            } 
        } 
         

    Also, there could be a record in the database but the physical file will be in the directory you have specified.

    There is no option from where you can select whether your files to be saved. This is the reason why we are using custom handler and replace the existing one.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Nauman
    Nauman avatar
    35 posts
    Registered:
    28 Jul 2008
    21 May 2009
    Link to this post
    Thanks Ivan Dimitrov

    For providing the detail code. I can save the file with the right format on the disk, but when i drop the download list control, i can not able to download the file.

    url format in the hypelink is like this
    /Libraries/Document_Library/first_pdf_file.sflb.ashx?download=true


    Image gallery control is working well. Please suggest further

    Thanks
    Nauman Ahmed
  8. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    24 May 2009
    Link to this post
    I tried to copy/paste all the pieces off code in the right file (global.asax) but it turns up with an buch of errors.

    IContent not found

    I figured out I needed to use:
    protected override object ProcessContentItem(Telerik.Cms.Engine.IContent content, HttpRequest request)

    This resulted into an error: NOTHING TO OVERRIDE

    Disabling this function will result in no displayed images.

    Uploading is making an .IMG file on the server, but with the wrong extention (img instead off the correct extention).

    And the binary data is also still in the database... what am I doing wrong?

    I have both functions:
    ContentManager_Executing
    ContentManager_Executed

    Version 3.6.1936
  9. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    24 May 2009
    Link to this post
    I added this line in APPLICATION_START and the correct extentions is solved... But still the data is also saved into the DB.

    Telerik.Cms.Engine.ContentManager.Executed += new EventHandler<Telerik.ExecutedEventArgs>(ContentManager_Executed);

    What is going wrong?
  10. Nauman
    Nauman avatar
    35 posts
    Registered:
    28 Jul 2008
    24 May 2009
    Link to this post
    Hi Frank

    Try this  Ivan Dimitrov provided code with little modification in  Globa.ascx

    void Application_Start(object sender, EventArgs e)  
        {
            Telerik.Cms.Engine.ContentManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(ContentManager_Executing);
     
        }
     
      void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e)
        {
            if (sender is Telerik.Libraries.Data.DefaultProvider)
            {
                Telerik.Libraries.Data.DefaultProvider provider = sender as Telerik.Libraries.Data.DefaultProvider;
                if (e.CommandName == "CreateContent")
                {
                    Telerik.Cms.Engine.IContent libraryItem = e.CommandArguments as Telerik.Cms.Engine.IContent;
                   
                    if (libraryItem != null )
                    {
                        string extension = libraryItem.GetMetaData("Extension") as string;
                        string directoryPath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name);
                        if (!System.IO.Directory.Exists(directoryPath))
                        {
                            System.IO.Directory.CreateDirectory(directoryPath);
                        }
                        string storePath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name + "/" + libraryItem.ID + extension);
                        using ( System.IO.FileStream fileStream =  
                            new System.IO.FileStream(storePath, System.IO.FileMode.CreateNew))
                        {
                            byte[] data = (byte[])libraryItem.Content;
                            fileStream.Write(data, 0, (int)data.Length);
                        }
                        libraryItem.Content = new byte[0];
                    }
                }
            }
        }

    Second create a class CustomCmsHttpHandler in App_Code, use code as listed in the post

    Third change the web config as mentioned in the post

    It will save file in on the disk and send zero bite to database and rest all information will be save in database

    Issues
    It works fine for image gallery, but download list control is not working with this, as  i am also waiting Sitefinity reply.

    But you can use there API to create your own control.

    Thanks
    Nauman Ahmed


  11. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    24 May 2009
    Link to this post
    This is working so far (same state as I got yesterday), but when I check the DB size (specially the sf_CmsBinaryContent table) it size is as big as the file size of the images on file structure.

    And changing the format (pixels) will result in changing the DB size, but on file level nothing is changed at all.

    So something is still going wrong and is still pointing to the DB.

    We want to become Sitefinity partners, but we MUST save the files on disk and not into the DB.

    Hope to get answers from you if the situation at your instance is the same.
  12. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    24 May 2009
    Link to this post
    After uploading an image the DB size is grown a little (for registration of the image), but after changing some info the DB size is changed and the complete binary info is saved into the DB.

    So not only on CREATECONTENT, but also on UPDATECONTENT the same function change must be made..

    Same goes for DELETING the image. The image is still on the disk while its deleted from the DB.

    Somebody got any ideas?
  13. Dido
    Dido avatar
    149 posts
    Registered:
    24 Sep 2012
    26 May 2009
    Link to this post
    Hello Frank,

    Definitely the safest way is just to implement a new provider for Images & Documents. What Ivan showed you was a quick solution. Since providers in ASP.NET are used exactly for this - storage - you should implement one for the file system.

    You will need to inherit Telerik.Cms.Engine.ContentProviderBase and implement Telerik.Libraries.ILibraryProvider.

    As an alternative, you can inherit Telerik.Libraries.Data.DefaultProvider and override the following methods:

     
    public override IContent CreateContent(string mimeType, Guid id) 
    public override void SaveContent(IContent content, bool fireEvents) 
    public override void DeleteContent(IContent content, bool fireEvents) 
    public override IContent GetContentWithCurrentlLanguage(Guid id) 
    public override IContent GetContent(Guid id, string itemInfo) 
    public override IList GetContent(int from, int max, string sortExp, string tagName, ContentStatus? status, string itemInfo, SearchCondition itemInfoCondition, Guid[] parentIDs, params IMetaSearchInfo[] filter) 
    public override IContent GetContent(string url) 
    public override int ContentCount(ContentStatus? status, Guid[] parentIDs, params IMetaSearchInfo[] filter) 
    public override int ContentCount(ContentStatus? status, Guid[] parentIDs, Guid tagId, params IMetaSearchInfo[] filter) 
    public override IThumbnail AddThumbnail(IContent content, byte[] thumbData, string mimeType, string extension, string name, bool deleteExistingThumbnails) 
    public override void SaveThumbnail(IThumbnail thumbnail) 
    public override void IncreaseViewCount(IContent content) 
    public override int GetViewCount(IContent content) 

    Of course, you can omit everything that you want to be kept in the database.

    Greetings,
    Dido
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  14. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    26 May 2009
    Link to this post
    Hmmm.. making my own provider.. sounds difficult for somebody who is new to Sitefinity.

    So the code I copied/modified from the Forum isn't completed and I need to make an new provider?!?!

    Isn't it possible to extend the current global.asax which I have right now?

    and after that is working, I will wait to the new version of sitefinity which is having this functionality by default.

    I mean.. everything may be saved in the database (viewcount, etc.) and may be registered there, but only the image must be saved on disk instead of the DB. Isn't that possible by modifing global.asax?

    When I need to make my own provider I need to program every functionality myself. That isnt why we are using and CMS like sitefinity :P

    Can you explain what functions I need to make/modify using the global.asax and what must be done when I choose for the new provider?
  15. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    30 May 2009
    Link to this post
    Hello Frank,

    Here is the updated implementation with DelteContent as CommandArgument.

    Golbal.asax

    void ContentManager_Executed(object sender, Telerik.ExecutedEventArgs e) 
        { 
            if (HttpContext.Current.Items.Contains("savedImage")) 
            { 
                Telerik.Cms.Engine.IContent content = HttpContext.Current.Items["contentItem"as Telerik.Cms.Engine.IContent; 
                string storePath = HttpContext.Current.Items["savedImage"as string
                string newPath = storePath.Substring(0, storePath.LastIndexOf(".")) + content.GetMetaData("Extension"); 
                System.IO.File.Move(storePath, newPath); 
                HttpContext.Current.Items.Remove("savedImage"); 
                HttpContext.Current.Items.Remove("contentItem"); 
            } 
        } 
     
        void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e) 
        { 
            if (sender is Telerik.Libraries.Data.DefaultProvider) 
            { 
                Telerik.Libraries.Data.DefaultProvider provider = sender as Telerik.Libraries.Data.DefaultProvider; 
                if (e.CommandName == "CreateContent"
                { 
                    Telerik.Cms.Engine.IContent libraryItem = e.CommandArguments as Telerik.Cms.Engine.IContent; 
                    if (libraryItem != null
                    { 
                        string directoryPath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name); 
                        if (!System.IO.Directory.Exists(directoryPath)) 
                        { 
                            System.IO.Directory.CreateDirectory(directoryPath); 
                        } 
                        string storePath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name + "/" + libraryItem.ID + ".img"); 
                        using (System.IO.FileStream fileStream = 
                            new System.IO.FileStream(storePath, System.IO.FileMode.CreateNew)) 
                        { 
                            byte[] data = (byte[])libraryItem.Content; 
                            fileStream.Write(data, 0, (int)data.Length); 
                            HttpContext.Current.Items["savedImage"] = storePath; 
                            HttpContext.Current.Items["contentItem"] = libraryItem; 
                        } 
                        libraryItem.Content = new byte[0]; 
     
                    } 
                } 
                else if (e.CommandName == "DeleteContent"
                { 
                    Telerik.Cms.Engine.IContent libraryItem = e.CommandArguments as Telerik.Cms.Engine.IContent; 
                    if (libraryItem != null
                    { 
                        string storePath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name + "/" + libraryItem.ID + libraryItem.GetMetaData("Extension")); 
                        if (System.IO.File.Exists(storePath)) 
                        { 
                            System.IO.File.Delete(storePath); 
                        } 
                    } 
                } 
            } 
        }  

    Note that the default handler should be replaced with the custom one in the web.config file

    <httpHandlers>  
          <add verb="*" path="Telerik.RadUploadProgressHandler.ashx" type="Telerik.Web.UI.Upload.RadUploadProgressHandler, Telerik.Web.UI"/>  
          <add verb="*" path="*.rss" type="Telerik.Rss.RssHttpHandler, Telerik.Rss"/>  
          <add verb="*" path="*.rss.ashx" type="Telerik.Rss.RssHttpHandler, Telerik.Rss"/>  
          <add verb="GET" path="*.sflb" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/>  
          <add verb="GET" path="*.sflb.ashx" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/>  
          <add verb="GET" path="*.tmb" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/>  
          <add verb="GET" path="*.tmb.ashx" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/>  
      
      
    .......  
      
    </httpHandlers>  

    or (for integrated mode)

    <handlers> 
    ... 
          <add name="SitefinityThumbnail" path="*.tmb" verb="*" preCondition="integratedMode" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/> 
          <add name="SitefinityThumbnailAdd" path="*.tmb.ashx" verb="*" preCondition="integratedMode" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/> 
          <add name="SitefinityLibrary" path="*.sflb" verb="*" preCondition="integratedMode" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/> 
          <add name="SitefinityLibraryAdd" path="*.sflb.ashx" verb="*" preCondition="integratedMode" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/> 
    .... 
    </handlers> 

    depending on the pipeline mode you are running. Also, the order of the entries in the <httpHandlers> section matters. The section is parsed from top to bottom

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  16. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    02 Jun 2009
    Link to this post
    why are the EXECUTING and EXECUTED function separeted again?
    In the previous code part the renaming of the image (extention) was done in the EXECUTING part. Now it is done in the EXECUTED part.

    And as far as I can see, still the problem will occure with UPDATING the image. When I UPLOAD an image now, this is saved on disk. When I update/change an image, it is saved in the database.

    Can this be changed?
    (and keep is as simple as possible... So keep in executing what can be done in executing, and not be moved to the executed function.)

    I got the idea that 3 different sollutions are posted in this thread for 3 different parts of the problem (save image on disk, change extention, update image on disk, etc.)
     
    Can this be done in one complete function, so sitefinity will work as optimized as possible, and the image is saved/updated/deleted on disk.

    Would love to hear the answer.
  17. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    03 Jun 2009
    Link to this post
    Deleting the image is just working fine now. But the EDIT option is still not working correctly.

    When I upload an image, the image is saved on disk and null byte Binaryvalue is saved into the Database.

    But when I click SAVE CHANGES (in the screen which appears after selecting the uploaded image) the BinaryValue isn't null bytes anymore.

    Also changing the width/height from the image will result in an big BinaryValue in the database. And the image on disk isn't overwritten (stays the same width and height).

    The images processed in the Generic content editor are also the old size. Let's for example say I upload an image from 1024px*786px. I downscale it to 400px*300px, then sitefinity is telling me the image IS 400px*300px, but when I use it in the editor, the image is loaded from the disk and is still 1024px*786px.

    So I think it is still not working when updating an image?
  18. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    03 Jun 2009
    Link to this post
    the code below those what we all want except one thing, but is discussed later.

    The next piece of code does :
    - Upload images to disk and save reference in the database.
    - Delete images from disk and database.
    - Change images on disk and save new width/height in the database.

    GLOBAL.ASAX
        //Executing function, when uploading images  
        void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e)  
        {  
            if (sender is Telerik.Libraries.Data.DefaultProvider)  
            {  
     
                Telerik.Libraries.Data.DefaultProvider provider = sender as Telerik.Libraries.Data.DefaultProvider;  
                if (e.CommandName == "CreateContent")  
                {  
                    Telerik.Cms.Engine.IContent libraryItem = e.CommandArguments as Telerik.Cms.Engine.IContent;  
     
                    if (libraryItem != null)  
                    {  
                        string extension = libraryItem.GetMetaData("Extension"as string;  
                        string directoryPath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name);  
                        if (!System.IO.Directory.Exists(directoryPath))  
                        {  
                            System.IO.Directory.CreateDirectory(directoryPath);  
                        }  
                        string storePath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name + "/" + libraryItem.ID + extension);  
                        using (System.IO.FileStream fileStream =  
                            new System.IO.FileStream(storePath, System.IO.FileMode.CreateNew))  
                        {  
                            byte[] data = (byte[])libraryItem.Content;  
                            fileStream.Write(data, 0, (int)data.Length);  
     
                            //HttpContext.Current.Items["savedImage"] = storePath;  
                            //HttpContext.Current.Items["contentItem"] = libraryItem;    
                              
                        }  
                        libraryItem.Content = new byte[0];  
                    }  
                }  
     
                  
     
                if (e.CommandName == "DeleteContent")  
                {  
                    Telerik.Cms.Engine.IContent libraryItem = e.CommandArguments as Telerik.Cms.Engine.IContent;  
                    if (libraryItem != null)  
                    {  
                        string storePath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name + "/" + libraryItem.ID + libraryItem.GetMetaData("Extension"));  
                        if (System.IO.File.Exists(storePath))  
                        {  
                            System.IO.File.Delete(storePath);  
                        }  
                    }  
                }  
     
                  
     
                if (e.CommandName == "UpdateContent")  
                {  
                    Telerik.Cms.Engine.IContent libraryItem = e.CommandArguments as Telerik.Cms.Engine.IContent;  
     
                    if (libraryItem != null)  
                    {  
                        string extension = libraryItem.GetMetaData("Extension"as string;  
                        string directoryPath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name);  
                                            
                        if (!System.IO.Directory.Exists(directoryPath))  
                        {  
                            System.IO.Directory.CreateDirectory(directoryPath);  
                        }  
                        string storePath = HttpContext.Current.Server.MapPath("~/Files/" + provider.Name + "/" + libraryItem.ID + extension);  
                        using (System.IO.FileStream fileStream =  
                            new System.IO.FileStream(storePath, System.IO.FileMode.Create))  
                        {  
                            byte[] data = (byte[])libraryItem.Content;  
                            fileStream.Write(data, 0, (int)data.Length);  
     
                            //HttpContext.Current.Items["savedImage"] = storePath;  
                            //HttpContext.Current.Items["contentItem"] = libraryItem;    
     
                        }  
                        libraryItem.Content = new byte[0];  
                    }  
                }  
            }  
        }  
     

    WEB.CONFIG
          <add verb="*" path="Telerik.RadUploadProgressHandler.ashx" type="Telerik.Web.UI.Upload.RadUploadProgressHandler, Telerik.Web.UI"/>  
          <add verb="*" path="*.rss" type="Telerik.Rss.RssHttpHandler, Telerik.Rss"/>  
          <add verb="*" path="*.rss.ashx" type="Telerik.Rss.RssHttpHandler, Telerik.Rss"/>  
            
          <add verb="GET" path="*.sflb" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/>  
          <add verb="GET" path="*.sflb.ashx" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/>  
          <add verb="GET" path="*.tmb" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/>  
          <add verb="GET" path="*.tmb.ashx" type="Telerik.Cms.Engine.CustomCmsHttpHandler, App_Code"/>  
     
     


    APP_CODE/CONTENTHTTPHANDLER.CS
        /// <summary>    
        /// Summary description for CustomCmsHttpHandler    
        /// </summary>    
        public class CustomCmsHttpHandler : ContentHttpHandler    
        {    
            public CustomCmsHttpHandler()    
            {    
        
            }  
     
     
            protected override object ProcessContentItem(IContent content, HttpRequest request)  
            {  
                object data = content.Content;  
                if (content.ProviderName == "Libraries")  
                {  
                    string storePath = HttpContext.Current.Server.MapPath("~/Files/" + content.ProviderName + "/" + content.ID + content.GetMetaData("Extension"));  
                    if (File.Exists(storePath))  
                    {  
                        using (System.IO.FileStream fileStream =  
                            new System.IO.FileStream(storePath, System.IO.FileMode.Open))  
                        {  
                            byte[] temp = new byte[fileStream.Length];  
                            fileStream.Read(temp, 0, (int)fileStream.Length);  
                            data = temp;  
                        }  
                    }  
                }  
                if (data is byte[])  
                {  
                    if (!String.IsNullOrEmpty(request.QueryString["width"]) &&  
                        !String.IsNullOrEmpty(request.QueryString["height"]))  
                    {  
                        try 
                        {  
                            data = this.ResizeImage((byte[])data, content.MimeType, request);  
                        }  
                        catch (Exception ex)  
                        {  
                            Log.Exception(ex);  
                        }  
                    }  
                }  
                return data;  
            }    
     

    The only thing which is not working right now is deleting an complete library including the content. The library is deleted, the database is cleaned up, but the images aren't deleted.

    What command is triggered when an library is deleted?
    Making an new library will trigger: SaveLibrary

    But I don't know what function is triggerd when deleting an libraby.

    Hope anybody can help.
  19. Parvan
    Parvan avatar
    151 posts
    Registered:
    24 Sep 2012
    05 Jun 2009
    Link to this post
    Hi Frank,

    The CommandName of event for deleting a library is DeleteLibrary, but it is currently only fired after deletion (Executed). Executed is fired after images are deleted from the database. In the next release of Sitefinity the Executing DeleteLibrary  event will be fired as well.

    Here is a workaround you can use:
    1. Copy the CustomLibrariesProvider.cs class in your App_Code folder. I added the logic for deleting library's images from the File System in this class. 

    2. Edit the web config file. Change the type of library provider (replace the green with yellow):
    BEFORE:
    <cmsEngine> 
    ... 
    <add name="Libraries" ... 
       type="Telerik.Libraries.Data.DefaultProvider, Telerik.Libraries.Data" /> 
    .... 
    </cmsEngine> 

    AFTER:
    <cmsEngine>  
    ...  
    <add name="Libraries" ...  
       type="Telerik.Libraries.Data.CustomLibrariesProvider, App_Code" />  
    ....  
    </cmsEngine>  
     


    I hope this helps you.

    All the best,
    Parvan
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  20. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    09 Jun 2009
    Link to this post
    Thanks Parvan,

    This is working fine.

    I still got 4 issues:
    - When I upload an ZIP-file containing several files (more then 25) only the first 25 are uploaded correctly (file size matches the original), the filesize of the other files from the batch will remain 0 (zero). (I think this has something to do with the paging-effect of the multiple upload screen.)

    - When I in am in the screen after uploading the file (the screen with the CANCEL and SAVE CHANGES buttons) and I don't click SAVE CHANGES then the filesize stays 0 kb (all the files). So the images are definitive uploaded wheb clicking the SAVE CHANGES button.

    - When I upload an image in an folder others the IMAGES, then I cannot change the width and height of the file during upload of when editing the image.

    - Is there a possibility to make an preset for the WIDTH and HEIGHT from an image during uploading?

    Hope this issues aren't that hard to fix, so the site can go live soon.

    When the customer is excited about the product and he dislikes the sitefinity logo, then we will surely buy the product :)
  21. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Jun 2009
    Link to this post
    Hello Frank,

    For the first 2 issues we need to investigate them further since this is a custom implementation and we are changing the default handlers.
    As for the resizing during upload - You should subscribe for LibraryManager.Executing. Then you should get the current item and make sure that it is image using (Telerik.Cms.Engine.ImagesHelper.IsBrowserImage(currentItem). Afterward  you can get the Height and Width metadata using  currentItem.GetMetaData("Width") currentItem.GetMetaData("Height"). Then create two integer values that will represents the desired width and height. Finally you need to use SetMetaData for size, width and height for the current item.

    I hope this helps.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  22. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    10 Jun 2009
    Link to this post
    And this is also working when not uploading in an default IMAGES-type folder?

    I mean, when I make an custom type, does it still work then?
  23. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Jun 2009
    Link to this post
    Hello Frank,

    The approach is using ImageHelper class, so you check the mime types for - gif, jpg, png,bmp using System.Drawing.Imaging.ImageFormat. So you are looking for a file that contains image information during the upload, not the library type.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  24. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    10 Jun 2009
    Link to this post
    Ok, but when I now upload an image into an different library, I cannot change the width and height when editting.

    Is this because of the width and height do not exist right now and will it be possible when the width and height are filled in?

    (And I hope you got an answer to my other problems too :P )
  25. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    15 Jun 2009
    Link to this post
    Hi Frank,

    I believe you will be able to resize the image once you set the Height and Width attributes. Please follow up if you find anything strange though.

    Sincerely yours,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  26. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    17 Jun 2009
    Link to this post
    This isn't working.

    When I create an library from an selfmade type, by adding the librarytype to the web.config and I call it, lets say FLASHGALERY and I want to upload images into it the given sollution, isn't working.

    When executing the following functions during upload, to get the current width and height of the image, no data is present.

     

    //string Breedte = libraryItem.GetMetaData("Width") as string;
    //string Hoogte = libraryItem.GetMetaData("Height") as string;

    So after uploading I am still not able to change the width and height of the image by editing the image. This because of Sitefinity doesn't recognize the item as an image.
    And this is because of Sitefinity doesn't expect an image in an selfmade librarytype.

    So the 4 issues still exist:
    - Zip with more then 25 items does only upload 25 items correctly.
    - Clicking something else then SAVE after uploading an image doesn't update the size of the uploaded image (0kb)
    - After uploading into an selfmade librabytype no possibility to update the width and height
    - Not able to scale images during upload

    Would love to hear some answers again

     

  27. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    18 Jun 2009
    Link to this post
    Hello Frank,

    Could I see the code for resizing the image width and height that you are using? Also for setting th width and height I suggest using integer.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  28. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    19 Jun 2009
    Link to this post
    I don't even try to scale so far. Getting the current image width/height doesn't work right now. So I cannot scale at all.

    //string Breedte = libraryItem.GetMetaData("Width") as string;
    //string Hoogte = libraryItem.GetMetaData("Height") as string;

    BREEDTE en HOOGTE keep empty. So I cannot scale to the correct width.
  29. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    19 Jun 2009
    Link to this post
    Hi Frank,

    The code you are using cannot resize the image during upload. I see only two properties that you are getting with no logic. You should use long integer 64 bit signed. The properties should also be set using SetMetaData.

    sample code:

    void Application_Start(object sender, EventArgs e)    
    {   
        Telerik.Libraries.LibraryManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(LibraryManager_Executing);   
    }   
      
    void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs args)   
    {   
        if (args.CommandName != "DeleteContent")   
        {   
            if (args.CommandArguments is Telerik.Cms.Engine.IContent)   
            {   
                Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)args.CommandArguments);   
      
                if (Telerik.Cms.Engine.ImagesHelper.IsBrowserImage(currentItem))   
                {   
                    long width = 0;   
                    long height = 0;   
      
                    object o2 = currentItem.GetMetaData("Width");   
                    if (o2 != null)   
                        width = Convert.ToInt64(o2);   
      
                    o2 = currentItem.GetMetaData("Height");   
                    if (o2 != null)   
                        height = Convert.ToInt64(o2);   
      
                    if (width > 900 || height > 500)   
                    {   
                        System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(currentItem.Content as byte[]));   
                        img = Telerik.Cms.Engine.ImagesHelper.GenerateThumbnail(900, 500, img, falsetrue);   
      
                        System.IO.MemoryStream stream = new System.IO.MemoryStream();   
                        Telerik.Cms.Engine.ImagesHelper.SaveImageToStream(img, stream, currentItem.MimeType);   
                        currentItem.Content = stream.GetBuffer();   
                        currentItem.SetMetaData("Size", ((byte[])currentItem.Content).Length);   
                        currentItem.SetMetaData("Width", img.Width);   
                        currentItem.SetMetaData("Height", img.Height);   
                    }   
                }   
            }   
        }   
    }   


    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  30. Frank
    Frank avatar
    19 posts
    Registered:
    03 Apr 2009
    19 Jun 2009
    Link to this post
    I know, but I don't get those properties filled.. so nothing happens... So when getting values isn't working, why should SETTING values do... :)

    I see you use LIBRABYMANAGER.EXECUTING and I am using CONTENTMANAGER.EXECUTING (as you can see in previous posts in this thread)
Register for webinar
53 posts, 0 answered
1 2