Allow custom db paths
This commit is contained in:
parent
087b9754d6
commit
5086550004
11
README.md
11
README.md
|
@ -34,6 +34,17 @@ MiniMime.lookup_by_content_type("text/plain").binary?
|
|||
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
If you'd like to add your own mime types, try using custom database files:
|
||||
|
||||
```
|
||||
MiniMime::Configuration.ext_db_path = "path_to_file_extension_db"
|
||||
MiniMime::Configuration.content_type_db_path = "path_to_content_type_db"
|
||||
```
|
||||
|
||||
Check out the [default databases](lib/db) for proper formatting and structure hints.
|
||||
|
||||
## Performance
|
||||
|
||||
MiniMime is optimised to minimize memory usage. It keeps a cache of 100 mime type lookups (and 100 misses). There are benchmarks in the [bench directory](https://github.com/discourse/mini_mime/blob/master/bench/bench.rb)
|
||||
|
|
|
@ -14,6 +14,16 @@ module MiniMime
|
|||
Db.lookup_by_content_type(mime)
|
||||
end
|
||||
|
||||
module Configuration
|
||||
class << self
|
||||
attr_accessor :ext_db_path
|
||||
attr_accessor :content_type_db_path
|
||||
end
|
||||
|
||||
self.ext_db_path = File.expand_path("../db/ext_mime.db", __FILE__)
|
||||
self.content_type_db_path = File.expand_path("../db/content_type_mime.db", __FILE__)
|
||||
end
|
||||
|
||||
class Info
|
||||
BINARY_ENCODINGS = %w(base64 8bit)
|
||||
|
||||
|
@ -83,8 +93,8 @@ module MiniMime
|
|||
class RandomAccessDb
|
||||
MAX_CACHED = 100
|
||||
|
||||
def initialize(name, sort_order)
|
||||
@path = File.expand_path("../db/#{name}", __FILE__)
|
||||
def initialize(path, sort_order)
|
||||
@path = path
|
||||
@file = File.open(@path)
|
||||
|
||||
@row_length = @file.readline.length
|
||||
|
@ -141,8 +151,8 @@ module MiniMime
|
|||
end
|
||||
|
||||
def initialize
|
||||
@ext_db = RandomAccessDb.new("ext_mime.db", 0)
|
||||
@content_type_db = RandomAccessDb.new("content_type_mime.db", 1)
|
||||
@ext_db = RandomAccessDb.new(Configuration.ext_db_path, 0)
|
||||
@content_type_db = RandomAccessDb.new(Configuration.content_type_db_path, 1)
|
||||
end
|
||||
|
||||
def lookup_by_extension(extension)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
liquid application/x-liquid 8bit
|
||||
mp4 video/vnd.objectvideo quoted-printable
|
|
@ -0,0 +1,2 @@
|
|||
lua application/x-lua 8bit
|
||||
m4v video/vnd.objectvideo quoted-printable
|
|
@ -0,0 +1,32 @@
|
|||
require 'test_helper'
|
||||
|
||||
class MiniMime::ConfigurationTest < Minitest::Test
|
||||
CUSTOM_EXT_MIME_DB_PATH = File.expand_path("../../fixtures/custom_ext_mime.db", __FILE__)
|
||||
CUSTOM_CONTENT_TYPE_MIME_DB_PATH = File.expand_path("../../fixtures/custom_content_type_mime.db", __FILE__)
|
||||
|
||||
def setup
|
||||
MiniMime::Db.instance_variable_set(:@db, nil)
|
||||
@old_ext_db_path = MiniMime::Configuration.ext_db_path
|
||||
@old_content_type_db_path = MiniMime::Configuration.content_type_db_path
|
||||
end
|
||||
|
||||
def teardown
|
||||
MiniMime::Db.instance_variable_set(:@db, nil)
|
||||
MiniMime::Configuration.ext_db_path = @old_ext_db_path
|
||||
MiniMime::Configuration.content_type_db_path = @old_content_type_db_path
|
||||
end
|
||||
|
||||
def test_using_custom_ext_mime_db
|
||||
MiniMime::Configuration.ext_db_path = CUSTOM_EXT_MIME_DB_PATH
|
||||
|
||||
assert_equal "application/x-lua", MiniMime.lookup_by_extension("lua").content_type
|
||||
assert_equal "quoted-printable", MiniMime.lookup_by_extension("m4v").encoding
|
||||
end
|
||||
|
||||
def test_using_custom_content_type_mime_db
|
||||
MiniMime::Configuration.content_type_db_path = CUSTOM_CONTENT_TYPE_MIME_DB_PATH
|
||||
|
||||
assert_equal "liquid", MiniMime.lookup_by_content_type("application/x-liquid").extension
|
||||
assert_equal "quoted-printable", MiniMime.lookup_by_content_type("video/vnd.objectvideo").encoding
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue