Is there a way to catch a download action?

Hi there !
Tl;dr at the end

I’ve been playing a bit with nextcloud for a little longer than a week with the aim of creating a dataroom.

Now I know this exists and I found the apps to recreate but there is one key feature I’m missing, which is the watermark with the userId and timestamp on downloaded PDFs.

So I’ve already looked into the external script app (as recommended here) but there is not option to execute a script that would add my watermark before the user download my pdf.

I’ve already had a piece of script to add my stamps (thanks to this thread) but I really don’t know where to look as to what/where is the downloading happening in the code so I can hook the event

I tried looking into the external script app to see if there was a way to add the download catch but I’m a novice in terms of development so I got overwhelmed quite fast

Now, I know the more correct way to would be to develop my own plugin but I just need to get a demo, a “proof of concept” it’s possible

I hope this was clear and I’d be happy to help if more information is needed!

Thanks for your help :slight_smile:

Tl;dr : I need to watermark PDFs before they get downloaded but I don’t know how nor where to look in order to do that

Sorry i do not use it. But perhaps you can use tagged files and Workflow external scripts.
Read Add Watermark to PDF while download from nextcloud

Yes indeed, I’ve looked into the Workflow external script but they don’t allow to trigger a script when a user downloads a particular file and apply this script onto this file which is what I need :pensive:

The reason why that wouldn’t work is because the file that is being downloaded has a link or a file ID already, and the download action is linked to that. In terms of catching this during download, you’d need some logic to

  • detect when file download is being requested
  • generate the watermarked PDF or file (and wait for it to complete)
  • Hijack the request, and put in the new file

I don’t think this sounds very feasible.

Hence, what @devnull suggested above, you should make a trigger for uploads instead. When a file is uploaded to certain folder or directory (and optionally if they have a certain tag on them), you should trigger a job to add a watermark to that file. Then all files are already ready with the watermark for when someone wants to download it.

Sadly this does not give an option to write the timestamp of the download, though.

1 Like

Thanks for your hindsight !
Unfortunately watermarking the files during the upload is not a solution neither because several people would be able to access those documents, meaning I really need a custom watermark every time someone looks and/or download the file.
I think I’ll just leave this here for the moment then :slight_smile:
Again, thanks for your time !

1 Like

Actually, this would be quite EASY to implement, although I don’t know anything about virtual data rooms or how it would tie in with that, BUT…

You can create your own plugin (app) for nextcloud, with a route as /apps/pdfwatermarker/{fileid}, and this route will link up to a controller that YOU get to write, which will look up the original PDF file with that ID and watermark it specially for the logged in user trying to download it, and send the watermarked file to the requesting client.

Of course if you stopped here, you would have to provide the file download URI for the watermarked PDF by yourself rather than “sharing” it with users.

So you’ll want to look into registering fileactions;

This can allow you to register a handler for certain events. You can also clear existing actions.

2 Likes