[Cubicweb] Preview with ViewerJS in CW

Christophe de Vienne christophe at unlish.com
Thu Mar 31 22:08:23 CEST 2016


Hi Stéphane,



Le 31/03/2016 19:23, Stéphane Bugat a écrit :
> Hi Christophe
> 
>> You can use the download_url()[1] function of the IDownloadable
>> interface to get one, but you will have to remove the base_url() from it
>> because download_url() returns an absolute URL.
> 
>> For example:
> 
>>  f = find('File', ...).one()
>>  url = f.cw_adapt_to('IDownloadable').download_url()
>>  path = url.rstrip(_cw.base_url())
> 
>> Then you just need to prepend the ViewerJS url and the needed '..' to
>> "go back to the top folder" (quoting ViewerJS doc) :
> 
>>  href = _cw.data_url('ViewerJS') + '#..' + path
> 
> Okay, thanks a lot...
> The built URL using your method looks like the following:
> http://myserver:8080/data/0a6bfcf4ea4e36b05a5bacb5e843de69/ViewerJS/#../file/1487/raw/foo.pdf
> 
> 
> Tried it but the preview failed with an error: "une erreur est survenue.
> Cette action est interdite. (...)"
> (sorry for the french...).
> 
> Any idea?

I guess it is because the Viewer is actually in the index.html file,
which cubicweb will not return when accessing ViewerJS/.

Try adding 'index.html' explicitely.
You may also need to change the '#..' into '#../../..' since the viewer
is in a subpath.

The resulting code would be:

    href = _cw.data_url('ViewerJS/index.html') + '#../../..' + path



-- 
Christophe de Vienne
Co-fondateur http://unli.sh



More information about the Cubicweb mailing list