Project

dire

0.0
No release in over a year
Pathname wrapper for safe and convenient filesystem traversal.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.14.4

Runtime

~> 0.4.3
~> 2.6.7
 Project Readme

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>

# check data encoding
file.binary?
=> false

# get MimeMagic (mimemagic gem)
file.mime
=> #<MimeMagic>

# inverse of binary
file.text?
=> true

Common Dire::Node methods:

# associated Pathname (for IO use)
file.path
=> #<Pathname>

# alias of path
file.absolute_path
=> #<Pathname>

# relative as string
file.param
=> #<String>

# relative to root
file.relative_path
=> #<Pathname>

Ignore patterns:

Dire::IGNORE << 'globbing/pattern'

# raises Dire::Error::InvalidPath
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