doublesecretagency / craft-digitaldownload

Digital Download plugin for Craft CMS
Other
4 stars 4 forks source link

Securing a Generated PDF? #5

Open danleecreates opened 4 years ago

danleecreates commented 4 years ago

Hi

Recently purchased Digital Download.

I'm wanting to secure a PDF download generated by another plugin called Enupal Snapshot. How would I do that with the Digital Download plugin and the following code which generates the PDF on the fly?

{% set downloadURL = craft.enupalsnapshot.displayTemplate("reports/pdf-report", settings) %}

<a href="{{ downloadURL }}">
        Download Report
</a>
lindseydiloreto commented 4 years ago

Hi @nearkingdom, sorry for the late reply.

The way Digital Download is currently architected, you can only create a link (or token) for an Asset. The generated PDF from Enupal Snapshot isn't an Asset, it's just a regular file.

I've considered allowing the plugin to just accept any file for download, but of course there are challenges associated with that. In the short term, unfortunately, it simply can't be used with dynamically generated files.

I'll consider this a feature request to add the ability to specify a non-asset file path.

lindseydiloreto commented 4 years ago

I stand corrected, Enupal Snapshot does generate an Asset!

https://enupal.com/craft-plugins/enupal-snapshot/docs/advanced/return-asset-model

If you have access to the Asset model, then you should be able to create a link or token normally.

Let me know if that does the trick for you!

danleecreates commented 4 years ago

I have tried to integrate this with the Enupal Snapshot plugin but am getting an error.

Impossible to invoke a method ("getPath") on a string variable ("Something went wrong when creating the PDF file, please check your logs").

Here is the code

{% set pdfName = 'now'|date('Y-m-d') ~ '-' ~ date().timestamp ~ '-evaluation-' ~ companyName ~ '-' ~ firstName ~ '-' ~ lastName ~ '-' ~ submissionId ~ random(50, 500) ~ '.pdf' %}
                            {% set assetFolderRef = evaluationSettings.evaluationAssetRef %}
                            {%
                                set settings = {
                                    filename: pdfName,
                                    inline: false,
                                    overrideFile: true,
                                    asModel: true,
                                    singleUploadLocationSource: assetFolderRef,
                                    cliOptions: {
                                        'header-html': 'evaluation-tools/evaluation/pdf-header',
                                        'footer-html': 'evaluation-tools/evaluation/pdf-footer',
                                        'orientation': 'Portrait',
                                        'page-size': 'A4',
                                        'title': 'Evaluation for ' ~ companyName,
                                    },
                                    variables: {
                                        passSubmissionId: submissionId,
                                    }
                                }
                            %}

                            {% set assetModel = craft.enupalsnapshot.displayTemplate("evaluation-tools/evaluation/pdf-report", settings) %}
                            {% set filePath = assetModel.getPath() %}
                            {% set token = craft.digitalDownload.createToken(filePath, options) %}
                            <a href="{{ craft.digitalDownload.url(token) }}" class="f6 link white bg-animate bg-teal hover-bg-white hover-teal br-pill bn w-auto ph4 pv3 tc b pointer mb3">
                                <i class="fas fa-file-download"></i> Download Report
                            </a>
lindseydiloreto commented 4 years ago

Don't pass in the file path, pass in the asset itself...

{% set token = craft.digitalDownload.createToken(assetModel, options) %}
Ω 2019-12-03 at 12 22 51 PM