Class: Zipline::ZipGenerator
- Inherits:
-
Object
- Object
- Zipline::ZipGenerator
- Defined in:
- lib/zipline/zip_generator.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #handle_file(streamer, file, name, options) ⇒ Object
-
#initialize(files, **kwargs_for_streamer) ⇒ ZipGenerator
constructor
takes an array of pairs [[uploader, filename], … ].
- #is_io?(io_ish) ⇒ Boolean
-
#normalize(file) ⇒ Object
This extracts either a url or a local file from the provided file.
- #write_file(streamer, file, name, options) ⇒ Object
Constructor Details
#initialize(files, **kwargs_for_streamer) ⇒ ZipGenerator
takes an array of pairs [[uploader, filename], … ]
6 7 8 9 10 11 12 13 |
# File 'lib/zipline/zip_generator.rb', line 6 def initialize(files, **kwargs_for_streamer) # Use RackBody as it has buffering built-in in zip_tricks 5.x+ @body = ZipTricks::RackBody.new(**kwargs_for_streamer) do |streamer| files.each do |file, name, = {}| handle_file(streamer, file, name.to_s, ) end end end |
Instance Method Details
#each(&block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/zipline/zip_generator.rb', line 15 def each(&block) return to_enum(:each) unless block_given? @body.each(&block) 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 = "zipline: an exception (#{e.inspect}) was raised when serving the ZIP body." += " The error occurred when handling #{@filename.inspect}" if @filename logger.error() raise end |
#handle_file(streamer, file, name, options) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/zipline/zip_generator.rb', line 27 def handle_file(streamer, file, name, ) file = normalize(file) # Store the filename so that a sensible error message can be displayed in the log # if writing this particular file fails @filename = name write_file(streamer, file, name, ) @filename = nil end |
#is_io?(io_ish) ⇒ Boolean
95 96 97 |
# File 'lib/zipline/zip_generator.rb', line 95 def is_io?(io_ish) io_ish.respond_to? :read 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/zipline/zip_generator.rb', line 40 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.[: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 (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(streamer, file, name, options) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/zipline/zip_generator.rb', line 74 def write_file(streamer, file, name, ) streamer.write_deflated_file(name, **.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 |