Skip to content

Commit

Permalink
Support for loading a freedesktop.org.xml that exists locally
Browse files Browse the repository at this point in the history
Currently looks at the value of `FREEDESKTOP_MIME_TYPES_PATH` and
in `/usr/share/mime/packages/freedesktop.org.xml`, which is the
path you would expect to find that file at on a typical Linux
system.
  • Loading branch information
jellybob committed Mar 24, 2021
1 parent f95088a commit 641561d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/mimemagic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

require 'stringio'

MimeMagic.parse_database("script/freedesktop.org.xml")
MimeMagic.parse_database

# Mime type detection
class MimeMagic
Expand Down
23 changes: 21 additions & 2 deletions lib/mimemagic/tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,27 @@ def self.get_matches(parent)
}.compact
end

def self.parse_database(source_path)
file = File.new(source_path)
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."
end

def self.parse_database
file = open_mime_database

doc = Nokogiri::XML(file)
extensions = {}
types = {}
Expand Down

0 comments on commit 641561d

Please sign in to comment.