Class: YoutubeChain

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip/storage/youtube.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*any_ios) ⇒ YoutubeChain

Returns a new instance of YoutubeChain.



290
291
292
293
# File 'lib/paperclip/storage/youtube.rb', line 290

def initialize(*any_ios)
  @autoclose = true
  @chain = any_ios.flatten.map{|e| e.respond_to?(:read)  ? e : StringIO.new(e.to_s) }
end

Instance Attribute Details

#autocloseObject

Returns the value of attribute autoclose.



285
286
287
# File 'lib/paperclip/storage/youtube.rb', line 285

def autoclose
  @autoclose
end

Class Method Details

.esc(s) ⇒ Object



286
287
288
# File 'lib/paperclip/storage/youtube.rb', line 286

def self.esc(s)
  s.to_s.gsub(/[^ \w.-]+/n){'%'+($&.unpack('H2'*$&.size)*'%').upcase}.tr(' ', '+')
end

Instance Method Details

#expected_lengthObject



315
316
317
318
319
320
321
322
323
324
325
# File 'lib/paperclip/storage/youtube.rb', line 315

def expected_length
  @chain.inject(0) do | len, io |
    if io.respond_to?(:length)
      len + (io.length - io.pos)
    elsif io.is_a?(File)
      len + File.size(io.path) - io.pos
    else
      raise "Cannot predict length of #{io.inspect}"
    end
  end
end

#read(buffer_size = 1024) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/paperclip/storage/youtube.rb', line 295

def read(buffer_size = 1024)
  current_io = @chain.shift
  return false if !current_io
  
  buf = current_io.read(buffer_size)
  if !buf && @chain.empty? # End of streams
    release_handle(current_io) if @autoclose
    false
  elsif !buf # This IO is depleted, but next one is available
    release_handle(current_io) if @autoclose
    read(buffer_size)
  elsif buf.length < buffer_size # This IO is depleted, but we were asked for more
    release_handle(current_io) if @autoclose
    buf + (read(buffer_size - buf.length) || '') # and recurse
  else # just return the buffer
    @chain.unshift(current_io) # put the current back
    buf
  end
end