Class: OpenSandbox::Execd::Files

Inherits:
Object
  • Object
show all
Defined in:
lib/open_sandbox/execd/files.rb

Defined Under Namespace

Classes: FileInfo, WriteEntry

Instance Method Summary collapse

Constructor Details

#initialize(execd) ⇒ Files

Returns a new instance of Files.



14
15
16
# File 'lib/open_sandbox/execd/files.rb', line 14

def initialize(execd)
  @execd = execd
end

Instance Method Details

#delete(paths) ⇒ Object

Delete files.



76
77
78
# File 'lib/open_sandbox/execd/files.rb', line 76

def delete(paths)
  @execd.proxy(method: :delete, path: "/files", body: { paths: paths })
end

#info(path) ⇒ Object

Get file metadata.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/open_sandbox/execd/files.rb', line 60

def info(path)
  response = @execd.proxy(
    method: :get,
    path: "/files/info?path=#{escape(path)}"
  )
  data = JSON.parse(response.body)
  FileInfo.new(
    path: data["path"],
    size: data["size"],
    mode: data["mode"],
    is_dir: data["isDir"],
    mod_time: data["modTime"]
  )
end

#mkdir(paths, mode: 755) ⇒ Object

Create directories (mkdir -p semantics).



81
82
83
84
# File 'lib/open_sandbox/execd/files.rb', line 81

def mkdir(paths, mode: 755)
  body = paths.to_h { |path| [ path, { mode: mode } ] }
  @execd.proxy(method: :post, path: "/directories", body: body)
end

#read_file(path) ⇒ Object

Read a file from the sandbox.



32
33
34
35
36
37
38
# File 'lib/open_sandbox/execd/files.rb', line 32

def read_file(path)
  response = @execd.proxy(
    method: :get,
    path: "/files/download?path=#{escape(path)}"
  )
  response.body
end

#search(path:, pattern: "*") ⇒ Object

Search for files matching a pattern.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/open_sandbox/execd/files.rb', line 41

def search(path:, pattern: "*")
  response = @execd.proxy(
    method: :get,
    path: "/files/search?path=#{escape(path)}&pattern=#{escape(pattern)}"
  )
  data = JSON.parse(response.body)
  files = data.is_a?(Array) ? data : (data["files"] || [])
  files.map do |f|
    FileInfo.new(
      path: f["path"],
      size: f["size"],
      mode: f["mode"],
      is_dir: f["isDir"],
      mod_time: f["modTime"]
    )
  end
end

#write_files(entries) ⇒ Object

Write one or more files into the sandbox.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/open_sandbox/execd/files.rb', line 19

def write_files(entries)
  boundary = "----OpenSandboxRuby#{SecureRandom.hex(12)}"
  body = build_multipart_body(entries, boundary)
  response = @execd.proxy(
    method: :post,
    path: "/files/upload",
    raw_body: body,
    headers: { "Content-Type" => "multipart/form-data; boundary=#{boundary}" }
  )
  response.code >= 200 && response.code < 300
end