Class: Dragonfly::ActiveRecord::File

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/dragonfly-activerecord/file.rb

Constant Summary collapse

MAX_CHUNK_SIZE =

BLOB is typically 65k maximum, but in our case there’s a 4/3 overhead for Base64 encoding, and up to 1% overhead for worst case GZip compression.

32_768
MAX_CHUNK_READ =

max number of chunks read at a time

10

Instance Method Summary collapse

Instance Method Details

#dataObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dragonfly-activerecord/file.rb', line 30

def data
  @_output ||= Tempfile.new('dar', encoding: 'binary').tap do |fd|
    index = 0
    while true
      range = index...(index + MAX_CHUNK_READ)
      chunklist = chunks.where(idx:range).to_a.sort_by(&:idx)
      break if chunklist.empty?
      chunklist.each { |chunk| fd.write(chunk.data) }
      index += MAX_CHUNK_READ
    end
    fd.rewind
  end
end

#data=(file) ⇒ Object



26
27
28
# File 'lib/dragonfly-activerecord/file.rb', line 26

def data=(file)
  @_data = file
end