Search code examples
file-uploadckfinder

How to enable HTML-Upload with Code-Configuration in CKFInder?


I am using CKFinder 3 to upload files with an authentication of the user. Because of technical issues we do not have a config-file, the configuration is in the code. Now we have to be able to upload files with a special ending *.template which are XML-files that include html in a CDATA-Section.

This is denied by CKFinder by default, so I want to extend the SetAllowedHtmlExtensionMatchers of ResourceTypeBulder with two StringMatches "*.xml" and "*.template". Here is the code of the SetupConnector

        OwinConnectorFactory connectorFactory = new OwinConnectorFactory();
        ConnectorBuilder connectorBuilder = new ConnectorBuilder();
        string l_licenseName = "DUMMY";
        string l_licenseKey = "DUMMY";
        connectorBuilder.SetLicense(l_licenseName, l_licenseKey); 
        connectorBuilder
            .SetAuthenticator(new CKFinderAuthenticator())
            .SetRequestConfiguration(
                (request, config) =>
                {
                    config.SetOverwriteOnUpload(true);
                    config.SetThumbnailSizes(new SizeAndQuality(100, 100, new ImageQuality(80))); 
                    config.AddProxyBackend("default", new LocalStorage(@""));
                    config.AddResourceType("MySite", resourceBuilder => resourceBuilder.SetBackend("default", ""));
                    config.AddAclRule(new AclRule(
                               new StringMatcher("*"),
                               new StringMatcher("*"),
                               new StringMatcher("*"),
                               new Dictionary<Permission, PermissionType> { { Permission.All, PermissionType.Allow } }));
                    var defaultBackend = config.GetBackend("default");
                    var keyValueStoreProvider = new FileSystemKeyValueStoreProvider(defaultBackend);
                    config.SetKeyValueStoreProvider(keyValueStoreProvider);
                    config.SetKeyValueStoreProvider(new EntityFrameworkKeyValueStoreProvider("MyConnection")));
                });
                app.UseConnector(connectorBuilder.Build(connectorFactory));

I have to extend the resourceBuilder to use an additional value but I cannot figure out how. Something like

config.AddResourceType("MySite", resourceBuilder => {resourceBuilder.SetBackend("default", ""); resourceBuilder.SetAllowedHtmlExtensionMatchers ......;});

did compile but was obviously wrong as the files could not be uploaded. This may be a beginners question but I am stuck. I can find loads of examples to do exactly what is shown here but I couldn't get the correct keywords to find more than one "parameter" in the expression. Can somebody please point out what I am doing wrong?


Solution

  • I believe the code you are looking for is:

    var allowedHtmlExtensions = new[] {
        new StringMatcher("template"),
        new StringMatcher("xml")
    };
    
    config.AddResourceType("Files", resourceBuilder => {
        resourceBuilder.SetBackend("default", "files");
        resourceBuilder.SetAllowedExtensions(new string[] { "template", "xml", "7z","aiff","asf","avi","bmp","csv","doc","docx","fla","flv","gif","gz","gzip","jpeg","jpg","mid","mov","mp3","mp4","mpc","mpeg","mpg","ods","odt","pdf","png","ppt","pptx","pxd","qt","ram","rar","rm","rmi","rmvb","rtf","sdc","sitd","swf","sxc","sxw","tar","tgz","tif","tiff","txt","vsd","wav","wma","wmv","xls","xlsx","zip" });
    resourceBuilder.SetAllowedHtmlExtensionMatchers(allowedHtmlExtensions);
    resourceBuilder.SetLazyLoaded(true);                            
    });
    

    Please note that in order to upload files you also need to add templates and xml extensions to allowedExtensions setting.

    NOTE: If you do not specify AllowedExtensions it will be possible to upload any file which is rather not safe.