Class: ActiveStorage::PostgreSQL::File

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_storage/postgresql/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum.



5
6
7
# File 'lib/active_storage/postgresql/file.rb', line 5

def checksum
  @checksum
end

#digestObject



8
9
10
# File 'lib/active_storage/postgresql/file.rb', line 8

def digest
  @digest ||= Digest::MD5.new
end

#ioObject

Returns the value of attribute io.



5
6
7
# File 'lib/active_storage/postgresql/file.rb', line 5

def io
  @io
end

Class Method Details

.open(key, &block) ⇒ Object



31
32
33
# File 'lib/active_storage/postgresql/file.rb', line 31

def self.open(key, &block)
  find_by!(key: key).open(&block)
end

Instance Method Details

#import(path) ⇒ Object



59
60
61
62
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



51
52
53
# File 'lib/active_storage/postgresql/file.rb', line 51

def read(bytes=size)
  lo_read(@lo, bytes)
end

#seek(position, whence = PG::SEEK_SET) ⇒ Object



55
56
57
# File 'lib/active_storage/postgresql/file.rb', line 55

def seek(position, whence=PG::SEEK_SET)
  lo_seek(@lo, position, whence)
end

#sizeObject



68
69
70
71
72
73
74
# 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

#tellObject



64
65
66
# File 'lib/active_storage/postgresql/file.rb', line 64

def tell
  lo_tell(@lo)
end


76
77
78
# File 'lib/active_storage/postgresql/file.rb', line 76

def unlink
  lo_unlink(oid)
end

#verify_checksumObject

Raises:

  • (ActiveStorage::IntegrityError)


27
28
29
# File 'lib/active_storage/postgresql/file.rb', line 27

def verify_checksum
  raise ActiveStorage::IntegrityError unless digest.base64digest == checksum
end

#write(content) ⇒ Object



46
47
48
49
# File 'lib/active_storage/postgresql/file.rb', line 46

def write(content)
  lo_write(@lo, content)
  digest.update(content)
end

#write_or_importObject



15
16
17
18
19
20
21
22
23
24
25
# 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