Class: Zipline::ZipHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/zipline/zip_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(streamer, logger) ⇒ ZipHandler

takes an array of pairs [[uploader, filename], … ]



4
5
6
7
# File 'lib/zipline/zip_handler.rb', line 4

def initialize(streamer, logger)
  @streamer = streamer
  @logger = logger
end

Instance Method Details

#handle_file(file, name, options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zipline/zip_handler.rb', line 9

def handle_file(file, name, options)
  normalized_file = normalize(file)
  write_file(normalized_file, name, options)
rescue => e
  # Since most APM packages do not trace errors occurring within streaming
  # Rack bodies, it can be helpful to print the error to the Rails log at least
  error_message = "zipline: an exception (#{e.inspect}) was raised  when serving the ZIP body."
  error_message += " The error occurred when handling file #{name.inspect}"
  @logger.error(error_message) if @logger
  raise
end

#normalize(file) ⇒ Object

This extracts either a url or a local file from the provided file. Currently support carrierwave and paperclip local and remote storage. returns a hash of the form aUrl or anIoObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zipline/zip_handler.rb', line 24

def normalize(file)
  if defined?(CarrierWave::Uploader::Base) && file.is_a?(CarrierWave::Uploader::Base)
    file = file.file
  end

  if defined?(Paperclip) && file.is_a?(Paperclip::Attachment)
    if file.options[:storage] == :filesystem
      {file: File.open(file.path)}
    else
      {url: file.expiring_url}
    end
  elsif defined?(CarrierWave::Storage::Fog::File) && file.is_a?(CarrierWave::Storage::Fog::File)
    {url: file.url}
  elsif defined?(CarrierWave::SanitizedFile) && file.is_a?(CarrierWave::SanitizedFile)
    {file: File.open(file.path)}
  elsif is_io?(file)
    {file: file}
  elsif defined?(ActiveStorage::Blob) && file.is_a?(ActiveStorage::Blob)
    {blob: file}
  elsif is_active_storage_attachment?(file) || is_active_storage_one?(file)
    {blob: file.blob}
  elsif file.respond_to? :url
    {url: file.url}
  elsif file.respond_to? :path
    {file: File.open(file.path)}
  elsif file.respond_to? :file
    {file: File.open(file.file)}
  elsif is_url?(file)
    {url: file}
  else
    raise(ArgumentError, 'Bad File/Stream')
  end
end

#write_file(file, name, options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zipline/zip_handler.rb', line 58

def write_file(file, name, options)
  @streamer.write_file(name, **options.slice(:modification_time)) do |writer_for_file|
    if file[:url]
      the_remote_uri = URI(file[:url])

      Net::HTTP.get_response(the_remote_uri) do |response|
        response.read_body do |chunk|
          writer_for_file << chunk
        end
      end
    elsif file[:file]
      IO.copy_stream(file[:file], writer_for_file)
      file[:file].close
    elsif file[:blob]
      file[:blob].download { |chunk| writer_for_file << chunk }
    else
      raise(ArgumentError, 'Bad File/Stream')
    end
  end
end