Class: CarrierWave::Storage::PostgresqlTable::File

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

Constant Summary collapse

READ_CHUNK_SIZE =
16_384
STREAM_CHUNK_SIZE =
16_384

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File



58
59
60
61
62
# File 'lib/carrierwave/storage/postgresql_table.rb', line 58

def initialize(path)
  @path = path
  @record = CarrierWaveFile.find_or_initialize_by(:path => path)
  @read_pos = 0
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



56
57
58
# File 'lib/carrierwave/storage/postgresql_table.rb', line 56

def path
  @path
end

Instance Method Details

#content_typeObject



119
120
121
# File 'lib/carrierwave/storage/postgresql_table.rb', line 119

def content_type
  @record.content_type
end

#content_type=(new_content_type) ⇒ Object



123
124
125
# File 'lib/carrierwave/storage/postgresql_table.rb', line 123

def content_type=(new_content_type)
  @record.update_attribute(:content_type, new_content_type)
end

#deleteObject



192
193
194
# File 'lib/carrierwave/storage/postgresql_table.rb', line 192

def delete
  CarrierWaveFile.delete_all_files("id = #{CarrierWaveFile.connection.quote(@record.id)}")
end

#eof?Boolean



111
112
113
# File 'lib/carrierwave/storage/postgresql_table.rb', line 111

def eof?
  @read_pos == self.size
end

#exists?Boolean



178
179
180
# File 'lib/carrierwave/storage/postgresql_table.rb', line 178

def exists?
  @record && @record.persisted?
end

#filenameObject



99
100
101
# File 'lib/carrierwave/storage/postgresql_table.rb', line 99

def filename
  ::File.basename(@path)
end

#last_modifiedObject



103
104
105
# File 'lib/carrierwave/storage/postgresql_table.rb', line 103

def last_modified
  @record.updated_at
end

#move_to(new_path) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/carrierwave/storage/postgresql_table.rb', line 182

def move_to(new_path)
  CarrierWaveFile.transaction do
    # Remove any existing files at the current path.
    CarrierWaveFile.delete_all_files("path = #{CarrierWaveFile.connection.quote(new_path)} AND id != #{CarrierWaveFile.connection.quote(@record.id)}")

    # Change the current record's path to the new path.
    @record.update_attribute(:path, new_path)
  end
end

#read(length = nil, buffer = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/carrierwave/storage/postgresql_table.rb', line 64

def read(length = nil, buffer = nil)
  data = nil
  CarrierWaveFile.transaction do
    raw_connection = CarrierWaveFile.connection.raw_connection

    begin
      lo = raw_connection.lo_open(@record.pg_largeobject_oid)
      if length
        raw_connection.lo_lseek(lo, @read_pos, PG::SEEK_SET)
        data = raw_connection.lo_read(lo, length)
        @read_pos = raw_connection.lo_tell(lo)
      else
        data = raw_connection.lo_read(lo, self.size)
      end
    ensure
      raw_connection.lo_close(lo) if lo
    end
  end

  if (buffer && data)
    buffer.replace(data)
  end

  data
end

#rewindObject



115
116
117
# File 'lib/carrierwave/storage/postgresql_table.rb', line 115

def rewind
  @read_pos = 0
end

#sizeObject



107
108
109
# File 'lib/carrierwave/storage/postgresql_table.rb', line 107

def size
  @size = @record.size || fetch_size
end

#store(new_file) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/carrierwave/storage/postgresql_table.rb', line 131

def store(new_file)
  CarrierWaveFile.transaction do
    connection = CarrierWaveFile.connection
    raw_connection = connection.raw_connection
    oid = nil
    if new_file.kind_of?(CarrierWave::Storage::PostgresqlTable::File)
      file = new_file
    else
      file = new_file.to_file
    end

    begin
      oid = @record.pg_largeobject_oid || raw_connection.lo_creat
      handle = raw_connection.lo_open(oid, PG::INV_WRITE)
      raw_connection.lo_truncate(handle, 0)
      buffer = String.new # rubocop:disable Style/EmptyLiteral
      until file.eof?
        file.read(READ_CHUNK_SIZE, buffer)
        raw_connection.lo_write(handle, buffer)
      end
      file.rewind
    ensure
      raw_connection.lo_close(handle)
    end

    begin
      old_oid = @record.pg_largeobject_oid
      @record.pg_largeobject_oid = oid
      @record.size = new_file.size
      @record.content_type = new_file.content_type
      @record.save

      # Cleanup old, unused largeobject OIDs if we're updating the
      # record with a new OID reference.
      if (old_oid && old_oid != oid)
        old_references = connection.select_value("SELECT COUNT(*) FROM #{CarrierWaveFile.table_name} WHERE pg_largeobject_oid = #{CarrierWaveFile.connection.quote(old_oid)}").to_i
        if (old_references == 0)
          raw_connection.lo_unlink(old_oid)
        end
      end
    rescue ::ActiveRecord::RecordNotUnique
      @record = CarrierWaveFile.find_or_initialize_by(:path => @path)
      retry
    end
  end
end

#to_tempfileObject



90
91
92
93
94
95
96
97
# File 'lib/carrierwave/storage/postgresql_table.rb', line 90

def to_tempfile
  Tempfile.new(:binmode => true).tap do |tempfile|
    IO.copy_stream(self, tempfile)
    self.rewind
    tempfile.rewind
    tempfile.fsync
  end
end

#url(_options = {}) ⇒ Object



127
128
129
# File 'lib/carrierwave/storage/postgresql_table.rb', line 127

def url(_options = {})
  ::File.join("/", @path)
end