Class: Gridfs

Inherits:
Storage show all
Defined in:
lib/attach_it/storage/gridfs.rb

Instance Method Summary collapse

Methods inherited from Storage

#transform

Constructor Details

#initializeGridfs

Returns a new instance of Gridfs.



3
4
5
# File 'lib/attach_it/storage/gridfs.rb', line 3

def initialize
  @grid ||= Mongo::Grid.new(MongoMapper.database)
end

Instance Method Details

#flush_delete(queued_for_delete = nil) ⇒ Object



24
25
26
27
28
# File 'lib/attach_it/storage/gridfs.rb', line 24

def flush_delete(queued_for_delete = nil)
  queued_for_delete.each do |id|
    @grid.delete(id)
  end
end

#flush_write(image_options = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/attach_it/storage/gridfs.rb', line 7

def flush_write(image_options = nil)
  image_options.styles.each do |style_name, style_value|
    begin
      gridfs_id = @grid.put(transform(style_value, image_options.assigned_file.path).to_blob, :filename => style_name.to_s + '_' + image_options.file_name, :_id => "#{image_options.object_id}_#{image_options.name}_#{style_name}")
    rescue Exception => exception
      image_options.add_error(exception.to_s)
    end
  end

  begin
    gridfs_id = @grid.put(image_options.assigned_file, :filename => 'original_' + image_options.file_name, :_id => "#{image_options.object_id}_#{image_options.name}_original")
  rescue Exception => exception
    image_options.add_error(exception.to_s)
  end

end

#read(id = nil) ⇒ Object



30
31
32
# File 'lib/attach_it/storage/gridfs.rb', line 30

def read(id = nil)    
  @grid.get(id) unless id.nil?
end