Fahim Abdullah
Fahim Abdullah

Reputation: 36

RubyZip : Unable to find path of file stored in Active Storage

I am using Rails 5.2 and ActiveStorage to let my users upload files in my app. I have all the uploaded files displayed in a table and I can choose to select which ones I want to download. After selecting, I will be able to download all of them in a zip file. To zip the files, I am using Rubyzip and I can't get to work properly.

I have tried two ways :
1 - I tried this way and faced this error
No such file or directory @ rb_sysopen - /rails/active_storage/blobs/..../2234.py
This is my controller :

def batch_download
  if params["record"].present?
    ids = params["record"].to_unsafe_h.map(&:first)

    if ids.present?
      folder_path = "#{Rails.root}/public/downloads/"
      zipfile_name = "#{Rails.root}/public/archive.zip"

      FileUtils.remove_dir(folder_path) if Dir.exist?(folder_path)
      FileUtils.remove_entry(zipfile_name) if File.exist?(zipfile_name)
      Dir.mkdir("#{Rails.root}/public/downloads")

      Record.where(id: ids).each do |attachment|
        open(folder_path + "#{attachment.file.filename}", 'wb') do |file|
          file << open("#{rails_blob_path(attachment.file)}").read
        end
      end

      input_filenames = Dir.entries(folder_path).select {|f| !File.directory? f}

      Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
        input_filenames.each do |attachment|
          zipfile.add(attachment,File.join(folder_path,attachment))
        end
      end

      send_file(File.join("#{Rails.root}/public/", 'archive.zip'), :type => 'application/zip', :filename => "#{Time.now.to_date}.zip")
    end
  else
    redirect_back fallback_location: root_path
  end
end

2 - Secondly I tried to follow the rubyzip documentation and the error was a bit different.
No such file or directory @ rb_file_s_lstat - /rails/active_storage/blobs/..../2234.py

if ids.present?
      folder = []
      input_filenames = []
      Record.where(id: ids).each do |attachment| 
        input_filenames.push("#{attachment.file.filename}")
        pre_path = "/rails/active_storage/blobs/"
        path_find = "#{rails_blob_path(attachment.file)}"
        folder.push(pre_path + path_find.split('/')[4])
      end
      container = Hash[folder.zip(input_filenames)]

      zipfile_name = "/Users/fahimabdullah/Documents/archive.zip"

      Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
        # input_filenames.each do |filename|
        container.map do |path, filename|
        zipfile.add(filename, File.join(path, filename))
        end
        zipfile.get_output_stream("myFile") { |f| f.write "myFile contains just this" }
      end

I expect it to download a zip file containing all the files inside. And this is my first question so please excuse me if the question was too long. Thank you.

Upvotes: 2

Views: 1412

Answers (1)

Devchris
Devchris

Reputation: 412

I just had a similar problem but saw this hasn't been answered yet. Even though it's a little older and you probably already solved this, here is my approach. The trick here was to create a tempfile in between

def whatever
    zip_file = Tempfile.new('invoices.zip')

    Zip::File.open(zip_file.path, Zip::File::CREATE) do |zipfile|
      invoices.each do |invoice|
        next unless invoice.attachment.attached?

        overlay = Tempfile.new(['overlay', '.pdf'])
        overlay.binmode
        overlay.write(invoice.attachment.download)
        overlay.close
        overlay.path

        zipfile.add(invoice.filename, File.join(overlay.path))
      end
    end

    invoices_zip = File.read(zip_file.path)

    UserMailer.with(user: user).invoice_export(invoices_zip, 'invoices.zip').deliver_now
  ensure
    zip_file.close
    zip_file.unlink
  end

Upvotes: 7

Related Questions