Sha256: 745380a643495c799e9378e7b080d377c9e07c6f23db98404e880447b125d390

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

require 'zbox'
require 'mongo_grid/version'

#Dir[File.join(File.dirname(__FILE__), 'mongo_grid', '*.rb')].each do |extension|
#  require extension
#end

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 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

  def uploadtogrid(upload,opts={})
    filename=upload.original_filename
    content_type=upload.content_type
    if content_type.include?("image")
      ::Zbox::Qm.resize(upload.tempfile.path,opts)
    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

  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 
end


require 'mongo_grid/mongoid'
require 'mongo_grid/action_base'

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_grid-0.2.7 lib/mongo_grid.rb
mongo_grid-0.2.6 lib/mongo_grid.rb