Skip to content
Ivanq edited this page Oct 15, 2018 · 4 revisions

ZeroFS constructor(ZeroPage page)

Creates a new ZeroFS instance from ZeroPage object.

File read/write

Promise<String|Uint8Array> readFile(String file, "arraybuffer"|Boolean bytes=false, Boolean required=false)

Reads the file specified by the file argument. If it doesn't exist, rejects String(`File doesn't exist: ${file}`). If required is true, ZeroNet will try to find the file for 5 minutes, and if it still doesn't find it, reject.

If the file was found, the result depends on bytes.

  • false -> the file is decoded by UTF8 and the resulting string is returned
  • true -> the file is returned as byte string
  • "arraybuffer" -> a Uint8Array object is returned
let data;
try {
    data = JSON.parse(await zeroFS.readFile(`data/users/${address}/data.json`));
} catch(e) {
    data = {tableName: []};
}

Promise<String> writeFile(String file, String|Uint8Array content, "arraybuffer"|Boolean bytes=false)

The content field is checked by the same rules as in readFile method (depending on bytes). Returns filename in case the write was successful, rejects error object otherwise.

await zeroFS.writeFile(`data/users/${address}/data.json`, JSON.stringify(data));

Promise<String> deleteFile(String file)

Tries to delete the file passed. If the delete was successful, returns filename, otherwise rejects error object.

await zeroFS.deleteFile(`data/users/${address}/temp.txt`);

Promise<String|Uint8Array> peekFile(String file, Number offset, Number length, "arraybuffer"|Boolean bytes)

Uses AJAX to read the part of the file specified by offset and length. offset is counted from zero. bytes has the same meaning as in readFile. In case the read isn't successful (i.e. the return code isn't 206), rejects Number(status) (e.g. Number(404)).

Directories

Promise<Array<String>> readDirectory(String dir, Boolean recursive=false)

If the recursive argument is true, returns the full list of files inside dir. If the recursive argument is false, file and directory list is returned. Empty directories are not returned.

The paths exclude dir prefix. The result is sorted by filenames.

for(const fileName of await zeroFS.readDirectory("data/users")) {
    if(fileName !== "content.json") {
        console.log("New user found:", fileName);
    }
}

Other

Promise<"file"|"dir"> getType(String file)

Returns "file" if file is a file, "dir" for directories (including site root), and rejects String(`File doesn't exist: ${file}`) in case the file doesn't exist.

if((await zeroFS.getType("data/users")) !== "dir") {
    console.log("Invalid data/users format");
}

Promise<Boolean> fileExists(String file)

Returns whether the file (or directory) you pass exists. Calls fileList command, so it works fast for big files, but slow if you've got a lot of small files in the parent directory of the file. In case you're checking for existence of data/users/..., reading data/users/.../content.json and catching the exception will probably be faster.

if(!await zeroFS.fileExists(`data/users/${address}/content.json`)) {
    openRegisterDialog();
}
Clone this wiki locally