use mime types only if we can load it

This commit is contained in:
Kyle Banker 2010-02-23 18:25:28 -05:00
parent ff63165bdd
commit f126518566
2 changed files with 14 additions and 8 deletions

View File

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

View File

@ -52,6 +52,8 @@ class GridIOTest < Test::Unit::TestCase
end end
context "Content types" do context "Content types" do
if defined?(MIME)
should "determine common content types from the extension" do should "determine common content types from the extension" do
file = GridIO.new(@files, @chunks, 'sample.pdf', 'w') file = GridIO.new(@files, @chunks, 'sample.pdf', 'w')
assert_equal 'application/pdf', file.content_type assert_equal 'application/pdf', file.content_type
@ -59,6 +61,7 @@ class GridIOTest < Test::Unit::TestCase
file = GridIO.new(@files, @chunks, 'sample.txt', 'w') file = GridIO.new(@files, @chunks, 'sample.txt', 'w')
assert_equal 'text/plain', file.content_type 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
file = GridIO.new(@files, @chunks, 'sample.l33t', 'w') file = GridIO.new(@files, @chunks, 'sample.l33t', 'w')