Get path to ActiveStorage file on disk

前端 未结 4 687
[愿得一人]
[愿得一人] 2020-12-09 02:16

I need to get the path to the file on disk which is using ActiveStorage. The file is stored locally.

When I was using paperclip, I used the path<

4条回答
  •  萌比男神i
    2020-12-09 02:35

    You can download the attachment to a local dir and then process it.

    Supposing you have in your model:

    has_one_attached :pdf_attachment
    

    You can define:

    def process_attachment      
       # Download the attached file in temp dir
       pdf_attachment_path = "#{Dir.tmpdir}/#{pdf_attachment.filename}"
       File.open(pdf_attachment_path, 'wb') do |file|
           file.write(pdf_attachment.download)
       end   
    
       # process the downloaded file
       # ...
    end
    

提交回复
热议问题