0
votes

Does anyone know how to change the disk_service.rb to specify upload paths, for example:

my_path = Rails.root.join("public", "websites", "example.com", "users", current_user.id, "avatar")
current_user.avatar.attach(file, my_path)

This would result in having the file uploaded here:

/public/websites/example.com/users/12345/avatar/blah.png

And then I'd be able to:

rails_representation_path( current_user.avatar.variant(resize: "100x100"), disposition: 'attachment')

and get back the path:

/websites/example.com/users/12345/avatar/blah-100x100.png

This will allow getting rid of many issues around the ActiveStorage public/private URLs and related to CDN caching.

I am playing with https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/service/disk_service.rb but can't figure out much of what it does and how it works really.

1
I spent a long time trying to make active storage work, but I kept bumping into limitations for frustratingly simple use-cases that aren't supported and cannot be easily implemented, so I ended up going with Shrine which is HEAVEN for anything files related. If you are not too deep into the project, go with Shrine.Tashows
It seems we're not the only ones running into trouble with ActiveStorage. Fortunately I haven't gone too far with it and will likely switch very soon.Nick M

1 Answers

0
votes

In your config/storage.yml you should have a configuration block for the local storage service, you can change where the attachments are stored by specifing a different path for root.

local:
  service: Disk
  root: <%= Rails.root.join('storage') %>

I don't think that you'll be able to address the specific images in the manner you are describing. ActiveRecord creates a unique key for everything that is attached to a model and renames the files with that key.