Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default url from Active Storage

Can we change the default 'permanent' url create from active storage to redirect to S3. Is something like rails/active_storage/representations/. I don't like the framework name in the url.

Thanks

like image 989
Olivier Avatar asked May 02 '18 06:05

Olivier


People also ask

How do I serve files in active storage?

Active Storage supports two ways to serve files: redirecting and proxying. All Active Storage controllers are publicly accessible by default. The generated URLs are hard to guess, but permanent by design. If your files require a higher level of protection consider implementing Authenticated Controllers.

Why can’t active storage determine the content type of a file?

If you don’t provide a content type and Active Storage can’t determine the file’s content type automatically, it defaults to application/octet-stream. To remove an attachment from a model, call purge on the attachment.

How do I find the URL of my static website?

In the pane that appears beside the account overview page of your storage account, select Static Website. The URL of your site appears in the Primary endpoint field.

How do I declare active storage services in yml?

Declare Active Storage services in config/storage.yml. For each service your application uses, provide a name and the requisite configuration. The example below declares three services named local, test, and amazon:


1 Answers

UPDATE: Recently, there was an addition which makes the route prefix configurable in Rails 6: https://guides.rubyonrails.org/6_0_release_notes.html#active-storage-notable-changes

It's just a matter of configuration:

Rails.application.configure do
  config.active_storage.routes_prefix = '/whereever'
end

Unfortunately, the url is defined in ActiveStorage routes.rb without easy means to change:

get "/rails/active_storage/blobs/:signed_id/*filename" => 
     "active_storage/blobs#show", as: :rails_service_blob
get "/rails/active_storage/representations/:signed_blob_id/:variation_key/*filename" => 
     "active_storage/representations#show", as: :rails_blob_representation

One solution starting point I can think of is defining your own Routes in addition and overriding the "rails_blob_representation_path" or similar

get "/my_uploads/:signed_blob_id/:variation_key/*filename" => 
  "active_storage/representations#show", as: :my_upload

and then overriding the path in a helper file and include the helper into the Named Helpers:

How to override routes path helper in rails?

module CustomUrlHelper
  def rails_blob_representation(*args)
    my_upload(*args)
  end
end

# initializer etc.
Rails.application.routes.named_routes.url_helpers_module.send(:include, CustomUrlHelper)

The solution might need some adjustments though, I didn't tested it.

like image 88
stwienert Avatar answered Oct 14 '22 21:10

stwienert