variabletime/bin/web/js/moduleFS.js

89 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-09-24 18:39:52 +02:00
const ModuleFS = function() {
const MODE_WRITE_TO_PERSISTENT = false;
const MODE_READ_FROM_PERSISTENT = true;
this.init = () => {
return new Promise((resolve) => {
FS.mkdir(config.fs.idbfsDir);
// Then mount with IDBFS type
FS.mount(IDBFS, {}, config.fs.idbfsDir);
this.syncfs(MODE_READ_FROM_PERSISTENT)
.then(() => {
// Then sync with true to get persistent data
if (!FS.analyzePath(config.fs.idbfsFontDir).exists) {
FS.mkdir(config.fs.idbfsFontDir);
}
if (!FS.analyzePath(config.fs.idbfsTmpDir).exists) {
FS.mkdir(config.fs.idbfsTmpDir);
}
resolve();
});
});
};
this.syncfs = (mode = MODE_READ_FROM_PERSISTENT) => {
return new Promise((resolve, reject) => {
FS.syncfs(mode, function(err) {
if (err !== null) {
// Error
console.error(err);
reject(error);
} else {
resolve();
}
});
});
};
// check utils::uploadFile() for details of file
this.save = (file) => {
return new Promise((resolve) => {
if (file.type.indexOf('font') >= 0 || file.hasOwnProperty('isFont') && file.isFont === true) {
var uint8View = new Uint8Array(file.arrayBuffer);
console.log('trying to save the font file, file, uint8View', file, uint8View);
if (!FS.analyzePath(`${config.fs.idbfsFontDir}/${file.name}`).exists) {
FS.createDataFile(config.fs.idbfsFontDir, file.name, uint8View, true, true);
}
this.syncfs(MODE_WRITE_TO_PERSISTENT)
.then(() => {
resolve(true);
});
} else if (file.type.indexOf('zip') >= 0 || file.hasOwnProperty('isZip') && file.isZip === true) {
var uint8View = new Uint8Array(file.arrayBuffer);
var filePath = `${config.fs.idbfsTmpDir}/${file.name}`;
console.log(filePath);
if (!FS.analyzePath(filePath).exists) {
FS.createDataFile(config.fs.idbfsTmpDir, file.name, uint8View, true, true);
}
this.syncfs(MODE_WRITE_TO_PERSISTENT)
.then(() => {
resolve(filePath);
});
} else {
resolve(false);
}
});
};
this.delete = (file) => {
if (file.type.indexOf('zip') >= 0 || file.hasOwnProperty('isZip') && file.isZip === true) {
var filePath = `${config.fs.idbfsTmpDir}/${file.name}`;
if (!FS.analyzePath(filePath).exists) {
console.log(`moduleFS::delete(${filePath})`, `file does not exist`);
} else {
FS.unlink(filePath);
}
this.syncfs(MODE_WRITE_TO_PERSISTENT)
.then(() => {
resolve(true);
});
} else {
resolve(false);
}
};
};
export {
ModuleFS
};