Dire
Simple Pathname
wrapper that provides convenient
and safe interface for filesystem traversal.
Useful in situations where you need to accept parameters
from external sources (like HTTP request) or read data from
potentially unsafe directory structure (containing symbolic
links).
Main Features:
- simple interface,
- easy to test,
- restrict access to selected directory (paths, links),
- content type recognition (binary, mime),
- ignore pattern support.
Sample Usage:
Restrict access to directory:
root = Dire.root 'path/to/dir'
=> #<Dire::Dir>
Basic Dire::Dir
methods:
dir = root.get 'path/to/subdir'
=> #<Dire::Dir>
dir.dirs
=> [#<Dire::Dir>, #<Dire::Dir>, ...]
dir.files
=> [#<Dire::File>, #<Dire::File>, ...]
root.list
=> [#<Dire::Dir>, #<Dire::File>, ...]
Basic Dire::File
methods:
file = dir.get 'path/to/file.txt'
=> #<Dire::File>
file.binary?
=> false
file.mime
=> #<MimeMagic>
file.text?
=> true
Common Dire::Node
methods:
file.path
=> #<Pathname>
file.absolute_path
=> #<Pathname>
file.param
=> #<String>
file.relative_path
=> #<Pathname>
Ignore patterns:
Dire::IGNORE << 'globbing/pattern'
dir.get 'path/matching/pattern'
For more information check lib
and test.
Rails Usage:
Add dire
gem.
Add route:
get 'controller_path(/:path_parameter)',
as: 'route_name',
to: 'controller_name#action_name',
constraints: { path: /.+/ },
format: false
Force HTML:
before_action :force_html, only: 'action_name'
def force_html
request.format = :html
end