use mime types only if we can load it
This commit is contained in:
parent
ff63165bdd
commit
f126518566
|
@ -15,7 +15,10 @@
|
||||||
# ++
|
# ++
|
||||||
|
|
||||||
require 'digest/md5'
|
require 'digest/md5'
|
||||||
|
begin
|
||||||
require 'mime/types'
|
require 'mime/types'
|
||||||
|
rescue LoadError
|
||||||
|
end
|
||||||
|
|
||||||
module Mongo
|
module Mongo
|
||||||
|
|
||||||
|
@ -44,8 +47,8 @@ module Mongo
|
||||||
# @options opts [ObjectID] :_id (ObjectID) a unique id for
|
# @options opts [ObjectID] :_id (ObjectID) a unique id for
|
||||||
# the file to be use in lieu of an automatically generated one.
|
# the file to be use in lieu of an automatically generated one.
|
||||||
# @options opts [String] :content_type ('binary/octet-stream') If no content type is specified,
|
# @options opts [String] :content_type ('binary/octet-stream') If no content type is specified,
|
||||||
# the content type will be inferred from the filename extension. If that can't be done, the default
|
# the content type will may be inferred from the filename extension if the mime-types gem can be
|
||||||
# content type of 'binary/octet-stream' will be used.
|
# loaded. Otherwise, the content type 'binary/octet-stream' will be used.
|
||||||
# @options opts [Boolean] :safe (false) When safe mode is enabled, the chunks sent to the server
|
# @options opts [Boolean] :safe (false) When safe mode is enabled, the chunks sent to the server
|
||||||
# will be validated using an md5 hash. If validation fails, an exception will be raised.
|
# will be validated using an md5 hash. If validation fails, an exception will be raised.
|
||||||
def initialize(files, chunks, filename, mode, opts={})
|
def initialize(files, chunks, filename, mode, opts={})
|
||||||
|
@ -284,7 +287,7 @@ module Mongo
|
||||||
# Initialize the class for writing a file.
|
# Initialize the class for writing a file.
|
||||||
def init_write(opts)
|
def init_write(opts)
|
||||||
@files_id = opts[:_id] || Mongo::ObjectID.new
|
@files_id = opts[:_id] || Mongo::ObjectID.new
|
||||||
@content_type = opts[:content_type] || get_content_type || DEFAULT_CONTENT_TYPE
|
@content_type = opts[:content_type] || (defined? MIME) && get_content_type || DEFAULT_CONTENT_TYPE
|
||||||
@chunk_size = opts[:chunk_size] || DEFAULT_CHUNK_SIZE
|
@chunk_size = opts[:chunk_size] || DEFAULT_CHUNK_SIZE
|
||||||
@file_length = 0
|
@file_length = 0
|
||||||
@metadata = opts[:metadata] if opts[:metadata]
|
@metadata = opts[:metadata] if opts[:metadata]
|
||||||
|
|
|
@ -52,12 +52,15 @@ class GridIOTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
context "Content types" do
|
context "Content types" do
|
||||||
should "determine common content types from the extension" do
|
|
||||||
file = GridIO.new(@files, @chunks, 'sample.pdf', 'w')
|
|
||||||
assert_equal 'application/pdf', file.content_type
|
|
||||||
|
|
||||||
file = GridIO.new(@files, @chunks, 'sample.txt', 'w')
|
if defined?(MIME)
|
||||||
assert_equal 'text/plain', file.content_type
|
should "determine common content types from the extension" do
|
||||||
|
file = GridIO.new(@files, @chunks, 'sample.pdf', 'w')
|
||||||
|
assert_equal 'application/pdf', file.content_type
|
||||||
|
|
||||||
|
file = GridIO.new(@files, @chunks, 'sample.txt', 'w')
|
||||||
|
assert_equal 'text/plain', file.content_type
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "default to binary/octet-stream when type is unknown" do
|
should "default to binary/octet-stream when type is unknown" do
|
||||||
|
|
Loading…
Reference in New Issue