class ActiveStorage::PostgreSQL::File

Attributes

checksum[RW]
digest[W]
io[RW]

Public Class Methods

open(key, &block) click to toggle source
# File lib/active_storage/postgresql/file.rb, line 31
def self.open(key, &block)
  find_by!(key: key).open(&block)
end

Public Instance Methods

digest() click to toggle source
# File lib/active_storage/postgresql/file.rb, line 8
def digest
  @digest ||= Digest::MD5.new
end
import(path) click to toggle source
# File lib/active_storage/postgresql/file.rb, line 59
def import(path)
  self.oid = lo_import(path)
  self.digest = Digest::MD5.file(path)
end
open(*args) { |self| ... } click to toggle source
# File lib/active_storage/postgresql/file.rb, line 35
def open(*args)
  transaction do
    begin
      @lo = lo_open(oid, *args)
      yield(self)
    ensure
      lo_close(@lo) if @lo
    end
  end
end
read(bytes=size) click to toggle source
# File lib/active_storage/postgresql/file.rb, line 51
def read(bytes=size)
  lo_read(@lo, bytes)
end
seek(position, whence=PG::SEEK_SET) click to toggle source
# File lib/active_storage/postgresql/file.rb, line 55
def seek(position, whence=PG::SEEK_SET)
  lo_seek(@lo, position, whence)
end
size() click to toggle source
# File lib/active_storage/postgresql/file.rb, line 68
def size
  current_position = tell
  seek(0, PG::SEEK_END)
  tell.tap do
    seek(current_position)
  end
end
tell() click to toggle source
# File lib/active_storage/postgresql/file.rb, line 64
def tell
  lo_tell(@lo)
end
verify_checksum() click to toggle source
# File lib/active_storage/postgresql/file.rb, line 27
def verify_checksum
  raise ActiveStorage::IntegrityError unless digest.base64digest == checksum
end
write(content) click to toggle source
# File lib/active_storage/postgresql/file.rb, line 46
def write(content)
  lo_write(@lo, content)
  digest.update(content)
end
write_or_import() click to toggle source
# File lib/active_storage/postgresql/file.rb, line 15
def write_or_import
  if io.respond_to?(:to_path)
    import(io.to_path)
  else
    open(::PG::INV_WRITE) do |file|
      while data = io.read(5.megabytes)
        write(data)
      end
    end
  end
end