ruby-on-rails - ruby 压缩包 : Unable to find path of file stored in Active Storage

标签 ruby-on-rails ruby rails-activestorage rubyzip

我正在使用 Rails 5.2 和 ActiveStorage 让我的用户在我的应用程序中上传文件。我将所有上传的文件显示在一个表格中,我可以选择要下载的文件。选择后,我将能够将它们全部下载到一个 zip 文件中。为了压缩文件,我使用的是 Rubyzip,但无法正常工作。

我试过两种方法:
1 - 我试过 this way并遇到此错误
No such file or directory @ rb_sysopen -/rails/active_storage/blobs/..../2234.py
这是我的 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 - 其次,我尝试关注 rubyzip documentation并且错误有点不同。
没有这样的文件或目录@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

我希望它下载一个包含所有文件的 zip 文件。这是我的第一个问题,如果问题太长,请原谅。谢谢。

最佳答案

我刚刚遇到了类似的问题,但看到这个问题还没有得到解答。尽管它有点老,你可能已经解决了这个问题,但这是我的方法。这里的技巧是在两者之间创建一个临时文件

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

关于ruby-on-rails - ruby 压缩包 : Unable to find path of file stored in Active Storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54209861/

相关文章:

ruby-on-rails - 如何使 rails form_for 提交按钮成为 Bootstrap 按钮

html - Nokogiri 获取所有 HTML 节点

ruby-on-rails - 如果我安排一个工作人员每天在 Heroku 上运行,我如何确定它不会运行两次或被跳过?

ruby-on-rails - ActiveStorage::FileNotFoundError 但文件实际存在

ruby-on-rails - Rails 5.2 ActiveStorage 上传图片及用户如何裁剪图片

ruby-on-rails - 如何在宿主应用程序中呈现 Rails 引擎的 View

css - rails 错误编译 css Assets

ruby-on-rails - Heroku Ruby 版本不会升级?

rails-activestorage - ActiveStorage service_url && rails_blob_path 在不使用 S3 时无法生成完整的 url

mysql - 从 postgres 迁移到 mysql 导致奇怪的错误 Mysql2::Error: MySQL server has gone away