2021-04-11 01:09:09 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
import { invokeTauriCommand } from './helpers/tauri'
|
2020-07-13 01:34:44 +03:00
|
|
|
|
|
|
|
export enum BaseDirectory {
|
|
|
|
Audio = 1,
|
|
|
|
Cache,
|
|
|
|
Config,
|
|
|
|
Data,
|
|
|
|
LocalData,
|
|
|
|
Desktop,
|
|
|
|
Document,
|
|
|
|
Download,
|
|
|
|
Executable,
|
|
|
|
Font,
|
|
|
|
Home,
|
|
|
|
Picture,
|
|
|
|
Public,
|
|
|
|
Runtime,
|
|
|
|
Template,
|
|
|
|
Video,
|
|
|
|
Resource,
|
2021-03-23 04:13:12 +03:00
|
|
|
App,
|
|
|
|
Current
|
2020-07-13 01:34:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface FsOptions {
|
|
|
|
dir?: BaseDirectory
|
|
|
|
}
|
|
|
|
|
2021-01-12 10:16:45 +03:00
|
|
|
export interface FsDirOptions {
|
|
|
|
dir?: BaseDirectory
|
|
|
|
recursive?: boolean
|
|
|
|
}
|
|
|
|
|
2020-07-13 01:34:44 +03:00
|
|
|
export interface FsTextFileOption {
|
|
|
|
path: string
|
|
|
|
contents: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FsBinaryFileOption {
|
|
|
|
path: string
|
|
|
|
contents: ArrayBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FileEntry {
|
|
|
|
path: string
|
2021-04-13 04:44:50 +03:00
|
|
|
/**
|
|
|
|
* Name of the directory/file
|
|
|
|
* can be null if the path terminates with `..`
|
|
|
|
*/
|
2020-07-13 01:34:44 +03:00
|
|
|
name?: string
|
2021-04-13 04:44:50 +03:00
|
|
|
/** Children of this entry if it's a directory; null otherwise */
|
2020-07-13 01:34:44 +03:00
|
|
|
children?: FileEntry[]
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Reads a file as text.
|
|
|
|
*
|
|
|
|
* @param filePath Path to the file
|
|
|
|
* @param [options]
|
|
|
|
* @returns A promise resolving to a string of the file content.
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-08 03:56:29 +03:00
|
|
|
async function readTextFile(
|
|
|
|
filePath: string,
|
|
|
|
options: FsOptions = {}
|
|
|
|
): Promise<string> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand<string>({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'readTextFile',
|
|
|
|
path: filePath,
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Reads a file as binary.
|
|
|
|
*
|
|
|
|
* @param filePath Path to the file
|
|
|
|
* @param [options]
|
|
|
|
* @returns A promise resolving to an array of the file bytes.
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-08 03:56:29 +03:00
|
|
|
async function readBinaryFile(
|
|
|
|
filePath: string,
|
|
|
|
options: FsOptions = {}
|
|
|
|
): Promise<number[]> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand<number[]>({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'readBinaryFile',
|
|
|
|
path: filePath,
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Writes a text file.
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
2021-04-13 04:44:50 +03:00
|
|
|
* @param file File configuration object
|
|
|
|
* @param [options]
|
|
|
|
* @returns
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function writeFile(
|
|
|
|
file: FsTextFileOption,
|
|
|
|
options: FsOptions = {}
|
|
|
|
): Promise<void> {
|
2020-06-27 18:20:00 +03:00
|
|
|
if (typeof options === 'object') {
|
|
|
|
Object.freeze(options)
|
|
|
|
}
|
|
|
|
if (typeof file === 'object') {
|
|
|
|
Object.freeze(file)
|
|
|
|
}
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'writeFile',
|
|
|
|
path: file.path,
|
|
|
|
contents: file.contents,
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const CHUNK_SIZE = 65536
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Convert an Uint8Array to ascii string.
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
|
|
|
* @param arr
|
2021-04-13 04:44:50 +03:00
|
|
|
* @returns An ASCII string.
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
|
|
|
function uint8ArrayToString(arr: Uint8Array): string {
|
|
|
|
if (arr.length < CHUNK_SIZE) {
|
|
|
|
return String.fromCharCode.apply(null, Array.from(arr))
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = ''
|
|
|
|
const arrLen = arr.length
|
|
|
|
for (let i = 0; i < arrLen; i++) {
|
|
|
|
const chunk = arr.subarray(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE)
|
|
|
|
result += String.fromCharCode.apply(null, Array.from(chunk))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Convert an ArrayBuffer to base64 encoded string.
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
|
|
|
* @param buffer
|
2021-04-13 04:44:50 +03:00
|
|
|
* @returns A base64 encoded string.
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
|
|
|
function arrayBufferToBase64(buffer: ArrayBuffer): string {
|
|
|
|
const str = uint8ArrayToString(new Uint8Array(buffer))
|
|
|
|
return btoa(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Writes a binary file
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
2021-04-13 04:44:50 +03:00
|
|
|
* @param file File configuration object
|
|
|
|
* @param [options]
|
|
|
|
* @returns
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function writeBinaryFile(
|
|
|
|
file: FsBinaryFileOption,
|
|
|
|
options: FsOptions = {}
|
|
|
|
): Promise<void> {
|
2020-06-27 18:20:00 +03:00
|
|
|
if (typeof options === 'object') {
|
|
|
|
Object.freeze(options)
|
|
|
|
}
|
|
|
|
if (typeof file === 'object') {
|
|
|
|
Object.freeze(file)
|
|
|
|
}
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'writeBinaryFile',
|
|
|
|
path: file.path,
|
|
|
|
contents: arrayBufferToBase64(file.contents),
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* List directory files.
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
2021-04-13 04:44:50 +03:00
|
|
|
* @param dir Path to the directory to read
|
|
|
|
* @param [options] Configuration object
|
|
|
|
* @returns
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function readDir(
|
|
|
|
dir: string,
|
2021-01-12 10:16:45 +03:00
|
|
|
options: FsDirOptions = {}
|
2020-08-19 05:36:46 +03:00
|
|
|
): Promise<FileEntry[]> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'readDir',
|
|
|
|
path: dir,
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Creates a directory.
|
2020-06-27 18:20:00 +03:00
|
|
|
* If one of the path's parent components doesn't exist
|
2021-04-13 04:44:50 +03:00
|
|
|
* and the `recursive` option isn't set to true, it will be rejected.
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
2021-04-13 04:44:50 +03:00
|
|
|
* @param dir Path to the directory to create
|
|
|
|
* @param [options] Configuration object
|
|
|
|
* @returns
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2021-01-12 10:16:45 +03:00
|
|
|
async function createDir(
|
|
|
|
dir: string,
|
|
|
|
options: FsDirOptions = {}
|
|
|
|
): Promise<void> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'createDir',
|
|
|
|
path: dir,
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Removes a directory.
|
|
|
|
* If the directory is not empty and the `recursive` option isn't set to true, it will be rejected.
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
2021-04-13 04:44:50 +03:00
|
|
|
* @param dir Path to the directory to remove
|
|
|
|
* @param [options] Configuration object
|
|
|
|
* @returns
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2021-01-12 10:16:45 +03:00
|
|
|
async function removeDir(
|
|
|
|
dir: string,
|
|
|
|
options: FsDirOptions = {}
|
|
|
|
): Promise<void> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'removeDir',
|
|
|
|
path: dir,
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Copys a file to a destination.
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
2021-04-13 04:44:50 +03:00
|
|
|
* @param source A path of the file to copy
|
|
|
|
* @param destination A path for the destination file
|
|
|
|
* @param [options] Configuration object
|
|
|
|
* @returns
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function copyFile(
|
|
|
|
source: string,
|
|
|
|
destination: string,
|
|
|
|
options: FsOptions = {}
|
|
|
|
): Promise<void> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'copyFile',
|
|
|
|
source,
|
|
|
|
destination,
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Removes a file.
|
2020-06-27 18:20:00 +03:00
|
|
|
*
|
2021-04-13 04:44:50 +03:00
|
|
|
* @param file Path to the file to remove
|
|
|
|
* @param [options] Configuration object
|
|
|
|
* @returns
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function removeFile(
|
|
|
|
file: string,
|
|
|
|
options: FsOptions = {}
|
|
|
|
): Promise<void> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'removeFile',
|
|
|
|
path: file,
|
|
|
|
options: options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renames a file
|
|
|
|
*
|
2021-04-13 04:44:50 +03:00
|
|
|
* @param oldPath A path of the file to rename
|
|
|
|
* @param newPath A path of the new file name
|
|
|
|
* @param [options] Configuration object
|
|
|
|
* @returns
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function renameFile(
|
|
|
|
oldPath: string,
|
|
|
|
newPath: string,
|
|
|
|
options: FsOptions = {}
|
|
|
|
): Promise<void> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Fs',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'renameFile',
|
|
|
|
oldPath,
|
|
|
|
newPath,
|
|
|
|
options
|
|
|
|
}
|
2020-06-27 18:20:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
BaseDirectory as Dir,
|
|
|
|
readTextFile,
|
|
|
|
readBinaryFile,
|
|
|
|
writeFile,
|
|
|
|
writeBinaryFile,
|
|
|
|
readDir,
|
|
|
|
createDir,
|
|
|
|
removeDir,
|
|
|
|
copyFile,
|
|
|
|
removeFile,
|
|
|
|
renameFile
|
|
|
|
}
|