masugadesign / link-vault-craft-cms

The Link Vault download link protection plugin for Craft CMS v3.
Other
5 stars 2 forks source link

Limit the number of downloads for a file #8

Closed rob-c-baker closed 4 years ago

rob-c-baker commented 5 years ago

I can see it's possible to get the number of times a file has been downloaded but how do I ensure a file can only be downloaded a certain number of times?

I am setting up a Craft Commerce store to deliver PDFs once purchased, I have got to the point where the items can be purchased using Pixel & Tonic's "Digital Products" plugin for Commerce 2. I then generate a link with your plugin which limits the download to the logged in user but it seems they can use that link as many times as they want.

I am using a Craft 3 volume attached to an Amazon S3 bucket to store the files.

To Reproduce Generate a link from a Asset field attached to a Craft 3 volume using Amazon S3 with Pixel & tonic's Amazon S3 integration.

Expected behavior Have a way to ensure the link can be used a set number of times.

benjaminkohl commented 5 years ago

Once you fetch the download count, you can just wrap the download link in a conditional that compares the number of downloads to whatever limit you want to set.

{% if totalDownloads < downloadLimit %}
   ...show the link...
{% else %}
   Download limit has been reached.
{% endif %}
benjaminkohl commented 5 years ago

The second part of this would be to block leech attempts in the Link Vault config so a link can't be saved by the user or passed around to other people.

rob-c-baker commented 5 years ago

Hi @benjaminkohl thanks for this.

I have something along these lines in place now. in your example above though, totalDownloads - as supplied by the link vault plugin - ends up being the total number of times a file has been downloaded for any user / order. I really need that to become "per order placed".

Do you know of a way to make that happen?

ryanmasuga commented 5 years ago

Are those downloads taking place on an order page, or somewhere where you can get the order number? If so, you might be able to store the order number in a Link Vault custom field on the downloads, then adjust the total downloads query to account for matching the value in that custom field.

rob-c-baker commented 5 years ago

That's a good idea. I'll have a go at that. Thanks.