簡體   English   中英

RubyZip:無法找到存儲在 Active Storage 中的文件路徑

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

我使用 Rails 5.2 和 ActiveStorage 讓我的用戶在我的應用程序中上傳文件。 我在表格中顯示了所有上傳的文件,我可以選擇要下載的文件。 選擇后,我將能夠將它們全部下載到一個 zip 文件中。 要壓縮文件,我使用的是 Rubyzip,但無法正常工作。

我嘗試了兩種方法:
1 - 我嘗試過這種方式並遇到了這個錯誤
沒有這樣的文件或目錄@ rb_sysopen - /rails/active_storage/blobs/..../2234.py
這是我的控制器:

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 文檔,但錯誤有點不同。
沒有這樣的文件或目錄@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM