R
R
rusrich2019-03-03 19:03:35
Ruby on Rails
rusrich, 2019-03-03 19:03:35

How to get the direct path to a file uploaded via Active Storage?

I decided to do without additional gems, since the implementation using Carrierwave heavily loads the system when working with gigabyte files. Began to do through standard Active Storage.
But there was a problem.
To convert a video, I need the full path to the file.
I don't know how to get it.
In the video model, I write the following:
class Video < ApplicationRecord
has_one_attached :clip
include Rails.application.routes.url_helpers
def cover_url
rails_blob_path(self.clip, disposition: "attachment", only_path: true)
end
end
In the view, for example, I display the file url
<% = rails_blob_url(@video.clip) if @video.clip.attached? %>
I get this:
localhost:3000/rails/active_storage/blobs/eyJfcmFp...
Physically, the file is located here:
storage/fT/Xr/fTXr7uMF7geiFtrS2HcxNhJE
Moreover, fTXr7uMF7geiFtrS2HcxNhJE is the file itself without extension.
/fT/Xr/ - these directories are randomly generated.
That is, I can’t even write the logic to find these files and automate the generation of url for video conversion.
If you could set something like this template
storage/#{video.id}/#{video.clip.filename}.#{video.clip.extension}
It would be perfect.
Can you advise or guide?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kozlov, 2019-03-05
@rusrich

ActiveStorage::Blob.service.send(:path_for, attachment.blob.key)

B
bakeevDR, 2019-11-08
@bakeevDR

Moreover, fTXr7uMF7geiFtrS2HcxNhJE is the file itself without extension.
/fT/Xr/ - these directories are randomly generated.
Directories are not generated randomly, but are taken from the file name by the first 4 characters, the file name can be taken from the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question