Skip to content

Instantly share code, notes, and snippets.

@lorenadl
Last active January 10, 2023 01:04
Show Gist options
  • Save lorenadl/36ba35893bf33981ac7c5eaad75743d7 to your computer and use it in GitHub Desktop.
Save lorenadl/36ba35893bf33981ac7c5eaad75743d7 to your computer and use it in GitHub Desktop.
[Rails] Download an Active Storage attachment

Rails download an Active Storage attachment in local disk

For master branch of rails/rails (http://edgeguides.rubyonrails.org/active_storage_overview.html#downloading-files):

message.video.open do |file|
  system '/path/to/virus/scanner', file.path
  # ...
end

For Rails 5.2:

def process_zip      
   # Download the zip file in temp dir
   zip_path = "#{Dir.tmpdir}/#{zip.filename}"
   File.open(zip_path, 'wb') do |file|
       file.write(zip.download)
   end   

   Zip::File.open(zip_path) do |zip_file|  
       # process the zip file
       # ...
       puts "processing file #{zip_file}"
   end
end

Source: https://www.reddit.com/r/rails/comments/7hr7la/getting_file_image_path_with_activestorage/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment