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

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

Constant Summary collapse

READ_CHUNK_SIZE =
16384
STREAM_CHUNK_SIZE =
16384

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



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

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.



54
55
56
# File 'lib/carrierwave/storage/postgresql_table.rb', line 54

def path
  @path
end

Instance Method Details

#content_typeObject



117
118
119
# File 'lib/carrierwave/storage/postgresql_table.rb', line 117

def content_type
  @record.content_type
end

#content_type=(new_content_type) ⇒ Object



121
122
123
# File 'lib/carrierwave/storage/postgresql_table.rb', line 121

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

#deleteObject



186
187
188
# File 'lib/carrierwave/storage/postgresql_table.rb', line 186

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

#eof?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/carrierwave/storage/postgresql_table.rb', line 109

def eof?
  @read_pos == self.size
end

#filenameObject



97
98
99
# File 'lib/carrierwave/storage/postgresql_table.rb', line 97

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

#last_modifiedObject



101
102
103
# File 'lib/carrierwave/storage/postgresql_table.rb', line 101

def last_modified
  @record.updated_at
end

#move_to(new_path) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/carrierwave/storage/postgresql_table.rb', line 176

def move_to(new_path)
  CarrierWaveFile.transaction do
    # Remove any existing files at the current path.
    CarrierWaveFile.delete_all_files("path = #{CarrierWaveFile.sanitize(new_path)} AND id != #{CarrierWaveFile.sanitize(@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



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

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



113
114
115
# File 'lib/carrierwave/storage/postgresql_table.rb', line 113

def rewind
  @read_pos = 0
end

#sizeObject



105
106
107
# File 'lib/carrierwave/storage/postgresql_table.rb', line 105

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

#store(new_file) ⇒ Object



129
130
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
# File 'lib/carrierwave/storage/postgresql_table.rb', line 129

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 = ""
      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.sanitize(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



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

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



125
126
127
# File 'lib/carrierwave/storage/postgresql_table.rb', line 125

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