class Dragonfly::ActiveRecord::File

Constants

MAX_CHUNK_READ

max number of chunks read at a time

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.

Public Instance Methods

data() click to toggle source
# 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) click to toggle source
# File lib/dragonfly-activerecord/file.rb, line 26
def data=(file)
  @_data = file
end

Private Instance Methods

build_chunks() click to toggle source
# File lib/dragonfly-activerecord/file.rb, line 46
def build_chunks
  # require 'pry' ; binding.pry
  chunks.delete_all
  index = 0
  while chunk = @_data.read(MAX_CHUNK_SIZE)
    chunks.create!(data:chunk, idx:index)
    index += 1
  end
end
set_defaults() click to toggle source
# File lib/dragonfly-activerecord/file.rb, line 56
def set_defaults
  self.metadata    ||= Hash.new
  self.accessed_at ||= Time.current
end