2024-04-09 08:04:36 -04:00
|
|
|
import { readFile, exists as tauriExists } from '@tauri-apps/plugin-fs'
|
2024-02-12 12:18:37 -08:00
|
|
|
import { isTauri } from 'lib/isTauri'
|
|
|
|
import { join } from '@tauri-apps/api/path'
|
2024-04-25 00:13:09 -07:00
|
|
|
import { readDirRecursive } from 'lib/tauri'
|
2024-02-12 12:18:37 -08:00
|
|
|
|
|
|
|
/// FileSystemManager is a class that provides a way to read files from the local file system.
|
|
|
|
/// It assumes that you are in a project since it is solely used by the std lib
|
|
|
|
/// when executing code.
|
|
|
|
class FileSystemManager {
|
|
|
|
private _dir: string | null = null
|
|
|
|
|
|
|
|
get dir() {
|
2024-06-24 11:45:40 -04:00
|
|
|
return this._dir ?? ''
|
2024-02-12 12:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
set dir(dir: string) {
|
|
|
|
this._dir = dir
|
|
|
|
}
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
async readFile(path: string): Promise<Uint8Array | void> {
|
2024-02-12 12:18:37 -08:00
|
|
|
// Using local file system only works from Tauri.
|
|
|
|
if (!isTauri()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
|
|
|
new Error('This function can only be called from a Tauri application')
|
2024-02-12 12:18:37 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(this.dir, path)
|
|
|
|
.catch((error) => {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error reading file: ${error}`))
|
2024-02-12 12:18:37 -08:00
|
|
|
})
|
|
|
|
.then((file) => {
|
2024-04-09 08:04:36 -04:00
|
|
|
return readFile(file)
|
2024-02-12 12:18:37 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
async exists(path: string): Promise<boolean | void> {
|
2024-02-12 12:18:37 -08:00
|
|
|
// Using local file system only works from Tauri.
|
|
|
|
if (!isTauri()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
|
|
|
new Error('This function can only be called from a Tauri application')
|
2024-02-12 12:18:37 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(this.dir, path)
|
|
|
|
.catch((error) => {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error checking file exists: ${error}`))
|
2024-02-12 12:18:37 -08:00
|
|
|
})
|
|
|
|
.then((file) => {
|
|
|
|
return tauriExists(file)
|
|
|
|
})
|
|
|
|
}
|
2024-02-19 12:33:16 -08:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
async getAllFiles(path: string): Promise<string[] | void> {
|
2024-02-19 12:33:16 -08:00
|
|
|
// Using local file system only works from Tauri.
|
|
|
|
if (!isTauri()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
|
|
|
new Error('This function can only be called from a Tauri application')
|
2024-02-19 12:33:16 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(this.dir, path)
|
|
|
|
.catch((error) => {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error joining dir: ${error}`))
|
2024-02-19 12:33:16 -08:00
|
|
|
})
|
|
|
|
.then((p) => {
|
2024-04-25 00:13:09 -07:00
|
|
|
readDirRecursive(p)
|
2024-02-19 12:33:16 -08:00
|
|
|
.catch((error) => {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error reading dir: ${error}`))
|
2024-02-19 12:33:16 -08:00
|
|
|
})
|
|
|
|
.then((files) => {
|
|
|
|
return files.map((file) => file.path)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2024-02-12 12:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const fileSystemManager = new FileSystemManager()
|