File system operations for reading, writing, and managing files.
Functions
delete-file core
fn (path: Str): Bool
Delete a file and return true if successful.
Example
success delete-file("temp.txt")
// true
file-exists core
fn (path: Str): Bool
Check if a file exists.
Example
file-exists("config.json") // true or false
file-info core
fn (path: Str): FileMeta
Get metadata information about a file.
Example
info file-info("data.json")
// info.size = 1024
// info.content-type = "application/json"
list-files core
fn (prefix: Str): Vec<FileMeta>
List all files matching a prefix path. Each file comes as a FileMeta.
Example
files list-files("uploads/")
map(files, %.path)
// ["uploads/image1.png", "uploads/image2.jpg", ...]
read-file core
fn (path: Str): Str
Read the contents of a file and return them as a string.
Example
contents read-file("config.json")
// "{\"debug\": true, \"port\": 8080}"
read-file-bytes core
fn (path: Str): Bytes
Read the contents of a file and return them as bytes.
Example
data read-file-bytes("image.png")
// [137, 80, 78, 71, ...]
read-file-stream core
fn (path: Str): Iter
fn (path: Str, opts: Map): Iter
Open a file as an iterator of Bytes chunks, without reading it into
memory. The file stays open until the iterator is exhausted or dropped.
Chunks can split anywhere, including inside a character. Pass chunk-size,
in bytes, to change the default of 64 KB.
Example
size reduce(read-file-stream("export.bin"), (total, chunk) {
add(total, length(chunk))
}, 0)
// Rows of a CSV file too big to load
for-each(iter-csv(read-file-stream("customers.csv")), (row) { print(row.id) })
read-file-stream("export.bin", {chunk-size: 1048576})
write-file core
fn (path: Str, contents: Str): FileMeta
Write a string to a file and return file metadata.
Example
meta write-file("output.txt", "Hello, World!")
// meta.path = "output.txt"
// meta.size = 13
write-file-bytes core
fn (path: Str, contents: Bytes): FileMeta
Write bytes to a file and return file metadata.
Example
data [72, 101, 108, 108, 111] // "Hello" as bytes
meta write-file-bytes("binary.dat", data)
Types
FileMeta
FileMeta type {
file-id: Str,
path: Str,
size: Int,
etag: Str?,
content-type: Str?,
storage-backend: Str,
created-at: Int,
updated-at: Int
}
Metadata about a stored file.
Example
meta file-meta("uploads/image.png")
meta.path // "uploads/image.png"
meta.size // 1024
meta.content-type // "image/png"