Sha256: a3af18f75d20e25756648fe80396b3b7513371f9c6b1b9ff59ee7160c28a9a3b

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require "mongo_grid/version"
require 'zbox'

module ::MongoGrid
  attr_accessor :db_name, :db_url
  extend self

  def configure
    yield self
  end  

  def grid
    client = Mongo::Client.new([db_url], :database => db_name)
    client.database.fs
  end

  def remove(gid)
    id = BSON::ObjectId.from_string(gid)
    grid.delete(id)
  end

  def uploadtogrid(upload)
    filename=upload.original_filename
    content_type=upload.content_type
    if content_type.include?("image")
      ::Zbox::Qm.resize(upload.tempfile.path,:width=>900)
    end
    data = File.open(upload.tempfile.path)
    gfile = ::Mongo::Grid::File.new(data,filename: filename, content_type: content_type)
    gid = grid.insert_one(gfile)
    grid_data=grid.find_one(_id: gid)
    length=grid_data.chunk_size
    file_size =
      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
    hsh = {:grid_id=>gid.to_s,:filename=>filename,
           :content_type=>content_type,:file_size=>file_size}
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_grid-0.1.2 lib/mongo_grid.rb
mongo_grid-0.1.1 lib/mongo_grid.rb