Class: RailsHttpLab::Storage::Filesystem

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_http_lab/storage/filesystem.rb

Overview

Filesystem-backed CRUD for Bruno collections under config.storage_path.

All paths are relative to the storage root. The class refuses to operate on absolute paths, paths containing “..”, or paths that escape the root after resolution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: RailsHttpLab.config.resolved_storage_path) ⇒ Filesystem

Returns a new instance of Filesystem.



13
14
15
# File 'lib/rails_http_lab/storage/filesystem.rb', line 13

def initialize(root: RailsHttpLab.config.resolved_storage_path)
  @root = Pathname.new(root.to_s)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



17
18
19
# File 'lib/rails_http_lab/storage/filesystem.rb', line 17

def root
  @root
end

Instance Method Details

#create_folder(relative_path, display_name: nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rails_http_lab/storage/filesystem.rb', line 66

def create_folder(relative_path, display_name: nil)
  abs = safe_path(relative_path)
  FileUtils.mkdir_p(abs)
  folder_meta = abs + "folder.bru"
  unless folder_meta.file?
    name = display_name || abs.basename.to_s
    doc = Bruno::Document.new(blocks: [
      Bruno::Block.new(name: "meta", mode: :kv, pairs: [
        ["name", name],
        ["seq",  next_seq(abs.dirname).to_s]
      ])
    ])
    File.write(folder_meta, Bruno.dump(doc))
  end
  relative_path
end

#delete(relative_path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rails_http_lab/storage/filesystem.rb', line 43

def delete(relative_path)
  abs = safe_path(relative_path)
  return false unless abs.exist?
  if abs.directory?
    FileUtils.rm_rf(abs)
  else
    abs.delete
  end
  true
end

#ensure_root!Object



23
24
25
26
27
28
# File 'lib/rails_http_lab/storage/filesystem.rb', line 23

def ensure_root!
  FileUtils.mkdir_p(@root) unless @root.directory?
  unless (@root + "bruno.json").file?
    File.write(@root + "bruno.json", default_bruno_json)
  end
end

#read_bru(relative_path) ⇒ Object

Raises:



30
31
32
33
34
# File 'lib/rails_http_lab/storage/filesystem.rb', line 30

def read_bru(relative_path)
  abs = safe_path(relative_path)
  raise NotFoundError, "no such file: #{relative_path}" unless abs.file?
  Bruno.parse(abs.read)
end

#read_bruno_jsonObject



83
84
85
86
87
# File 'lib/rails_http_lab/storage/filesystem.rb', line 83

def read_bruno_json
  path = @root + "bruno.json"
  return nil unless path.file?
  JSON.parse(path.read)
end

#rename(from, to) ⇒ Object

Moves a file or directory from one relative path to another. Both paths are validated through safe_path. Refuses if destination already exists.

Raises:



56
57
58
59
60
61
62
63
64
# File 'lib/rails_http_lab/storage/filesystem.rb', line 56

def rename(from, to)
  abs_from = safe_path(from)
  raise NotFoundError, "no such path: #{from}" unless abs_from.exist?
  abs_to = safe_path(to)
  raise Error, "destination exists: #{to}" if abs_to.exist?
  FileUtils.mkdir_p(abs_to.dirname)
  FileUtils.mv(abs_from.to_s, abs_to.to_s)
  abs_to
end

#root_exists?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rails_http_lab/storage/filesystem.rb', line 19

def root_exists?
  @root.directory?
end

#write_bru(relative_path, document) ⇒ Object



36
37
38
39
40
41
# File 'lib/rails_http_lab/storage/filesystem.rb', line 36

def write_bru(relative_path, document)
  abs = safe_path(relative_path)
  FileUtils.mkdir_p(abs.dirname)
  File.write(abs, Bruno.dump(document))
  document
end