Class: CarrierWave::Storage::PostgresqlLo::File

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/postgresql_lo.rb

Instance Method Summary collapse

Constructor Details

#initialize(uploader) ⇒ File

Returns a new instance of File.



6
7
8
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 6

def initialize(uploader)
  @uploader = uploader
end

Instance Method Details

#connectionObject



51
52
53
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 51

def connection
  @connection ||= @uploader.model.class.connection.raw_connection
end

#content_typeObject



37
38
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 37

def content_type
end

#deleteObject



33
34
35
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 33

def delete
  connection.lo_unlink(identifier)
end

#file_lengthObject Also known as: size



40
41
42
43
44
45
46
47
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 40

def file_length
  @uploader.model.transaction do
    lo = connection.lo_open(identifier)
    size = connection.lo_lseek(lo, 0, 2)
    connection.lo_close(lo)
    size
  end
end

#identifierObject



55
56
57
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 55

def identifier
  @oid ||= @uploader.identifier.to_i
end

#original_filenameObject



59
60
61
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 59

def original_filename
  identifier.to_s
end

#readObject



14
15
16
17
18
19
20
21
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 14

def read
  @uploader.model.transaction do
    lo = connection.lo_open(identifier)
    content = connection.lo_read(lo, file_length)
    connection.lo_close(lo)
    content
  end
end

#urlObject



10
11
12
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 10

def url
  "/#{@uploader.model.class.name.underscore.gsub('/', '_')}_#{@uploader.mounted_as.to_s.underscore}/#{identifier}"
end

#write(file) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/carrierwave/storage/postgresql_lo.rb', line 23

def write(file)
  @uploader.model.transaction do
    lo = connection.lo_open(identifier, ::PG::INV_WRITE)
    connection.lo_truncate(lo, 0)
    size = connection.lo_write(lo, file.read)
    connection.lo_close(lo)
    size
  end
end