New option ctor arg takes :content_type and :chunk_size

This commit is contained in:
Jim Menard 2009-01-30 14:54:58 -05:00
parent 6532dd7d9b
commit b0612054f9
2 changed files with 64 additions and 6 deletions

View File

@ -28,6 +28,9 @@ module XGen
# } # }
class GridStore class GridStore
DEFAULT_ROOT_COLLECTION = 'gridfs'
DEFAULT_CONTENT_TYPE = 'text/plain'
include Enumerable include Enumerable
attr_accessor :filename attr_accessor :filename
@ -35,10 +38,13 @@ module XGen
# Array of strings; may be +nil+ # Array of strings; may be +nil+
attr_accessor :aliases attr_accessor :aliases
# Default is 'text/plain' # Default is DEFAULT_CONTENT_TYPE
attr_accessor :content_type attr_accessor :content_type
attr_reader :object_id, :upload_date attr_reader :object_id
# Time that the file was first saved.
attr_reader :upload_date
attr_reader :chunk_size attr_reader :chunk_size
@ -50,8 +56,8 @@ module XGen
db.collection('_files').find({'filename' => name}).next_object.nil? db.collection('_files').find({'filename' => name}).next_object.nil?
end end
def open(db, name, mode) def open(db, name, mode, options={})
gs = self.new(db, name, mode) gs = self.new(db, name, mode, options)
result = nil result = nil
begin begin
result = yield gs if block_given? result = yield gs if block_given?
@ -90,7 +96,17 @@ module XGen
#+++ #+++
# Mode may only be 'r', 'w', or 'w+'. # Mode may only be 'r', 'w', or 'w+'.
def initialize(db, name, mode='r') #
# Options:
#
# :chunk_size :: Ignored if mode is 'r' or 'w+'. Sets chunk size for
# files opened for writing ('w'). See also #chunk_size=
# which may only be called before any data is written.
#
# :content_type :: Ignored if mode is 'r' or 'w+'. String. Default
# value is DEFAULT_CONTENT_TYPE. See also
# #content_type=
def initialize(db, name, mode='r', options={})
@db, @filename, @mode = db, name, mode @db, @filename, @mode = db, name, mode
doc = @db.collection('_files').find({'filename' => @filename}).next_object doc = @db.collection('_files').find({'filename' => @filename}).next_object
@ -112,7 +128,7 @@ module XGen
else else
@upload_date = Time.new @upload_date = Time.new
@chunk_size = Chunk::DEFAULT_CHUNK_SIZE @chunk_size = Chunk::DEFAULT_CHUNK_SIZE
@content_type = 'text/plain' @content_type = DEFAULT_CONTENT_TYPE
@length = 0 @length = 0
end end
@ -122,6 +138,8 @@ module XGen
when 'w' when 'w'
delete_chunks delete_chunks
@first_chunk = @curr_chunk = nil @first_chunk = @curr_chunk = nil
@content_type = options[:content_type] if options[:content_type]
@chunk_size = options[:chunk_size] if options[:chunk_size]
when 'w+' when 'w+'
@curr_chunk = find_last_chunk @curr_chunk = find_last_chunk
@curr_chunk.pos = @curr_chunk.data.length if @curr_chunk @curr_chunk.pos = @curr_chunk.data.length if @curr_chunk
@ -340,6 +358,7 @@ module XGen
files.remove('_id' => @object_id) files.remove('_id' => @object_id)
else else
@object_id = XGen::Mongo::Driver::ObjectID.new @object_id = XGen::Mongo::Driver::ObjectID.new
@upload_date = Time.now
end end
files.insert(to_mongo_object) files.insert(to_mongo_object)
end end

View File

@ -174,4 +174,43 @@ class GridStoreTest < Test::Unit::TestCase
} }
end end
def test_chunk_size_in_option
GridStore.open(@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
GridStore.open(@db, 'new-file', 'r') { |f|
assert f.chunk_size == 42
}
end
def test_upload_date
now = Time.now
orig_file_upload_date = nil
GridStore.open(@db, 'foobar', 'r') { |f| orig_file_upload_date = f.upload_date }
assert_not_nil orig_file_upload_date
assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
sleep(2)
GridStore.open(@db, 'foobar', 'w') { |f| f.write "new data" }
file_upload_date = nil
GridStore.open(@db, 'foobar', 'r') { |f| file_upload_date = f.upload_date }
assert_equal orig_file_upload_date, file_upload_date
end
def test_content_type
ct = nil
GridStore.open(@db, 'foobar', 'r') { |f| ct = f.content_type }
assert_equal GridStore::DEFAULT_CONTENT_TYPE, ct
GridStore.open(@db, 'foobar', 'w+') { |f| f.content_type = 'text/html' }
ct2 = nil
GridStore.open(@db, 'foobar', 'r') { |f| ct2 = f.content_type }
assert_equal 'text/html', ct2
end
def test_content_type_option
GridStore.open(@db, 'new-file', 'w', :content_type => 'image/jpg') { |f| f.write('foo') }
ct = nil
GridStore.open(@db, 'new-file', 'r') { |f| ct = f.content_type }
assert_equal 'image/jpg', ct
end
end end