Class: Zipline::ZipGenerator

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

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ ZipGenerator

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



6
7
8
# File 'lib/zipline/zip_generator.rb', line 6

def initialize(files)
  @files = files
end

Instance Method Details

#each(&block) ⇒ Object



15
16
17
18
19
20
# File 'lib/zipline/zip_generator.rb', line 15

def each(&block)
  output = new_output(&block)
  OutputStream.open(output) do |zip|
    @files.each {|file, name| handle_file(zip, file, name) }
  end
end

#get_size(file) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/zipline/zip_generator.rb', line 67

def get_size(file)
  if is_io?(file) || file.respond_to?(:size)
    file.size
  elsif file.respond_to? :content_length
    file.content_length
  else
    throw 'cannot determine file size'
  end
end

#handle_file(zip, file, name) ⇒ Object



22
23
24
25
26
# File 'lib/zipline/zip_generator.rb', line 22

def handle_file(zip, file, name)
  file = normalize(file)
  name = uniquify_name(name)
  write_file(zip, file, name)
end

#is_io?(file) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/zipline/zip_generator.rb', line 77

def is_io?(file)
  file.is_a?(IO) || (defined?(StringIO) && file.is_a?(StringIO))
end

#new_output(&block) ⇒ Object



43
44
45
# File 'lib/zipline/zip_generator.rb', line 43

def new_output(&block)
  FakeStream.new(&block)
end

#normalize(file) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zipline/zip_generator.rb', line 28

def normalize(file)
  unless is_io?(file)
    if file.respond_to?(:url) && (!defined?(::Paperclip::Attachment) || !file.is_a?(::Paperclip::Attachment))
      file = file
    elsif file.respond_to? :file
      file = File.open(file.file)
    elsif file.respond_to? :path
      file = File.open(file.path)
    else
      raise(ArgumentError, 'Bad File/Stream')
    end
  end
  file
end

#to_sObject

this is supposed to be streamed!



11
12
13
# File 'lib/zipline/zip_generator.rb', line 11

def to_s
  throw "stop!"
end

#uniquify_name(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/zipline/zip_generator.rb', line 81

def uniquify_name(name)
  @used_names ||= Set.new

  if @used_names.include?(name)

    #remove suffix e.g. ".foo"
    parts = name.split '.'
    name, extension =
      if parts.length == 1
        #no suffix, e.g. README
        parts << ''
      else
        extension = parts.pop
        [parts.join('.'), ".#{extension}"]
      end

    #trailing _#{number}
    pattern = /_(\d+)$/

    unless name.match pattern
      name = "#{name}_1"
    end

    while @used_names.include? name + extension
      #increment trailing number
      name = name.sub( pattern ) { |x| "_#{$1.to_i + 1}" }
    end

    #reattach suffix
    name += extension
  end

  @used_names << name
  name
end

#write_file(zip, file, name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zipline/zip_generator.rb', line 47

def write_file(zip, file, name)
  size = get_size(file)
  zip.put_next_entry name, size

  if is_io?(file)
    while buffer = file.read(2048)
      zip << buffer
    end
  else
    the_remote_url = file.url(Time.now + 1.minutes)
    c = Curl::Easy.new(the_remote_url) do |curl|
      curl.on_body do |data|
        zip << data
        data.bytesize
      end
    end
    c.perform
  end
end