Commit 82d1bfa9 by Jon Wood

Check for existence of a MIME type database at build time.

Because we're required to build a C extension in order to do so
(don't ask, its a long story), use that C extension to make the
path provided at build time available at run time.
parent cd65290a
require "mkmf"
def locate_mime_database
# User provided path.
unless ENV["FREEDESKTOP_MIME_TYPES_PATH"].nil?
path = File.expand_path(ENV["FREEDESKTOP_MIME_TYPES_PATH"])
unless File.exist?(path)
raise "The path #{path} was provided for the MIME types database, but no file exists at that path."
end
return path
end
# Default path on Linux installs for the MIME types database.
return "/usr/share/mime/packages/freedesktop.org.xml" if File.exist?("/usr/share/mime/packages/freedesktop.org.xml")
raise "No database of MIME types could be found. Ensure you have either installed the shared-mime-types package for your distribution, or obtain a version of freedesktop.org.xml, and set FREEDESKTOP_MIME_TYPES_PATH to the location of that file."
end
mime_database_path = locate_mime_database
$defs.push("-DMIMEDB_PATH=\\\"#{mime_database_path}\\\"")
create_header
create_makefile("mimemagic/mimemagic")
\ No newline at end of file
#include <ruby.h>
#include "extconf.h"
void Init_mimemagic(void) {
VALUE cMimeMagic;
cMimeMagic = rb_const_get(rb_cObject, rb_intern("MimeMagic"));
rb_define_const(cMimeMagic, "DATABASE_PATH", rb_str_new(MIMEDB_PATH, strlen(MIMEDB_PATH)));
}
......@@ -8,7 +8,6 @@ class MimeMagic
TYPES = {}
MAGIC = []
def self.str2int(s)
return s.to_i(16) if s[0..1].downcase == '0x'
return s.to_i(8) if s[0..0].downcase == '0'
......@@ -55,22 +54,10 @@ class MimeMagic
}.compact
end
def self.locate_mime_database
# User provided path.
return ENV["FREEDESKTOP_MIME_TYPES_PATH"] unless ENV["FREEDESKTOP_MIME_TYPES_PATH"].nil?
# Default path on Linux installs for the MIME types database.
return "/usr/share/mime/packages/freedesktop.org.xml" if File.exist?("/usr/share/mime/packages/freedesktop.org.xml")
nil
end
def self.open_mime_database
path = locate_mime_database
return File.open(path) unless path.nil?
# Couldn't find it locally, pull it from the Internet.
raise "MIME types database could not be found. Pulling from the internet is currently unsupported."
require "mimemagic/mimemagic"
path = MimeMagic::DATABASE_PATH
File.open(path)
end
def self.parse_database
......
class MimeMagic
# MimeMagic version string
# @api public
VERSION = '0.4.0'
VERSION = '0.3.7'
end
......@@ -12,11 +12,12 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(test|script)/}) }
s.require_paths = %w(lib)
s.extensions = %w(ext/mimemagic/extconf.rb)
s.summary = 'Fast mime detection by extension or content'
s.description = 'Fast mime detection by extension or content in pure ruby (Uses freedesktop.org.xml shared-mime-info database)'
s.homepage = 'https://github.com/minad/mimemagic'
s.license = 'GPL-2.0'
s.license = 'MIT'
s.add_development_dependency('minitest', '~> 5.14')
s.add_development_dependency('rake', '~> 13.0')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment