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);
|
|
|
|
}
|
2023-10-12 21:32:38 +02:00
|
|
|
if (!FS.analyzePath(config.fs.idbfsAudioDir).exists) {
|
|
|
|
FS.mkdir(config.fs.idbfsAudioDir);
|
|
|
|
}
|
2023-09-24 18:39:52 +02:00
|
|
|
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);
|
|
|
|
});
|
2023-10-12 21:32:38 +02:00
|
|
|
} else if (file.type.indexOf('audio') === 0) {
|
|
|
|
var uint8View = new Uint8Array(file.arrayBuffer);
|
|
|
|
console.log('trying to save the audio file, file, uint8View', file, uint8View);
|
|
|
|
if (!FS.analyzePath(`${config.fs.idbfsAudioDir}/${file.name}`).exists) {
|
|
|
|
FS.createDataFile(config.fs.idbfsAudioDir, file.name, uint8View, true, true);
|
|
|
|
this.syncfs(MODE_WRITE_TO_PERSISTENT)
|
|
|
|
.then(() => {
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
alert(`It seems as if an audiofile with the name "${file.name}" already exists. Please rename your file and upload again, thanks <3`);
|
|
|
|
resolve(false);
|
|
|
|
}
|
2023-09-24 18:39:52 +02:00
|
|
|
} 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
|
|
|
|
};
|