dire 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e906e8fff287d41ee5dd703c581b631ab621fdff3a7756dcd42cea4b1c533866
4
+ data.tar.gz: 51e92742b580410563a5860dbcbad8dde8a257a5a5520a88bbdb3cb15b53f48d
5
+ SHA512:
6
+ metadata.gz: cceb3d5d623e467593dd395b302116d0e95a89ccc69908434c7cd1dc74af57d6eef4de4b34e3a6e44d1a2b1d8170d0a03a657e17b7557b3936fe79951ca6176a
7
+ data.tar.gz: 568fdf77d605419283ebb50ce7da44e35aa4af773ed01facb02319369e1fb641d54c582497847b23825bb71262f741d648d790fee2fd7f05f7b54bede11650ea
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Filip Pyda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ Dire
2
+ ====
3
+
4
+ Simple `Pathname` wrapper that provides convenient
5
+ and safe interface for filesystem traversal.
6
+
7
+ Useful in situations where you need to accept parameters
8
+ from external sources (like HTTP request) or read data from
9
+ potentially unsafe directory structure (containing symbolic
10
+ links).
11
+
12
+ Main Features:
13
+ --------------
14
+
15
+ * simple interface,
16
+ * easy to test,
17
+ * restrict access to selected directory (paths, links),
18
+ * content type recognition (binary, mime),
19
+ * ignore pattern support.
20
+
21
+ Sample Usage:
22
+ -------------
23
+
24
+ Restrict access to directory:
25
+
26
+ ```ruby
27
+ root = Dire.root 'path/to/dir'
28
+ => #<Dire::Dir>
29
+ ```
30
+
31
+ Basic `Dire::Dir` methods:
32
+
33
+ ```ruby
34
+ dir = root.get 'path/to/subdir'
35
+ => #<Dire::Dir>
36
+
37
+ dir.dirs
38
+ => [#<Dire::Dir>, #<Dire::Dir>, ...]
39
+
40
+ dir.files
41
+ => [#<Dire::File>, #<Dire::File>, ...]
42
+
43
+ root.list
44
+ => [#<Dire::Dir>, #<Dire::File>, ...]
45
+ ```
46
+
47
+ Basic `Dire::File` methods:
48
+
49
+ ```ruby
50
+ file = dir.get 'path/to/file.txt'
51
+ => #<Dire::File>
52
+
53
+ # check data encoding
54
+ file.binary?
55
+ => false
56
+
57
+ # get MimeMagic (mimemagic gem)
58
+ file.mime
59
+ => #<MimeMagic>
60
+
61
+ # inverse of binary
62
+ file.text?
63
+ => true
64
+ ```
65
+
66
+ Common `Dire::Node` methods:
67
+
68
+ ```ruby
69
+ # associated Pathname (for IO use)
70
+ file.path
71
+ => #<Pathname>
72
+
73
+ # alias of path
74
+ file.absolute_path
75
+ => #<Pathname>
76
+
77
+ # relative as string
78
+ file.param
79
+ => #<String>
80
+
81
+ # relative to root
82
+ file.relative_path
83
+ => #<Pathname>
84
+ ```
85
+
86
+ Ignore patterns:
87
+
88
+ ```ruby
89
+ Dire::IGNORE << 'globbing/pattern'
90
+
91
+ # raises Dire::Error::InvalidPath
92
+ dir.get 'path/matching/pattern'
93
+ ```
94
+
95
+ For more information check [lib](https://github.com/f6p/dire/tree/master/lib)
96
+ and [test](https://github.com/f6p/dire/tree/master/test).
97
+
98
+ Rails Usage:
99
+ ------------
100
+
101
+ Add `dire` gem.
102
+
103
+ Add route:
104
+
105
+ ```ruby
106
+ get 'controller_path(/:path_parameter)',
107
+ as: 'route_name',
108
+ to: 'controller_name#action_name',
109
+ constraints: { path: /.+/ },
110
+ format: false
111
+ ```
112
+
113
+ Force HTML:
114
+
115
+ ```ruby
116
+ before_action :force_html, only: 'action_name'
117
+
118
+ def force_html
119
+ request.format = :html
120
+ end
121
+ ```
data/lib/dire/dir.rb ADDED
@@ -0,0 +1,56 @@
1
+ module Dire
2
+ class Dir < Node
3
+ @@invalid_link_handler = -> (path) {}
4
+
5
+ def self.invalid_link_handler= lambda
6
+ @@invalid_link_handler = lambda
7
+ end
8
+
9
+ @@invalid_path_handler = -> (path) {}
10
+
11
+ def self.invalid_path_handler= lambda
12
+ @@invalid_path_handler = lambda
13
+ end
14
+
15
+ def dirs
16
+ nodes.select { |i| i.absolute_path.directory? }
17
+ end
18
+
19
+ def files
20
+ nodes.select { |i| not i.absolute_path.directory? }
21
+ end
22
+
23
+ def list
24
+ dirs + files
25
+ end
26
+
27
+ def root?
28
+ not parent
29
+ end
30
+
31
+ def validate!
32
+ super && validate_type!('directory')
33
+ end
34
+
35
+ private
36
+
37
+ def nodes
38
+ return @nodes if @nodes
39
+
40
+ @nodes = absolute_path.glob '*', ::File::FNM_DOTMATCH
41
+ @nodes = @nodes.filter_map do |node|
42
+ next if ignore? node
43
+
44
+ if absolute_path.to_s < node.to_s
45
+ begin
46
+ get node
47
+ rescue Dire::Error::InvalidLink
48
+ @@invalid_link_handler.(node)
49
+ rescue Dire::Error::InvalidPath
50
+ @@invalid_path_handler.(node)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ module Dire
2
+ module Error
3
+ class Base < StandardError ; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Dire
2
+ module Error
3
+ class InvalidLink < Base ; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Dire
2
+ module Error
3
+ class InvalidPath < Base ; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Dire
2
+ module Error
3
+ class InvalidRoot < Base ; end
4
+ end
5
+ end
data/lib/dire/file.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'mimemagic'
2
+
3
+ module Dire
4
+ class File < Node
5
+ def binary?
6
+ return @binary if @binary
7
+
8
+ unless %w(application text).include? mime.mediatype
9
+ return @binary = true
10
+ end
11
+
12
+ @binary = !absolute_path.read.valid_encoding? rescue true
13
+ end
14
+
15
+ def mime
16
+ @mime ||= MimeMagic.by_magic absolute_path.open
17
+ end
18
+
19
+ def text?
20
+ !binary?
21
+ end
22
+
23
+ def validate!
24
+ super && validate_type!('file')
25
+ end
26
+ end
27
+ end
data/lib/dire/link.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Dire
2
+ class Link < Node
3
+ def broken?
4
+ !link.exist? || !inside?(link)
5
+ end
6
+
7
+ def param
8
+ chop(link).to_s
9
+ end
10
+
11
+ def validate!
12
+ super && validate_type!('link')
13
+
14
+ if broken?
15
+ raise Dire::Error::InvalidLink, 'Dead link'
16
+ end
17
+
18
+ true
19
+ end
20
+
21
+ private
22
+
23
+ def link
24
+ path.readlink.expand_path path.dirname
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,57 @@
1
+ module Dire::Mixin::Check
2
+ def == other
3
+ path.to_s == other.path.to_s
4
+ end
5
+
6
+ def broken?
7
+ false
8
+ end
9
+
10
+ def known?
11
+ true
12
+ end
13
+
14
+ def parent? node
15
+ parent == node
16
+ end
17
+
18
+ def root?
19
+ false
20
+ end
21
+
22
+ def validate!
23
+ begin
24
+ raise Errno::ENOENT if ignore? path
25
+
26
+ path.lstat
27
+ rescue Errno::ENOENT
28
+ raise Dire::Error::InvalidPath, 'Not found'
29
+ end
30
+
31
+ unless inside? path
32
+ raise Dire::Error::InvalidPath, 'Outside root'
33
+ end
34
+
35
+ true
36
+ end
37
+
38
+ private
39
+
40
+ def ignore? path
41
+ Dire::IGNORE.any? do |ignore|
42
+ File.fnmatch? ignore, path, File::FNM_PATHNAME
43
+ end
44
+ end
45
+
46
+ def inside? path
47
+ expand(path).to_s.index(root.to_s) == 0
48
+ end
49
+
50
+ def validate_type! name
51
+ unless type == name
52
+ raise Dire::Error::InvalidPath, 'Node missmatch'
53
+ end
54
+
55
+ true
56
+ end
57
+ end
@@ -0,0 +1,23 @@
1
+ module Dire::Mixin::Path
2
+ def absolute_path
3
+ path
4
+ end
5
+
6
+ def param
7
+ relative_path.to_s
8
+ end
9
+
10
+ def relative_path
11
+ chop path
12
+ end
13
+
14
+ private
15
+
16
+ def chop pth
17
+ expand(pth).relative_path_from(root)
18
+ end
19
+
20
+ def expand pth
21
+ path.join(pth).expand_path
22
+ end
23
+ end
data/lib/dire/node.rb ADDED
@@ -0,0 +1,60 @@
1
+ module Dire
2
+ class Node
3
+ include Mixin::Check
4
+ include Mixin::Path
5
+
6
+ attr_reader :path, :root
7
+
8
+ def initialize node, root, validate = true
9
+ @path = str2abs node
10
+ @root = plant root
11
+
12
+ validate! if validate
13
+ end
14
+
15
+ def get path, validate = true
16
+ path = expand path
17
+ type = path.ftype rescue nil
18
+
19
+ const = case type
20
+ when 'directory' then Dir
21
+ when 'file' then File
22
+ when 'link' then Link
23
+
24
+ else Other
25
+ end
26
+
27
+ const.new path, root, validate
28
+ end
29
+
30
+ def parent
31
+ Dire::Dir.new absolute_path.join('..'), root
32
+ rescue Error::InvalidPath
33
+ nil
34
+ end
35
+
36
+ def to_s
37
+ relative_path.to_s
38
+ end
39
+
40
+ def type
41
+ path.ftype rescue 'other'
42
+ end
43
+
44
+ private
45
+
46
+ def plant root
47
+ root = str2abs root
48
+
49
+ unless root.directory?
50
+ raise Dire::Error::InvalidRoot, 'Not a dir'
51
+ end
52
+
53
+ root
54
+ end
55
+
56
+ def str2abs path
57
+ Pathname.new(path).expand_path
58
+ end
59
+ end
60
+ end
data/lib/dire/other.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Dire
2
+ class Other < Node
3
+ def known?
4
+ false
5
+ end
6
+
7
+ def type
8
+ 'other'
9
+ end
10
+
11
+ def validate!
12
+ super && validate_type!('other')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Dire
2
+ VERSION = "0.1.0"
3
+ end
data/lib/dire.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'zeitwerk'
2
+
3
+ loader = Zeitwerk::Loader.for_gem
4
+ loader.setup
5
+
6
+ module Dire
7
+ IGNORE=%w()
8
+
9
+ def self.root path
10
+ Dire::Dir.new path, path
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Filip Pyda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mimemagic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: zeitwerk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.4.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.4.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.14.4
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.14.4
55
+ description: Personal
56
+ email:
57
+ - filip.pyda@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files:
61
+ - README.md
62
+ - LICENSE.txt
63
+ files:
64
+ - LICENSE.txt
65
+ - README.md
66
+ - lib/dire.rb
67
+ - lib/dire/dir.rb
68
+ - lib/dire/error/base.rb
69
+ - lib/dire/error/invalid_link.rb
70
+ - lib/dire/error/invalid_path.rb
71
+ - lib/dire/error/invalid_root.rb
72
+ - lib/dire/file.rb
73
+ - lib/dire/link.rb
74
+ - lib/dire/mixin/check.rb
75
+ - lib/dire/mixin/path.rb
76
+ - lib/dire/node.rb
77
+ - lib/dire/other.rb
78
+ - lib/dire/version.rb
79
+ homepage: https://github.com/f6p/dire
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.2.22
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Filesystem Browser
102
+ test_files: []