Ben
Ben

Reputation: 5172

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 :

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)

Upvotes: 8

Views: 1405

Answers (1)

JSpang
JSpang

Reputation: 186

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.

Upvotes: 4

Related Questions