carrierwaveとrubyzipのgemを利用しています。
zipファイルを作成後、そのzipファイルの保存するところができません。
zipファイルをファイルとして認識していないことが原因かと思っています。
作成したzipファイルをcarrierwaveにファイルとして認識されるにはどうしたらよいでしょうか。
ruby
1class SendMaskInsuranceFile < ApplicationRecord 2 mount_uploader :csv_url, DocumentFileUploader 3 mount_uploader :zip_url, DocumentFileUploader 4 5def self.generate_pdf_file_with_save 6 ActiveRecord::Base.transaction do 7 SendMaskInsuranceFile.where(created_at: Time.current.yesterday..Time.current).each do |send_mask_insurance_file| 8 open(send_mask_insurance_file.csv_url.path) do |f| 9 csv = CSV.new(f, headers: :first_row) 10 csv.each do |row| 11 staff = Staff.find_by(id: row[3]) 12 if staff 13 mask_insurance_tempfile = Tempfile.new(["#{Time.current.to_i}", ".pdf"], Rails.root.join('tmp')) 14 zip = send_mask_insurance_file.generate_zip_file(mask_insurance_tempfile) 15 send_mask_insurance_file.update!(zip_url: zip.zipfile) 16 **↑エラーは起きませんがzipファイルの保存ができていません。** 17 mask_insurance_tempfile.close 18 end 19 end 20 end 21 end 22 end 23 end 24 25 def generate_zip_file(mask_insurance_tempfile) 26 zip_tempfile = Tempfile.new(["#{Time.current.to_i}", ".zip"], Rails.root.join('tmp')) 27 Zip::File.open(zip_tempfile.path, Zip::File::CREATE) do |zipfile| 28 zipfile.add(File.basename(mask_insurance_tempfile.path), mask_insurance_tempfile.path) 29 end 30 end 31end
追記
ruby
1 2class DocumentFileUploader < CarrierWave::Uploader::Base 3 include CarrierWave::MiniMagick 4 5 process :fix_exif_rotation, if: :image? 6 process :strip, if: :image? 7 8 version :thumb, if: :image? do 9 process resize_and_pad: [300, 300] 10 end 11 12 def store_dir 13 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 14 end 15 16 def size_range 17 1..2.megabytes 18 end 19 20 def extension_allowlist 21 %w(jpg jpeg gif png pdf docx xlsx xlsm pptx doc xls ppt txt zip csv msg) 22 end 23 24 private 25 26 def image?(file) 27 file.content_type.include? 'image' 28 end 29end 30```追記
回答1件
あなたの回答
tips
プレビュー