Jump to content

FieldtypeSecureFile


Wanze

Recommended Posts

@Peejay

Of course, it doesn't matter where you place the folder as long as the user running apache has write permission. So for testing purposes, you could also place the folder inside the document root.

Cheers

  • Like 1
Link to comment
Share on other sites

On 4/13/2017 at 10:03 AM, Wanze said:

@Peejay

Of course, it doesn't matter where you place the folder as long as the user running apache has write permission. So for testing purposes, you could also place the folder inside the document root.

Cheers

Your solution worked! I wil see what happens when I place the website online.

 

I found a little issue:

The download link doesn't work when the secure fieldtype is used in the user template file. 

You get this link: {your site}admin/access/users/?id=41&ftsd=document_1.pdf (-> then you go to the users list)

But when je place "edit" after users/, the download link works!: {your site}admin/access/users/edit/?id=41&ftsd=document_1.pdf

Cheers! 

Link to comment
Share on other sites

  • 6 months later...

I needed a way for the files to not force download, so that admin users can quickly view a document in their browser;

i added this to the hookDownloadFile() method:

$options = [];
if($this->wire('input')->get('view')) {
   $options['forceDownload'] = false;
}

and then added options to the download:

$file->download($options); // Access check performed by this method

up in the hookRenderItem i added this:

$segments['view'] = 1;
$link = $this->wire('page')->url . '?' . http_build_query($segments);
$markup .= " | <a href='{$link}' target='_blank'><i class='fa fa-eye'></i> " . $this->_('View File') . "</a>";
$markup .= "</div>";

so now the file looks like this:

view_file.jpg.844bb9bb968564498c1847d5eb41ebc2.jpg

It would be cool to consider adding this functionality, since it can't always be assumed that the files should force download; maybe it needs to be a config option where you choose the behavior or opt in for download and/or view links...

  • Like 2
  • Thanks 2
Link to comment
Share on other sites

Also, for the field to work in lister/lister pro, some additional changes needed to be made to how the links to the download are formed; this is the complete hookRenderItem method; so basically instead of referencing the page being edited, it would need to reference the $pagefile->page; then since the editUrl already has the id, you don't need to have that in the $segments array.. this works now in listers if you show a secure files field, when it renders in the list you can click on the item to download/view the file...

    public function hookRenderItem(HookEvent $event)
    {
        /** @var PagefileSecure $pagefile */
        $pagefile = $event->arguments('pagefile');
        if (!$pagefile instanceof PagefileSecure) {
            return;
        }
        $markup = $event->return;
        $markup = preg_replace("/<a class='InputfieldFileName'[^>]*>(.*)<\/a>/", "$1", $markup);
        if ($pagefile->field->get('allowDownloadInAdmin') && $pagefile->isDownloadable()) {
            $segments = array(
                //'id' => $this->wire('input')->get('id'),
                self::GET_VAR_DOWNLOAD => urlencode($pagefile->basename),
            );
            //$link = $this->wire('page')->url . '?' . http_build_query($segments);
            $link = $pagefile->page->editUrl . '&' . http_build_query($segments);
            $markup .= "<div class='FieldtypeSecureFileDownload InputfieldFileData'><a href='{$link}'><i class='fa fa-download'></i> " . $this->_('Download File') . "</a>";

            $segments['view'] = 1;
            //$link = $this->wire('page')->url . '?' . http_build_query($segments);
            $link = $pagefile->page->editUrl . '&' . http_build_query($segments);
            $markup .= " | <a href='{$link}' target='_blank'><i class='fa fa-eye'></i> " . $this->_('View File') . "</a>";
            $markup .= "</div>";
        }
        $event->return = $markup;
    }

after additional testing, i can consider forking and pull request, but wanted to run this by here on the forum first..  also haven't setup a module config to account for the showing pref (view and or download).. will possibly get to that soon..

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

A little information for the observers here: The feature to view a secret file beside the forced download is now available in version 1.0.3. Thanks @Macrura!

I introduced an additional setting on field level to toggle the "View" possibility.

Cheers

  • Like 2
Link to comment
Share on other sites

  • 4 months later...

Try to add PW namespace at the very top of the .module file:

<?php namespace ProcessWire;

