mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-02 13:12:13 +08:00
SCI-3976 different approach to file download
This commit is contained in:
parent
39a2669eef
commit
5f16926280
1 changed files with 10 additions and 14 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue