Class: Indexmap::Storage::Filesystem
- Inherits:
-
Object
- Object
- Indexmap::Storage::Filesystem
- Defined in:
- lib/indexmap/storage/filesystem.rb
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
"application/xml"
Instance Method Summary collapse
- #delete(filename) ⇒ Object
- #exist?(filename) ⇒ Boolean
-
#initialize(path:, public_url: nil) ⇒ Filesystem
constructor
A new instance of Filesystem.
- #inspect ⇒ Object
- #list(prefix: nil, suffix: nil) ⇒ Object
- #public_url(filename) ⇒ Object
- #read(filename) ⇒ Object
- #write(filename, body, content_type: DEFAULT_CONTENT_TYPE) ⇒ Object
Constructor Details
#initialize(path:, public_url: nil) ⇒ Filesystem
Returns a new instance of Filesystem.
10 11 12 13 |
# File 'lib/indexmap/storage/filesystem.rb', line 10 def initialize(path:, public_url: nil) @path = Pathname(path) @public_url_base = public_url end |
Instance Method Details
#delete(filename) ⇒ Object
45 46 47 |
# File 'lib/indexmap/storage/filesystem.rb', line 45 def delete(filename) path_for(filename).delete if exist?(filename) end |
#exist?(filename) ⇒ Boolean
31 32 33 |
# File 'lib/indexmap/storage/filesystem.rb', line 31 def exist?(filename) path_for(filename).file? end |
#inspect ⇒ Object
55 56 57 |
# File 'lib/indexmap/storage/filesystem.rb', line 55 def inspect "#<#{self.class.name} path=#{path}>" end |
#list(prefix: nil, suffix: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/indexmap/storage/filesystem.rb', line 35 def list(prefix: nil, suffix: nil) path.glob("**/*").select(&:file?).filter_map do |file| filename = file.relative_path_from(path).to_s next if prefix && !filename.start_with?(prefix) next if suffix && !filename.end_with?(suffix) filename end.sort end |
#public_url(filename) ⇒ Object
49 50 51 52 53 |
# File 'lib/indexmap/storage/filesystem.rb', line 49 def public_url(filename) return normalize_filename(filename) if public_url_base.to_s.strip.empty? URI.join("#{public_url_base.to_s.delete_suffix("/")}/", normalize_filename(filename)).to_s end |
#read(filename) ⇒ Object
27 28 29 |
# File 'lib/indexmap/storage/filesystem.rb', line 27 def read(filename) path_for(filename).read(encoding: "UTF-8") end |
#write(filename, body, content_type: DEFAULT_CONTENT_TYPE) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/indexmap/storage/filesystem.rb', line 15 def write(filename, body, content_type: DEFAULT_CONTENT_TYPE) target = path_for(filename) target.dirname.mkpath target.write(body.to_s) File.new( filename: normalize_filename(filename), body: body.to_s, content_type: content_type || DEFAULT_CONTENT_TYPE ) end |