Saturday, October 6, 2012

CKFinder for Domino. Downloading

CKFinder have two options to download files (images) - by request to the server "connector" command processor and by direct downloading.

Request to the "connector" ("ckfinder-connector-url\connector.xsp?command=Download...") is not accessible for Domino.

For direct download CKFinder use file(image) link with the "?download" command. Let's modify this way for allowing CKFinder to "download" files from the Domino server.

Browser would download file in the case if "content-disposition" header available (and filled). We need to have ability to open file (no "content-disposition" header) and to download file ("content-disposition" header present). Let assume we have some database with web view "web-files" for open files on the web. Then we will need another one, completely the same, view "web-download" for downloading files and HTTP response headers rule for  adding "content-disposition" header.

HTTP response headers rule:
Incoming URL pattern: */web-download/*
Custom headers:
            Name: content-disposition 
            Value: attachment

Now, when you will open file "web-files/companylogo.jpg", then browser will show it, and when you open file "web-files/companylogo.jpg", then browser will ask to save it.

Domino configured. Now we need to modify ckfinder.js to allow it to load files from another url. I am using "url" attribute of "ResourceType" tag in the response of "Init" command to put files url. So I  add "download" attribute to the "ResourceType" tag to put files download url.

ckfinder.js with modification that supports "download" attribute: 

p.resourceTypes.push(new a.aL.ResourceType(p, {
 name: t.getNamedItem('name').value,
 url: t.getNamedItem('url').value,
 
 //Modification
 download: t.getNamedItem('download').value,
 
 hasChildren: t.getNamedItem('hasChildren').value,
 allowedExtensions: 
  t.getNamedItem('allowedExtensions').value,
 deniedExtensions: 
  t.getNamedItem('deniedExtensions').value,
 acl: t.getNamedItem('acl').value,
 hash: t.getNamedItem('hash').value,
 maxSize: t.getNamedItem('maxSize').value
}));

And

a.aL.ResourceType = function (q, r) {
 var s = this;
 s.app = q;
 s.name = r.name;
 s.url = r.url;
 
 //Modification
 s.download = r.download;
 
 s.hasChildren = r.hasChildren === 'true';
 s.defaultView = 'Thumbnails';
 s.allowedExtensions = r.allowedExtensions;
 s.deniedExtensions = r.deniedExtensions;
 s.oT = p(r.allowedExtensions);
 s.ms = p(r.deniedExtensions);
 s.nS = r.acl;
 s.hash = r.hash;
 s.maxSize = r.maxSize;
};

Now we need to change generation of download url in he CKFinder:

if (R.config.directDownload){
 //Modification
 var filename = encodeURIComponent(S.name);
 
 var basenanme = 
 filename.substring(0, filename.lastIndexOf('.'));
 
 T = S.folder.getResourceType().download + 
 basenanme+'/$file/'+filename;

 //original CKFinder String
 //T = S.folder.getUrl() + S.name + '?download';
}

That's all.

No comments: