Module: MongoGrid

Extended by:
MongoGrid
Included in:
MongoGrid
Defined in:
lib/mongo_grid.rb,
lib/mongo_grid/version.rb

Constant Summary collapse

VERSION =
"0.2.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#db_nameObject

Returns the value of attribute db_name.



5
6
7
# File 'lib/mongo_grid.rb', line 5

def db_name
  @db_name
end

#db_urlObject

Returns the value of attribute db_url.



5
6
7
# File 'lib/mongo_grid.rb', line 5

def db_url
  @db_url
end

Instance Method Details

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (MongoGrid)

    the object that the method was called on



8
9
10
# File 'lib/mongo_grid.rb', line 8

def configure
  yield self
end

#fsize(length) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/mongo_grid.rb', line 22

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

#gridObject



12
13
14
15
# File 'lib/mongo_grid.rb', line 12

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

#remove(gid) ⇒ Object



17
18
19
20
# File 'lib/mongo_grid.rb', line 17

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

#savetogrid(fpath, fname = "poster.jpg", content_type = 'image/jpeg') ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/mongo_grid.rb', line 48

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 = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mongo_grid.rb', line 33

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