MagicMime
Elixir wrapper for MIME type detection using the system's file command.
Features
- Wrapper around the system file command
- Binary detection (not guess from file extension)
- Parallel processing support for multiple files
Requirements
- System file command must be available
Note: Windows is not supported.
Installation
Add magic_mime to your list of dependencies in mix.exs:
def deps do
[
{:magic_mime, "~> 0.1"}
]
end
Usage
Basic Detection
# Detect MIME type for a single file
{:ok, mime_type} = MagicMime.detect("path/to/file.png")
# => {:ok, "image/png"}
# Exception-raising version
mime_type = MagicMime.detect!("path/to/file.png")
# => "image/png"
Parallel Processing
# Process multiple files in parallel
paths = ["image.png", "document.pdf", "data.json"]
results = MagicMime.detect_many(paths)
# => [
# {"image.png", {:ok, "image/png"}},
# {"document.pdf", {:ok, "application/pdf"}},
# {"data.json", {:ok, "application/json"}}
# ]
# Specify concurrency level
results = MagicMime.detect_many(paths, concurrency: 4)
System Support Check
# Check if file command is available
MagicMime.mime_supported?()
# => true
# Get file command version information
MagicMime.version()
# => "file-5.44"
Error Handling
MagicMime provides the following error types:
:file_command_not_found
- file command not found:enoent
- file does not exist:eacces
- permission denied{:command_error, exit_code, stderr}
- command execution error
case MagicMime.detect("nonexistent.file") do
{:ok, mime_type} -> IO.puts("MIME Type: #{mime_type}")
{:error, :enoent} -> IO.puts("File not found")
{:error, reason} -> IO.puts("Error: #{inspect(reason)}")
end
Development
Setup
# Install dependencies
mix deps.get
# Run tests
mix test
# Generate documentation
mix docs
Code Quality
# Format code
mix format
# Static analysis
mix credo --strict
# Type checking
mix dialyzer
Contributing
Pull requests and issue reports are welcome.
- Fork the repository
- Create a feature branch (git checkout -b feature/amazing-feature)
- Commit your changes (git commit -am 'Add amazing feature')
- Push to the branch (git push origin feature/amazing-feature)
- Open a Pull Request
License
MIT License.