SCI-3976 different approach to file download

This commit is contained in:
Miha Mencin 2019-10-08 16:20:16 +02:00
parent 39a2669eef
commit 5f16926280

View file

@ -1,11 +1,13 @@
# frozen_string_literal: true
require 'fileutils'
require 'active_storage/downloading'
module ModelExporters
class ModelExporter
attr_accessor :assets_to_copy
attr_accessor :tiny_mce_assets_to_copy
include ActiveStorage::Downloading
def initialize
@assets_to_copy = []
@ -15,22 +17,16 @@ module ModelExporters
def copy_files(assets, attachment_name, dir_name)
assets.flatten.each do |a|
next unless a.public_send(attachment_name).attached?
blob = a.public_send(attachment_name).blob
dir = FileUtils.mkdir_p(File.join(dir_name, a.id.to_s)).first
destination_path = File.join(dir, a.file_name)
blob.open do |file|
system '/path/to/virus/scanner', file.path
FileUtils.cp(file.path, destination_path)
end
yield if block_given?
dir = FileUtils.mkdir_p(File.join(dir_name, a.id.to_s)).first
tempfile = Tempfile.new
tempfile.binmode
a.public_send(attachment_name).blob.download { |chunk| tempfile.write(chunk) }
tempfile.flush
tempfile.rewind
FileUtils.cp(
tempfile.path,
File.join(dir, a.file_name)
)
ensure
tempfile.close
tempfile.unlink
end
end