Class: OBF::Utils::Zipper

Inherits:
Object
  • Object
show all
Defined in:
lib/obf/utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(zipfile) ⇒ Zipper



252
253
254
# File 'lib/obf/utils.rb', line 252

def initialize(zipfile)
  @zipfile = zipfile
end

Instance Method Details

#add(path, contents) ⇒ Object



256
257
258
# File 'lib/obf/utils.rb', line 256

def add(path, contents)
  @zipfile.get_output_stream(path) {|os| os.write contents }
end

#glob(path) ⇒ Object



265
266
267
# File 'lib/obf/utils.rb', line 265

def glob(path)
  @zipfile.glob(path)
end

#read(path) ⇒ Object



260
261
262
263
# File 'lib/obf/utils.rb', line 260

def read(path)
  entry = @zipfile.glob(path).first
  entry ? entry.get_input_stream.read : nil
end

#read_as_data(path) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/obf/utils.rb', line 269

def read_as_data(path)
  attrs = {}
  raw = @zipfile.read(path)
  types = MIME::Types.type_for(path)
  attrs['content_type'] = types[0] && types[0].to_s

  str = "data:" + attrs['content_type']
  str += ";base64," + Base64.strict_encode64(raw)
  attrs['data'] = str

  if attrs['content_type'].match(/^image/)
    fn = OBF::Utils.temp_path('file')
    file = Tempfile.new('file')
    file.binmode
    file.write raw
    file.close
    data = `identify -verbose #{file.path}`
    data.split(/\n/).each do |line|
      pre, post = line.sub(/^\s+/, '').split(/:\s/, 2)
      if pre == 'Geometry'
        match = post.match(/(\d+)x(\d+)/)
        if match && match[1] && match[2]
          attrs['width'] = match[1].to_i
          attrs['height'] = match[2].to_i
        end
      end
    end
  end
  attrs
end