Answer the question
In order to leave comments, you need to log in
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
ActiveStorage::Blob.service.send(:path_for, attachment.blob.key)
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 questionAsk a Question
731 491 924 answers to any question