Forum

Thread tagged as: Question, Suggestions

Runtime functions for Assets?

I'm working on a project where it would be very handy to be able to access an Asset by filepath. Specifically for now: perch_assets_get_asset_by_filepath() or similar.

The scenario is that the client wants to have the ability to make some downloadable files spawn an intermediary contact information form before the file will download. My thought to make it easy for them is:

  • set a predetermined tag on the asset in Perch
  • use htaccess to route all PDF requests through a php script
  • use php to determine if the predetermined tag is set or not (this is where having access to Assets is essential)
  • not set: download
  • set: show the form

I guess this is an edge case. Maybe there is another way to accomplish the same thing?

Kirk Roberts

Kirk Roberts 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Could you use buckets? Put the ones that need the form in a different bucket, and then secure the bucket a la https://docs.grabaperch.com/docs/resources/buckets/

Ah... I hadn't considered that as an option (that the bucket can be switched post-upload). Doing a quick test it doesn't look like the asset file is actually moved if the bucket is switched (e.g. from the default /resources to the file_path specified in config/buckets.php). Is it supposed to be moved?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't switch the bucket, you'd need to set the secure bucket up in your bucket list and then upload the assets to it.

That's what I assumed, but there is a "Save in bucket" drop-down when viewing an asset's details.

Ahhh... perhaps that is meant only for when re-uploading an asset on that screen? That wasn't initially apparent. Especially because the selection "sticks" even if not re-uploading an asset file, and the asset is visible if filtering by the newly "assigned" bucket.

Back on track, if someone wanted to switch buckets they could re-upload the file, selecting the appropriate bucket?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's right. Have you tried?

Yes. When I do so, the new file is uploaded to the other bucket, but these points were unexpected: there are now two files, one in each bucket, with separate IDs and entries in Assets... i.e. the old file was not "replaced" by the new one.

I was expecting that changing the bucket and reuploading from the asset details screen would replace the current asset, maintaining the same ID.

Would it be possible to use the "Save in bucket" dropdown to actually move the file without re-uploading? Or would that cause chaos? As it stands, the "Save in bucket" dropdown is confusing because it appears to be a bucket-switcher but isn't.

Drew McLellan

Drew McLellan 2638 points
Perch Support

It would cause chaos, as anything that references that asset would then have a broken path.

Okay. Thanks for helping me work through this. The resulting workflow won't be quite what I imagined, but I still see this as a win for the new Assets feature.

It may have been lost in the shuffle of the thread, but I recommend checking out the "Save in bucket" dropdown and how it can change which bucket Assets thinks a file is in. That seems unintended. Perhaps a line of help text with something like "For use when uploading a new file"? And/or removing the dividing line between the drop down and the file upload.

Thanks, Drew!