Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

active storage / clean files url

Multiple question around the same issue, the way active storage returns file urls

For now with the default setup, the following (cloud or local), returns somehow the following :

_domain/_path/_superlong_hash/_original_filename._ext

Given paperclip or many other existing gems, the _path/_superlong_hash/_original_filename._ext part is in hand to be customised, could end up in a clean url for any files

Meaning by that :

  • is there a way to "proxy" the _path with something more custom ?
  • is there a way to avoid the _superlong_hash ?
  • is there a way to customize the filename on the fly (or on uploads) ?

To make it a one-liner, how one would customize the files urls ?

I've seen here and there people ending up creating custom controllers to serve file with decent urls, but let's admit this is a no go (IMHO)

like image 529
Ben Avatar asked Apr 11 '18 06:04

Ben


People also ask

How do I delete active storage attachments?

To remove an attachment from a model, call purge on the attachment. If your application is set up to use Active Job, removal can be done in the background instead by calling purge_later . Purging deletes the blob and the file from the storage service.

Where are active storage images stored?

By default in the development environment, Active Storage stores all uploaded images on your local disk in the storage subdirectory of the Rails application directory. That's the file you uploaded!

What is Activestorage?

Active storage is an inbuilt gem in Rails that developers widely use to handle file uploads. Combined with the encrypted credentials feature in the latest releases of Rails, active storage is a safe and easy method to upload, serve, and analyze files onto cloud-based storage services as well as local storage.

How does active storage work in Rails?

Active Storage uses two tables in your application's database named active_storage_blobs and active_storage_attachments . After creating a new application (or upgrading your application to Rails 5.2), run rails active_storage:install to generate a migration that creates these tables.


1 Answers

I hope that ActiveStorage proves me wrong soon, but at the time of writing Rails 5.2, the straight answer seems to be that you have to go with your 'no go' option, hacking your own controllers together and heavily patching ActiveStorage to expose files.

For proxying see:

https://github.com/rails/rails/issues/31419

https://github.com/rails/rails/pull/30465

  • especially georgeclaghorn responses are interesting

For renaming file:

@user.avatar.blob.update(filename: 'NewFilename.jpg')

Manipulating the _superlong_hash / url

I have no good answer for this one. Although ActiveStorage makes it breathtakingly easy to upload (and somewhat easy to manipulate) files it takes Rails opinionated software philosophy to the edge, making it quite difficult to bypass it's obscurity by abstraction approach to url generation. ActiveStorage provides no built-in methods to do rudimentary things like permanent or direct links to files and variants once generated. File/image caching and nice urls seem therefore not to be possible to accomplish out of the box with ActiveStorage at this point in time.

like image 167
JSpang Avatar answered Nov 15 '22 12:11

JSpang