koine-filesystem 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +1 -0
- data/.rubocop.yml +66 -0
- data/.travis.yml +11 -0
- data/CHANGELOG +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/koine-filesystem.gemspec +31 -0
- data/lib/koine/filesystem.rb +14 -0
- data/lib/koine/filesystem/adapters/base.rb +160 -0
- data/lib/koine/filesystem/adapters/local.rb +101 -0
- data/lib/koine/filesystem/filesystem.rb +77 -0
- data/lib/koine/filesystem/path_sanitizer.rb +13 -0
- data/lib/koine/filesystem/version.rb +7 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e234a9220a0e67f7ba54c18ca8b06e2a1a7ccc2f1f61c002a32b98dffaeac878
|
4
|
+
data.tar.gz: b983c2f922cbdd1e027b75ca194445dad29802615e8fab57be017954aaaed8dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 929e9d0d1348f8aadfaf5193fb20ce790c97b626b1c4f9ed685662857378ace1eee06ea1c36ec8e5cbf9800c1e9d2595b0a2b5f5db1692b1d6e6fd1f722b9ed8
|
7
|
+
data.tar.gz: b7e4b678109970ad8ef71ceba4a9ab80798d2c4fadcead94957710284d019654ac0da5585ed2142448088e6b9632c86505051eb66d09fa29bb7739629b901f3c
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
Metrics/BlockLength:
|
6
|
+
Exclude:
|
7
|
+
- 'spec/**/*'
|
8
|
+
|
9
|
+
Metrics/MethodLength:
|
10
|
+
Exclude:
|
11
|
+
- 'db/migrate/**.rb'
|
12
|
+
|
13
|
+
Layout/LineLength:
|
14
|
+
Max: 100
|
15
|
+
Exclude:
|
16
|
+
- 'db/seeds.rb'
|
17
|
+
|
18
|
+
Style/Documentation:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Style/GuardClause:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Style/IfUnlessModifier:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Layout/MultilineMethodCallIndentation:
|
28
|
+
EnforcedStyle: indented
|
29
|
+
|
30
|
+
Layout/MultilineOperationIndentation:
|
31
|
+
Enabled: true
|
32
|
+
EnforcedStyle: indented
|
33
|
+
|
34
|
+
Layout/FirstArrayElementIndentation:
|
35
|
+
EnforcedStyle: consistent
|
36
|
+
|
37
|
+
Naming/RescuedExceptionsVariableName:
|
38
|
+
PreferredName: 'exception'
|
39
|
+
|
40
|
+
Style/FormatStringToken:
|
41
|
+
EnforcedStyle: template
|
42
|
+
|
43
|
+
RSpec:
|
44
|
+
Exclude:
|
45
|
+
- 'spec/factories/**/*'
|
46
|
+
|
47
|
+
RSpec/ExampleLength:
|
48
|
+
Max: 10
|
49
|
+
|
50
|
+
RSpec/MultipleExpectations:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
AllCops:
|
54
|
+
Exclude:
|
55
|
+
- 'node_modules/**/*'
|
56
|
+
- 'script/**/*'
|
57
|
+
- 'log/**/*'
|
58
|
+
- 'docker/**/*'
|
59
|
+
- 'doc/**/*'
|
60
|
+
- 'bin/**/*'
|
61
|
+
- 'Rakefile'
|
62
|
+
- 'config.ru'
|
63
|
+
- 'config/**/*'
|
64
|
+
- 'db/schema.rb'
|
65
|
+
- 'node_modules/**/*'
|
66
|
+
- 'vendor/**/*'
|
data/.travis.yml
ADDED
data/CHANGELOG
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at marcelo.jacobus@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in koine-filesystem.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'coveralls', '~> 0.8.23'
|
9
|
+
gem 'rake', '~> 12.0'
|
10
|
+
gem 'reek'
|
11
|
+
gem 'rspec', '~> 3.0'
|
12
|
+
gem 'rubocop', '0.79.0'
|
13
|
+
gem 'rubocop-performance'
|
14
|
+
gem 'rubocop-rspec'
|
15
|
+
gem 'simplecov', '~> 0.16.1'
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Marcelo Jacobus
|
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,46 @@
|
|
1
|
+
# Koine::Filesystem
|
2
|
+
|
3
|
+
This will [hopefully] be a file system abstraction as good as the [PHP League's Flysystem](https://github.com/thephpleague/flysystem).
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/mjacobus/koine-file_system.svg?branch=master)](https://travis-ci.org/mjacobus/koine-file_system)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/github/mjacobus/koine-file_system/badge.svg?branch=master)](https://coveralls.io/github/mjacobus/koine-file_system?branch=master)
|
7
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/ae41e3facbadaabaa463/maintainability)](https://codeclimate.com/github/mjacobus/koine-file_system/maintainability)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'koine-filesystem'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install koine-filesystem
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO: Write usage instructions here
|
28
|
+
|
29
|
+
## Development
|
30
|
+
|
31
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
32
|
+
|
33
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mjacobus/koine-file_system. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mjacobus/koine-file_system/blob/master/CODE_OF_CONDUCT.md).
|
38
|
+
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
43
|
+
|
44
|
+
## Code of Conduct
|
45
|
+
|
46
|
+
Everyone interacting in the Koine::Filesystem project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mjacobus/koine-file_system/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "koine/filesystem"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/koine/filesystem/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'koine-filesystem'
|
7
|
+
spec.version = Koine::Filesystem::VERSION
|
8
|
+
spec.authors = ['Marcelo Jacobus']
|
9
|
+
spec.email = ['marcelo.jacobus@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'File System abstraction'
|
12
|
+
spec.description = 'File System abstraction'
|
13
|
+
spec.homepage = 'https://github.com/mjacobus/koine-file_system'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
|
+
|
17
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
18
|
+
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/mjacobus/koine-file_system'
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/mjacobus/koine-file_system/blob/CHANGELOG'
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'koine/filesystem/version'
|
4
|
+
require 'koine/filesystem/filesystem'
|
5
|
+
require 'koine/filesystem/path_sanitizer'
|
6
|
+
require 'koine/filesystem/adapters/base'
|
7
|
+
require 'koine/filesystem/adapters/local'
|
8
|
+
|
9
|
+
module Koine
|
10
|
+
module Filesystem
|
11
|
+
class Error < StandardError; end
|
12
|
+
class FileNotFound < Error; end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Koine
|
4
|
+
module Filesystem
|
5
|
+
module Adapters
|
6
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
7
|
+
class Base
|
8
|
+
# @param path [String] path
|
9
|
+
#
|
10
|
+
# @return string
|
11
|
+
#
|
12
|
+
# @raise [FileNotFound] when file does not exist or cannot be read
|
13
|
+
def read(path)
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param path [String] path
|
18
|
+
#
|
19
|
+
# @return Boolean
|
20
|
+
#
|
21
|
+
# @raise [FileNotFound] when file does not exist or cannot be read
|
22
|
+
def read_stream(path)
|
23
|
+
raise NotImplementedError
|
24
|
+
end
|
25
|
+
|
26
|
+
# @param path [String] path
|
27
|
+
# @param contents [String] the content
|
28
|
+
# @param options [<Hash>] the options
|
29
|
+
#
|
30
|
+
# @return [void]
|
31
|
+
def write(path, contents, options: {})
|
32
|
+
raise NotImplementedError
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param path [String] path
|
36
|
+
# @param contents [String] the content
|
37
|
+
# @param options [<Hash>] the options
|
38
|
+
#
|
39
|
+
# @return [void]
|
40
|
+
def write_stream(path, contents, options: {})
|
41
|
+
raise NotImplementedError
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param path [String] path
|
45
|
+
# @param contents [String] the new content
|
46
|
+
# @param options [<Hash>] the options
|
47
|
+
#
|
48
|
+
# @return [void]
|
49
|
+
#
|
50
|
+
# @raise [FileNotFound] when file does not exist or cannot be read
|
51
|
+
def update(path, contents, options: {})
|
52
|
+
raise NotImplementedError
|
53
|
+
end
|
54
|
+
|
55
|
+
# @param path [String] path
|
56
|
+
# @param contents [String] the new content
|
57
|
+
# @param options [<Hash>] the options
|
58
|
+
#
|
59
|
+
# @return [void]
|
60
|
+
#
|
61
|
+
# @raise [FileNotFound] when file does not exist or cannot be read
|
62
|
+
def update_stream(path, contents, options: {})
|
63
|
+
raise NotImplementedError
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param path [String] path
|
67
|
+
#
|
68
|
+
# @return Boolean
|
69
|
+
def has?(path)
|
70
|
+
raise NotImplementedError
|
71
|
+
end
|
72
|
+
|
73
|
+
# @param path [String] path
|
74
|
+
#
|
75
|
+
# @return [void]
|
76
|
+
#
|
77
|
+
# @raise [FileNotFound] when file does not exist or cannot be read
|
78
|
+
def delete(path)
|
79
|
+
raise NotImplementedError
|
80
|
+
end
|
81
|
+
|
82
|
+
# @param path [String] path
|
83
|
+
#
|
84
|
+
# @return [String]
|
85
|
+
#
|
86
|
+
# @raise [FileNotFound] when file does not exist or cannot be read
|
87
|
+
def read_and_delete(path)
|
88
|
+
raise NotImplementedError
|
89
|
+
end
|
90
|
+
|
91
|
+
# @param from [String] path
|
92
|
+
# @param to [String] path
|
93
|
+
#
|
94
|
+
# @return [void]
|
95
|
+
#
|
96
|
+
# @raise [FileNotFound] when source file does not exist or cannot be read
|
97
|
+
def rename(from, to)
|
98
|
+
raise NotImplementedError
|
99
|
+
end
|
100
|
+
|
101
|
+
# @param from [String] path
|
102
|
+
# @param to [String] path
|
103
|
+
#
|
104
|
+
# @return [void]
|
105
|
+
#
|
106
|
+
# @raise [FileNotFound] when source file does not exist or cannot be read
|
107
|
+
def copy(from, to)
|
108
|
+
raise NotImplementedError
|
109
|
+
end
|
110
|
+
|
111
|
+
# @param path [String] path
|
112
|
+
#
|
113
|
+
# @return [String]
|
114
|
+
#
|
115
|
+
# @raise [FileNotFound] when source file does not exist or cannot be read
|
116
|
+
def mime_type(path)
|
117
|
+
raise NotImplementedError
|
118
|
+
end
|
119
|
+
|
120
|
+
# @param path [String] path
|
121
|
+
#
|
122
|
+
# @return [Integer] Number of bytes
|
123
|
+
#
|
124
|
+
# @raise [FileNotFound] when file does not exist or cannot be read
|
125
|
+
def size(path)
|
126
|
+
raise NotImplementedError
|
127
|
+
end
|
128
|
+
|
129
|
+
# @param path [String] path
|
130
|
+
#
|
131
|
+
# @return [void]
|
132
|
+
def create_dir(path)
|
133
|
+
raise NotImplementedError
|
134
|
+
end
|
135
|
+
|
136
|
+
# @param path [String] path
|
137
|
+
#
|
138
|
+
# @return [void]
|
139
|
+
def delete_dir(path)
|
140
|
+
raise NotImplementedError
|
141
|
+
end
|
142
|
+
|
143
|
+
# @param path [String] path
|
144
|
+
# @param recursive [Bool] path
|
145
|
+
#
|
146
|
+
# @return [void]
|
147
|
+
def list(path, recursive: false)
|
148
|
+
raise NotImplementedError
|
149
|
+
end
|
150
|
+
|
151
|
+
private
|
152
|
+
|
153
|
+
def raise_not_found(file)
|
154
|
+
raise FileNotFound, "File not found: #{file}"
|
155
|
+
end
|
156
|
+
end
|
157
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module Koine
|
6
|
+
module Filesystem
|
7
|
+
module Adapters
|
8
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
9
|
+
class Local < Base
|
10
|
+
def initialize(root:, path_sanitizer: PathSanitizer.new)
|
11
|
+
@root = root
|
12
|
+
@path_sanitizer = path_sanitizer
|
13
|
+
end
|
14
|
+
|
15
|
+
def read(path)
|
16
|
+
if has?(path)
|
17
|
+
file = File.open(full_path(path), 'rb')
|
18
|
+
content = file.read
|
19
|
+
file.close
|
20
|
+
return content
|
21
|
+
end
|
22
|
+
raise_not_found(path)
|
23
|
+
end
|
24
|
+
|
25
|
+
def has?(path)
|
26
|
+
File.exist?(full_path(path))
|
27
|
+
end
|
28
|
+
|
29
|
+
def write(path, content, options: {})
|
30
|
+
path = full_path(path)
|
31
|
+
ensure_target_dir(path)
|
32
|
+
File.open(path, 'w') do |f|
|
33
|
+
f.write(content)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def list(dir = '', recursive: false)
|
38
|
+
Dir[create_list_pattern(dir, recursive)].map do |file|
|
39
|
+
metadata_for(file)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def size(path)
|
44
|
+
File.size(full_path(path))
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
# rubocop:disable Metrics/MethodLength
|
50
|
+
def metadata_for(file)
|
51
|
+
relative_path = file.sub("#{@root}/", '')
|
52
|
+
type = File.directory?(file) ? 'dir' : 'file'
|
53
|
+
filename = File.basename(file)
|
54
|
+
|
55
|
+
{
|
56
|
+
path: relative_path,
|
57
|
+
type: type,
|
58
|
+
extension: type == 'dir' ? nil : filename.split('.').last,
|
59
|
+
filename: filename,
|
60
|
+
dirname: File.dirname(relative_path),
|
61
|
+
timestamp: File.mtime(file),
|
62
|
+
size: size(relative_path)
|
63
|
+
}
|
64
|
+
end
|
65
|
+
# rubocop:enable Metrics/MethodLength
|
66
|
+
|
67
|
+
def full_path(path)
|
68
|
+
File.expand_path(sanitize_path(path), @root)
|
69
|
+
end
|
70
|
+
|
71
|
+
def ensure_target_dir(path)
|
72
|
+
dir = File.dirname(path)
|
73
|
+
unless Dir.exist?(dir)
|
74
|
+
FileUtils.mkdir_p(dir)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def sanitize_path(path)
|
79
|
+
@path_sanitizer.sanitize(path)
|
80
|
+
end
|
81
|
+
|
82
|
+
def create_list_pattern(dir, recursive)
|
83
|
+
dir = sanitize_path(dir)
|
84
|
+
parts = [@root]
|
85
|
+
|
86
|
+
unless dir.empty?
|
87
|
+
parts << dir
|
88
|
+
end
|
89
|
+
|
90
|
+
if recursive
|
91
|
+
parts << '**'
|
92
|
+
end
|
93
|
+
|
94
|
+
parts << '*'
|
95
|
+
parts.join('/')
|
96
|
+
end
|
97
|
+
end
|
98
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Koine
|
4
|
+
module Filesystem
|
5
|
+
# Inspired on
|
6
|
+
# https://flysystem.thephpleague.com/v1/docs/usage/filesystem-api/
|
7
|
+
class Filesystem
|
8
|
+
def initialize(adapter)
|
9
|
+
@adapter = adapter
|
10
|
+
end
|
11
|
+
|
12
|
+
def read(path, &block)
|
13
|
+
@adapter.read(path, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def read_stream(path, &block)
|
17
|
+
@adapter.read_stream(path, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def write(path, contents, options: {})
|
21
|
+
@adapter.write(path, contents, options: options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def write_stream(path, contents, options: {})
|
25
|
+
@adapter.write_stream(path, contents, options: options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def update(path, contents, options: {})
|
29
|
+
@adapter.update(path, contents, options: options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def update_stream(path, contents, options: {})
|
33
|
+
@adapter.update_stream(path, contents, options: options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def has?(path)
|
37
|
+
@adapter.has?(path)
|
38
|
+
end
|
39
|
+
|
40
|
+
def delete(path)
|
41
|
+
@adapter.delete(path)
|
42
|
+
end
|
43
|
+
|
44
|
+
def read_and_delete(path)
|
45
|
+
@adapter.read_and_delete(path)
|
46
|
+
end
|
47
|
+
|
48
|
+
def rename(from, to)
|
49
|
+
@adapter.rename(from, to)
|
50
|
+
end
|
51
|
+
|
52
|
+
def copy(from, to)
|
53
|
+
@adapter.copy(from, to)
|
54
|
+
end
|
55
|
+
|
56
|
+
def mime_type(path)
|
57
|
+
@adapter.mime_type(path)
|
58
|
+
end
|
59
|
+
|
60
|
+
def size(path)
|
61
|
+
@adapter.size(path)
|
62
|
+
end
|
63
|
+
|
64
|
+
def create_dir(path)
|
65
|
+
@adapter.create_dir(path)
|
66
|
+
end
|
67
|
+
|
68
|
+
def delete_dir(path)
|
69
|
+
@adapter.delete_dir(path)
|
70
|
+
end
|
71
|
+
|
72
|
+
def list(path, recursive: false)
|
73
|
+
@adapter.list(path, recursive: recursive)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: koine-filesystem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcelo Jacobus
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: File System abstraction
|
14
|
+
email:
|
15
|
+
- marcelo.jacobus@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- ".travis.yml"
|
24
|
+
- CHANGELOG
|
25
|
+
- CODE_OF_CONDUCT.md
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- koine-filesystem.gemspec
|
33
|
+
- lib/koine/filesystem.rb
|
34
|
+
- lib/koine/filesystem/adapters/base.rb
|
35
|
+
- lib/koine/filesystem/adapters/local.rb
|
36
|
+
- lib/koine/filesystem/filesystem.rb
|
37
|
+
- lib/koine/filesystem/path_sanitizer.rb
|
38
|
+
- lib/koine/filesystem/version.rb
|
39
|
+
homepage: https://github.com/mjacobus/koine-file_system
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata:
|
43
|
+
allowed_push_host: https://rubygems.org
|
44
|
+
homepage_uri: https://github.com/mjacobus/koine-file_system
|
45
|
+
source_code_uri: https://github.com/mjacobus/koine-file_system
|
46
|
+
changelog_uri: https://github.com/mjacobus/koine-file_system/blob/CHANGELOG
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.3.0
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.0.3
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: File System abstraction
|
66
|
+
test_files: []
|