KScript docs
EN

App folders: KScript.Storage

Reading and writing files

A folder has methods for the files in it. A file's name is its path inside the folder, with / between folders on every platform. A name that would leave the folder, such as ../other or an absolute path, is refused.

Reading files

->read() gives a file's text. When the file isn't there, you get null, or the default you give:

<import package="KScript.Storage" />

<let name="notes" value="@storage(data)->read('notes.txt', default: '')" />
<echo value="$notes" />
import KScript.Storage

$notes = @storage(data)->read('notes.txt', default: '')
echo $notes

->read_json() reads a file as JSON. ->exists() tells you whether a file or folder is there, and with no name, whether the folder itself exists yet:

<import package="KScript.Storage" />

<let name="recent" value="@storage(state)->read_json('recent.json', default: [])" />
<if not @storage(state)->exists('recent.json')>
  <echo>Nothing opened yet</echo>
</if>
<echo value="$recent" />
import KScript.Storage

$recent = @storage(state)->read_json('recent.json', default: [])
if not @storage(state)->exists('recent.json') => echo 'Nothing opened yet'
echo $recent

Reading typed settings

Give ->read_json() a type with as:, and it checks the file's shape and turns its objects into records of that type. A missing file gives you the default exactly as you wrote it, so make the default a record of the same type:

<import package="KScript.Storage" />

<type name="Settings">
  <field name="theme" type="string" />
</type>

<let name="settings" value="@storage(config)->read_json('settings.json', default: Settings({ theme: 'light' }), as: Settings)" />
<echo value="$settings.theme" />
import KScript.Storage

type Settings {
  theme: string
}

$settings = @storage(config)->read_json('settings.json', default: Settings({ theme: 'light' }), as: Settings)
echo $settings.theme

Writing files

->write() writes text to a file, and ->write_json() writes a value as indented JSON. Both write beside the old file and then move the new one into place, so a crash never leaves half a file. ->append() adds text to the end of a file:

<import package="KScript.Storage" />

<do>@storage(data)->write('notes/today.txt', 'Buy milk')</do>
<do>@storage(data)->write_json('counter.json', { count: 1 })</do>
<do>@storage(logs)->append('app.log', 'Started\n')</do>
import KScript.Storage

@storage(data)->write('notes/today.txt', 'Buy milk')
@storage(data)->write_json('counter.json', { count: 1 })
@storage(logs)->append('app.log', 'Started\n')

Binary files, moving and copying

->read_bytes() and ->write_bytes() read and write any file, an image say, as a base64 value. ->file() gives a file in the folder as a file value, with .size, .modified, .mime and the file methods. ->move() and ->copy() keep a file inside the folder, and overwrite replaces what is already there:

<import package="KScript.Storage" />

<let name="data" value="@storage(data)" />
<do>$data->write_bytes('avatars/ann.png', $upload.bytes)</do>
<echo>{ $data->file('avatars/ann.png').size } bytes</echo>
<do>$data->copy('avatars/ann.png', 'avatars/ann-original.png')</do>
<do>$data->move('avatars/ann.png', 'avatars/1.png', overwrite)</do>
import KScript.Storage

$data = @storage(data)
$data->write_bytes('avatars/ann.png', $upload.bytes)
echo "{ $data->file('avatars/ann.png').size } bytes"
$data->copy('avatars/ann.png', 'avatars/ann-original.png')
$data->move('avatars/ann.png', 'avatars/1.png', overwrite)

This is how a route keeps an uploaded file.

Listing, deleting and clearing

->list() gives the names of the files, sorted, such as ['notes/a.txt', 'settings.json']. Give it a wildcard such as '*.json' to list only some, and recursive to look inside folders too. ->delete() deletes a file, or a folder and everything in it, and returns true if something was deleted. ->clear() deletes everything in the folder and returns how many files there were:

<import package="KScript.Storage" />

<echo value="@storage(data)->list(recursive)" />
<do>@storage(cache)->delete('thumbnails')</do>
<echo>Cleared { @storage(cache)->clear() } files</echo>
import KScript.Storage

echo @storage(data)->list(recursive)
@storage(cache)->delete('thumbnails')
echo "Cleared { @storage(cache)->clear() } files"

Folders inside folders

->folder() gives a folder inside this one, with the same methods:

<import package="KScript.Storage" />

<let name="drafts" value="@storage(data)->folder('drafts')" />
<do>$drafts->write('letter.md', '# Dear Joan')</do>
<for each="name" in="$drafts->list('*.md')">
  <echo>{ $name }: { $drafts->read($name)->count() } characters</echo>
</for>
<do>$drafts->delete('letter.md')</do>
import KScript.Storage

$drafts = @storage(data)->folder('drafts')
$drafts->write('letter.md', '# Dear Joan')
for name in $drafts->list('*.md') {
  echo "{ $name }: { $drafts->read($name)->count() } characters"
}
$drafts->delete('letter.md')

A folder also has the fields .app, .kind and .path (its own full path), and it prints as its path. When you need more than these methods, ->path_of() gives a file's full path to pass to KScript.IO.