2020-05-09 14:45:23 +00:00
|
|
|
/* typehints:start */
|
|
|
|
|
import { Application } from "../application";
|
|
|
|
|
/* typehints:end */
|
|
|
|
|
|
|
|
|
|
export const FILE_NOT_FOUND = "file_not_found";
|
|
|
|
|
|
2024-06-19 22:12:08 +00:00
|
|
|
export class Storage {
|
2020-05-09 14:45:23 +00:00
|
|
|
constructor(app) {
|
|
|
|
|
/** @type {Application} */
|
|
|
|
|
this.app = app;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes the storage
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
|
|
|
|
initialize() {
|
2024-06-19 22:12:08 +00:00
|
|
|
return Promise.resolve();
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Writes a string to a file asynchronously
|
|
|
|
|
* @param {string} filename
|
|
|
|
|
* @param {string} contents
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
|
|
|
|
writeFileAsync(filename, contents) {
|
2024-06-19 22:12:08 +00:00
|
|
|
return ipcRenderer.invoke("fs-job", {
|
|
|
|
|
type: "write",
|
|
|
|
|
filename,
|
|
|
|
|
contents,
|
|
|
|
|
});
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads a string asynchronously. Returns Promise<FILE_NOT_FOUND> if file was not found.
|
|
|
|
|
* @param {string} filename
|
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
|
*/
|
|
|
|
|
readFileAsync(filename) {
|
2024-06-19 22:12:08 +00:00
|
|
|
return ipcRenderer
|
|
|
|
|
.invoke("fs-job", {
|
|
|
|
|
type: "read",
|
|
|
|
|
filename,
|
|
|
|
|
})
|
|
|
|
|
.then(res => {
|
|
|
|
|
if (res && res.error === FILE_NOT_FOUND) {
|
|
|
|
|
throw FILE_NOT_FOUND;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
});
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tries to delete a file
|
|
|
|
|
* @param {string} filename
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
|
|
|
|
deleteFileAsync(filename) {
|
2024-06-19 22:12:08 +00:00
|
|
|
return ipcRenderer.invoke("fs-job", {
|
|
|
|
|
type: "delete",
|
|
|
|
|
filename,
|
|
|
|
|
});
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
}
|