module MongoGrid

Constants

VERSION

Attributes

db_name[RW]
db_url[RW]

Public Instance Methods

configure() { |self| ... } click to toggle source
# File lib/mongo_grid.rb, line 11
def configure
  yield self
end
fsize(length) click to toggle source
# File lib/mongo_grid.rb, line 25
def fsize(length)
  case length
  when 0..(1024**2)
    (length.to_f/1024.to_f).round(1).to_s+"K"
  when (1024**2)...(1024**3)
    (length.to_f/(1024**2).to_f).round(1).to_s+"M"
  when (1024**3)...(1024**4)
    (length.to_f/(1024**3).to_f).round(1).to_s+"G"
  end
end
grid() click to toggle source
# File lib/mongo_grid.rb, line 15
def grid
  client = Mongo::Client.new([db_url], :database => db_name)
  client.database.fs
end
remove(gid) click to toggle source
# File lib/mongo_grid.rb, line 20
def remove(gid)
  id = BSON::ObjectId.from_string(gid)
  grid.delete(id)
end
savetogrid(fpath,fname="poster.jpg",content_type='image/jpeg') click to toggle source
# File lib/mongo_grid.rb, line 55
def savetogrid(fpath,fname="poster.jpg",content_type='image/jpeg')
  data = File.open(fpath)
  length=File.size(fpath)
  gfile = ::Mongo::Grid::File.new(data,filename: fname, metadata: {content_type: content_type,length: length})
  gid = grid.insert_one(gfile)
  file_size = fsize(length)
  hsh = {:grid_id=>gid.to_s,:filename=>fname,
         :content_type=>content_type,:file_size=>file_size}

end
uploadtogrid(upload,opts={}) click to toggle source
# File lib/mongo_grid.rb, line 36
def uploadtogrid(upload,opts={})
  filename=upload.original_filename
  content_type=upload.content_type
  #if /jpg|jpeg|png/ =~ content_type
  #  if opts[:width]
  #    %x[resize -fixed -w #{opts[:width]} #{upload.tempfile.path}]
  #  else
  #    %x[resize -fixed #{upload.tempfile.path}]
  #  end
  #end
  data = File.open(upload.tempfile.path)
  length=File.size(upload.tempfile.path)
  gfile = ::Mongo::Grid::File.new(data,filename: filename, metadata: {content_type: content_type,length: length})
  gid = grid.insert_one(gfile)
  file_size = fsize(length)
  hsh = {:grid_id=>gid.to_s,:filename=>filename,
         :content_type=>content_type,:file_size=>file_size}
end