allow to create memory-based temporal VFS file system

This commit is contained in:
DanyLE 2022-07-02 18:52:50 +02:00
parent 81d78aa8e5
commit fdcc5ce784
5 changed files with 6577 additions and 6457 deletions

12906
d.ts/antos.d.ts vendored

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -73,6 +73,7 @@ namespace OS {
/** /**
* Loading animation check timeout * Loading animation check timeout
* *
* @private
* @memberof BaseApplication * @memberof BaseApplication
*/ */
private _loading_toh: any; private _loading_toh: any;

View File

@ -171,6 +171,13 @@ interface Date {
* @memberof Date * @memberof Date
*/ */
timestamp(): number; timestamp(): number;
/**
* Covnert to GMTString
*
* @returns {number}
* @memberof Date
*/
toGMTString(): string;
} }
/** /**

View File

@ -272,11 +272,12 @@ namespace OS {
/** /**
* Once read, file content will be cached in this placeholder * Once read, file content will be cached in this placeholder
* *
* @private
* @type {*} * @type {*}
* @memberof BaseFileHandle * @memberof BaseFileHandle
*/ */
cache: any; private _cache: any;
/** /**
* Flag indicated whether the file meta-data is loaded * Flag indicated whether the file meta-data is loaded
@ -355,6 +356,8 @@ namespace OS {
this.setPath(path); this.setPath(path);
} }
/** /**
* Set a file path to the current file handle * Set a file path to the current file handle
* *
@ -408,6 +411,22 @@ namespace OS {
set filename(v: string) { set filename(v: string) {
this.basename = v; this.basename = v;
} }
/**
* Getter: Get the file cache
* Setter: set the file cache
*
* @returns {any}
* @memberof BaseFileHandle
*/
get cache(): any {
return this._cache;
}
set cache(v: any) {
this._cache = v;
this._cache_changed();
}
/** /**
* Set data to the file cache * Set data to the file cache
* *
@ -793,6 +812,17 @@ namespace OS {
}); });
} }
/**
* trigger cache changed event
*
* This function triggered when the file cached changed
*
* @protected
* @memberof BaseFileHandle
*/
protected _cache_changed():void {
}
/** /**
* Low level protocol-specific read operation * Low level protocol-specific read operation
* *
@ -1405,6 +1435,8 @@ namespace OS {
register("^app$", ApplicationHandle); register("^app$", ApplicationHandle);
var MEM_PARTITION: BufferFileHandle = undefined;
/** /**
* A buffer file handle represents a virtual file that is stored * A buffer file handle represents a virtual file that is stored
* on the system memory. Its protocol pattern is defined as: * on the system memory. Its protocol pattern is defined as:
@ -1426,19 +1458,75 @@ namespace OS {
*/ */
constructor(path: string, mime: string, data: any) { constructor(path: string, mime: string, data: any) {
super(path); super(path);
if (data) {
this.cache = data;
}
this.info = { this.info = {
mime: mime, mime: mime,
path: path, path: path,
size: data ? data.length : 0, size: data ? data.length : 0,
name: this.basename, name: this.basename,
type: "file", filename:this.basename,
ctime: (new Date()).toGMTString(),
mtime: (new Date()).toGMTString(),
type: undefined,
}; };
if (data) {
this.cache = data;
}
return this.init_file_tree();
}
/**
* init the mem file tree if necessary
*/
private init_file_tree(): BufferFileHandle
{
if(this.isRoot())
{
if(!MEM_PARTITION)
{
this.cache = {};
MEM_PARTITION = this;
}
return MEM_PARTITION;
}
let curr_level = "mem://".asFileHandle();
for(let i = 0;i<this.genealogy.length - 1;i++)
{
const segment = this.genealogy[i];
let handle = curr_level.cache[segment];
if(!handle)
{
handle = new BufferFileHandle(`${curr_level.path}/${segment}`, 'dir', {});
curr_level.cache[segment] = handle;
}
curr_level = handle;
if(!curr_level.cache)
{
curr_level.cache = {};
}
}
if(!curr_level.cache[this.basename])
curr_level.cache[this.basename] = this;
return curr_level.cache[this.basename];
}
/**
* cache changed handle
*/
protected _cache_changed(): void
{
if(!this.cache)
{
return;
}
if(typeof this.cache === "string" || this.cache instanceof Blob || this.cache instanceof Uint8Array)
{
this.info.type = "file";
}
else
{
this.info.type = "dir";
}
this.info.size = this.cache.length;
} }
/** /**
* Read the file meta-data * Read the file meta-data
* *
@ -1454,16 +1542,6 @@ namespace OS {
); );
} }
/**
* Get the parent file handle of the current file
*
*/
parent(): BaseFileHandle {
const handle = super.parent();
handle.info.type = "dir";
return handle;
}
/** /**
* Read file content stored in the file cached * Read file content stored in the file cached
* *
@ -1477,7 +1555,7 @@ namespace OS {
// read dir // read dir
if (this.info.type === "dir") { if (this.info.type === "dir") {
return resolve({ return resolve({
result: [], result: (Object.values(this.cache) as BufferFileHandle[]).map(o=>o.info),
error: false, error: false,
}); });
} }
@ -1513,6 +1591,10 @@ namespace OS {
*/ */
protected _down(): Promise<void> { protected _down(): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(this.info.type == "dir")
{
return reject(API.throwe(__("{0} is a directory", this.path)));
}
const blob = new Blob([this.cache], { const blob = new Blob([this.cache], {
type: "octet/stream", type: "octet/stream",
}); });