Skip to content

Commit

Permalink
updated mongo storage to new GridFileSystem API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sho Fukamachi authored and jnicklas committed May 6, 2010
1 parent c07f53a commit 8f4d363
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
17 changes: 11 additions & 6 deletions lib/carrierwave/storage/grid_fs.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8
require 'mongo'
require 'mongo/gridfs'
include Mongo

module CarrierWave
module Storage
Expand All @@ -14,6 +14,7 @@ class File

def initialize(uploader, database, path)
@database = database
@grid = GridFileSystem.new(@database)
@path = path
@uploader = uploader
end
Expand All @@ -31,15 +32,15 @@ def url
end

def read
::GridFS::GridStore.read(@database, @path)
@grid.open(@path, 'r').data
end

def delete
::GridFS::GridStore.unlink(@database, @path)
@grid.delete(@path)
end

def content_type
::GridFS::GridStore.open(@database, @path, 'r') { |f| return f.content_type }
@grid.open(@path, 'r').content_type
end

end
Expand All @@ -56,7 +57,7 @@ def content_type
# [CarrierWave::SanitizedFile] a sanitized file
#
def store!(file)
::GridFS::GridStore.open(database, uploader.store_path, 'w', :content_type => file.content_type) do |f|
grid.open(uploader.store_path, 'w', :content_type => file.content_type) do |f|
f.write file.read
end
CarrierWave::Storage::GridFS::File.new(uploader, database, uploader.store_path)
Expand Down Expand Up @@ -91,7 +92,11 @@ def database
db
end
end


def grid
GridFileSystem.new(database)
end

end # File
end # Storage
end # CarrierWave
14 changes: 9 additions & 5 deletions spec/storage/grid_fs_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# encoding: utf-8

require File.dirname(__FILE__) + '/../spec_helper'
require 'mongo'
include Mongo

describe CarrierWave::Storage::GridFS do

Expand All @@ -13,13 +15,15 @@
@uploader.stub!(:grid_fs_access_url).and_return(nil)
@uploader.stub!(:grid_fs_username).and_return(nil)
@uploader.stub!(:grid_fs_password).and_return(nil)

@grid = GridFileSystem.new(@database)

@storage = CarrierWave::Storage::GridFS.new(@uploader)
@file = stub_tempfile('test.jpg', 'application/xml')
end

after do
GridFS::GridStore.unlink(@database, 'uploads/bar.txt')
@grid.delete('uploads/bar.txt')
end

describe '#store!' do
Expand All @@ -29,7 +33,7 @@
end

it "should upload the file to gridfs" do
GridFS::GridStore.read(@database, 'uploads/bar.txt').should == 'this is stuff'
@grid.open('uploads/bar.txt', 'r').data.should == 'this is stuff'
end

it "should not have a path" do
Expand All @@ -42,7 +46,7 @@

it "should be deletable" do
@grid_fs_file.delete
GridFS::GridStore.read(@database, 'uploads/bar.txt').should == ''
lambda {@grid.open('uploads/bar.txt', 'r')}.should raise_error(Mongo::GridFileNotFound)
end

it "should store the content type on GridFS" do
Expand All @@ -52,7 +56,7 @@

describe '#retrieve!' do
before do
GridFS::GridStore.open(@database, 'uploads/bar.txt', 'w') { |f| f.puts "A test, 1234" }
@grid.open('uploads/bar.txt', 'w') { |f| f.write "A test, 1234" }
@uploader.stub!(:store_path).with('bar.txt').and_return('uploads/bar.txt')
@grid_fs_file = @storage.retrieve!('bar.txt')
end
Expand All @@ -76,7 +80,7 @@

it "should be deletable" do
@grid_fs_file.delete
GridFS::GridStore.read(@database, 'uploads/bar.txt').should == ''
lambda {@grid.open('uploads/bar.txt', 'r')}.should raise_error(Mongo::GridFileNotFound)
end
end

Expand Down

0 comments on commit 8f4d363

Please sign in to comment.