Lots of older modules can be made PW3-compatible that way... worth a try anyway.

  • Like 1
Link to comment
Share on other sites

6 hours ago, pwfans said:

Make it compatible to pw 3.x please ..

@pwfans

i have it running well on a large PW3 site. Did you run into some problem? I didn't need to add any namespace or do anything in particular, it just works.

  • Like 2
Link to comment
Share on other sites

  • 3 months later...

Hello, I'm trying to make this module to work but It seems that my Processwire knowledge is not enough to make it work.

I have a user profile with some pdf files that I need to be secured for every user. Only the owner can dowload his/her own files.

So, I added the custom field 'profile-contract' to the system user template, so far so good.

The secure file module is saving the files outside of the document root.

Now I have created a new template in the front end (profile.php) so users can download their files and access other information about their profile.

I have been using this:

$user->profile_name, $user->birth_date etc to access custom field data from the user template and printed to the profile.php template

The question is how can I make a link to download a secured file stored in the user system template?, in this case a custom field named $user->profile_contract in to the profile.php template

Thank you.

 

 

 

Edited by Krlos
Typo
Link to comment
Share on other sites

On 8/14/2018 at 7:53 PM, Krlos said:

The question is how can I make a link to download a secured file stored in the user system template?, in this case a custom field named $user->profile_contract in to the profile.php template

@Krlos You can echo a link to the external file like that (I might not understood all the issue...) :

echo $user->profile_contract->first()->filename;

 

To offer a download to your client, check this tutorial and adapt it for your needs (credit: @jmartsch) : https://jensmartsch.de/blog/simple-file-downloads-with-processwire/

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Hello Again!

I was able to make this module work... but now my client wants to open the PDF file on a lightbox or something so users don't need to download and open the files everytime.

I have tried many things but browers allways open download prompt, there is a way I can alter the behavior?

Much appreciated

 

Link to comment
Share on other sites

2 hours ago, Krlos said:

I have tried many things but browers allways open download prompt, there is a way I can alter the behavior?

there is, at least in the dev branch (not sure if it was committed to main)  – there should be a download file, and a view file option.

and if you are serving the file, make sure to supply the $options, with ['forceDownload'] => false

Link to comment
Share on other sites

1 hour ago, Macrura said:

$options, with ['forceDownload'] => false

That is exactly what cannot override the customers private browser setting for file downloads of type PDF. If you serve a file of type PDF you can tell the browser "your wishes", but every individual browser may have its own preferences that cannot be overridden.

I had understand that it should be not downloaded but directly shown as document, what only can be asured if you also provide the app for displaying the doc, as you don't know if a browser has a setting that enables such a behave.

Link to comment
Share on other sites

2 minutes ago, horst said:

That is exactly what cannot override the customers private browser setting for file downloads of type PDF. If you serve a file of type PDF you can tell the browser "your wishes", but every individual browser may have its own preferences that cannot be overridden.

Exactly, I know about this.

Could be posible to capture the download file and open it in pdf.js for example?

Right now I'm using secure file like this:

I pass the file ID to a download template where it queries and serve the file.

I was experimenting with Fancybox, usign this code:

<a data-fancybox data-type="iframe" data-src="download/?=fid" href="javascript:;">
    Sample PDF file 
</a>

If I use a hardcoded or a relative pdf url it woks, but as SecureFile has no URL (As far as I know) I'm getting an error not found.

Link to comment
Share on other sites

The download url is the $page editor, with some parameters; the person accessing the file needs the correct role/perms

the file has to be delivered to the browser, since it is in a non-web accessible location.

Link to comment
Share on other sites

  • 2 months later...
  • 11 months later...
  • 3 months later...

I was not able to make this work on due to the following issue: 

 ProcessWire: ProcessUser: Secure File: Path '/' does not exist or is not writeable

I have tried multiple directories and approaches to change permissions, but PW could simply not find the path I specified with pwd. I am running the local server on Processwire 3.0.148.

Any suggestions?

Link to comment
Share on other sites

31 minutes ago, Testic said:

 ProcessWire: ProcessUser: Secure File: Path '/' does not exist or is not writeable

U need absolute/relative filepath but not url. Ex: /var/www/secure/ or c:\www\secure or something like ./../secure if U use windows local server and linux production server

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...