Working with Files
Structure
Files & folders in win32.run shared the same structure. I'm not good at naming, let's call them fs_item
To get a fs_item from id
import { hardDrive } from '$lib/store.js';
let id = '1Ggc8cPyxmAqp5tqKdo3s9';
let fs_item = $hardDrive[id];Each fs_item is identified with an id. Here's the general structure of a fs_item
{
id: String,
//used to identify a fs_item
basename: String,
//name of the item without extension
ext: String,
//extension. e.g. .jpg, .png
name: String,
// = basename + ext. :) now I'm ashamed at this redundancy
size: Number,
//item size, in Kilobytes
date_created: Number,
//timestamp of the Date when item was first created,
date_modified: Number,
//timestamp of the Date when item was last modified,
storage_type: String,
//possible values include 'local' and 'remote'
//local: the content of the file is stored locally in IndexedDB as File
//remote: the content is on a web address (https or http)
url: String,
//if the storage_type is local, url is a id point to a File object
//in IndexedDB
//if the storage_type is remote, url is the web address of the file
parent: String,
//id of the parent folder (the folder that contains this fs_item)
children: [String],
//list of its children fs_items' ids
//if it's a file, then children is a empty array
}Operations
It's recommended that you use $lib/fs.js instead of working with files manually.
When I wrote this fs.js file, I didn't intend for its functions to be used by another human being, so it's pretty messed up there, without any code comments. Shame on me.
Create
This function creates a new fs_item (can create both file and folder), returns a promise that resolves with the newly created fs_item id.
type: String: can be 'file' to create a new file, or 'folder' to create a new folderext: String:thefs_itemextension, start with a dot. e.g: .jpg, .png. Leave an empty string''iftype == 'folder'seedname: String:suggested file name. e.g: 'nevada desert'.parent_id: String:id of the parent folder where the newly created item will be located.file: File:the actually file content. See Mozilla Docs
Copy
To actually copy a fs_item, use fs.clone_fs()
item_id: String: the fs_item id that you want to copynew_parent_id: String: id of the destination directory (folder)new_item_id: String: provide the newly created item an id so you can refer to it later. Sorry for this inconvenience.
To generate an id:
Note: The fs.copy, fs.cut functions are used to put an item into the clipboard. These functions don't actually copy or cut an item.
Delete
To permanently delete a fs_item
Save
To save content to a fs_item
In which file is the new content as a File object. See Mozilla Docs
Example:
Read
To read content of a fs_item, use fs.get_file(item_id). This function returns a promise that resolves with a File object. See Mozilla Docs.
fs_item's id to human-readable path
If you have a fs_item's id (e.g. 1Ggc8cPyxmAqp5tqKdo3s9) and want to convert it into a human-readable path like what is being displayed in My Computer D:\webpages\any_name.html
human-readable path to fs_item id
If you have path (for example D:\webpages\any_name.html ) and want to get the fs_item id out of it
Last updated