[XFRM] Include $attachment in logDownload

DragonByte Tech

Well-known member
Currently, the XFRM:ResourceVersion :: logDownload does not pass the $attachment parameter, making it very difficult to determine exactly which attachment is being downloaded.

I do understand that changing the signature could introduce backwards compatibility breaking, so perhaps an appropriate change would be to change the relevant block of /src/addons/XFRM/Pub/Controller/ResourceVersion.php to this:

PHP:
if ($version->download_url)
{
    return $this->redirectPermanently($version->download_url);
}
else
{
    $this->repository('XFRM:ResourceVersion')->logAttachmentDownload($version, $attachment);

    /** @var \XF\ControllerPlugin\Attachment $attachPlugin */
    $attachPlugin = $this->plugin('XF:Attachment');

    return $attachPlugin->displayAttachment($attachment);
}
It would be an empty method in the base version, but it would be immensely helpful.

Use case: Our Credits add-on wants to be able to use the file size as a multiplier for the event that triggers on resource download. For instance, you could set up an event that paid the resource author 1 credits for every download, and an additional 0.001 credits for every byte in their attachment.


Fillip
 
Upvote 0
It's a little bit icky, but it's not actually that difficult.

I'd just extend logDownload and use:
PHP:
$attachments = $version->Attachments;
if ($attachments->count() > 1)
{
    $attachmentId = $this->app()->inputFilterer()->filter('file', 'uint');
    $attachment = $attachments[$attachmentId];
}
else
{
    $attachment = $attachments->first();
}
We wouldn't usually recommend accessing what is essentially controller layer stuff in what is essentially the model layer but, needs must :)

You might have already gone down that route already, but if not, that's probably the easiest way to do it.
 
IWe wouldn't usually recommend accessing what is essentially controller layer stuff in what is essentially the model layer but, needs must :)

You might have already gone down that route already, but if not, that's probably the easiest way to do it.
Interesting, thanks for that example :)

What I did just now was override actionDownload and essentially copied the code up to and including the DownloadChooser view, but I don't like duplication like that.

I'll switch to that method instead since it'll be the most compatible with other 3rd party addons, thanks!


Fillip
 
Top Bottom