diff --git a/d.ts/antos.d.ts b/d.ts/antos.d.ts index 308c7c1..8daae6b 100644 --- a/d.ts/antos.d.ts +++ b/d.ts/antos.d.ts @@ -9232,6 +9232,14 @@ declare namespace OS { * @memberof BufferFileHandle */ meta(): Promise; + /** + * Load the file meta-data before performing + * any task + * + * @returns {Promise} a promise on file meta-data + * @memberof BufferFileHandle + */ + onready(): Promise; /** * Read file content stored in the file cached * @@ -9246,11 +9254,40 @@ declare namespace OS { * * @protected * @param {string} t data type, see [[write]] - * @param {*} d data * @returns {Promise} * @memberof BufferFileHandle */ - protected _wr(t: string, d: any): Promise; + protected _wr(t: string): Promise; + /** + * Create sub directory + * + * Only work on directory file handle + * + * @protected + * @param {string} d sub directory name + * @returns {Promise} + * @memberof BufferFileHandle + */ + protected _mk(d: string): Promise; + /** + * Delete file/folder + * + * @protected + * @returns {Promise} + * @memberof BufferFileHandle + */ + protected _rm(): Promise; + setPath(p: string): void; + private updatePath; + /** + * Move file/folder + * + * @protected + * @param {string} d + * @returns {Promise} + * @memberof BufferFileHandle + */ + protected _mv(d: string): Promise; /** * Download the buffer file * diff --git a/release/antos-1.2.1.tar.gz b/release/antos-1.2.1.tar.gz index 13dd9e4..85b34ca 100644 Binary files a/release/antos-1.2.1.tar.gz and b/release/antos-1.2.1.tar.gz differ diff --git a/src/core/vfs.ts b/src/core/vfs.ts index 124a468..656b22f 100644 --- a/src/core/vfs.ts +++ b/src/core/vfs.ts @@ -1459,14 +1459,14 @@ namespace OS { constructor(path: string, mime: string, data: any) { super(path); this.info = { - mime: mime, + mime: mime?mime:'application/octet-stream', path: path, size: data ? data.length : 0, name: this.basename, filename:this.basename, ctime: (new Date()).toGMTString(), mtime: (new Date()).toGMTString(), - type: undefined, + type: 'file', }; if (data) { this.cache = data; @@ -1491,7 +1491,14 @@ namespace OS { for(let i = 0;i { return new Promise((resolve, reject) => + { + const data = {}; + for(const k in this.info) + { + data[k] = this.info[k]; + } resolve({ - result: this.info, + result: data, error: false, }) - ); + }); + } + + /** + * Load the file meta-data before performing + * any task + * + * @returns {Promise} a promise on file meta-data + * @memberof BufferFileHandle + */ + onready(): Promise { + // read meta data + return new Promise(async (resolve, reject) => { + try + { + const d = await this.meta(); + resolve(d.result as FileInfoType); + } + catch(e) + { + reject(__e(e)); + } + }); } /** @@ -1568,12 +1617,10 @@ namespace OS { * * @protected * @param {string} t data type, see [[write]] - * @param {*} d data * @returns {Promise} * @memberof BufferFileHandle */ - protected _wr(t: string, d: any): Promise { - this.cache = d; + protected _wr(t: string): Promise { return new Promise((resolve, reject) => resolve({ result: true, @@ -1581,6 +1628,106 @@ namespace OS { }) ); } + + /** + * Create sub directory + * + * Only work on directory file handle + * + * @protected + * @param {string} d sub directory name + * @returns {Promise} + * @memberof BufferFileHandle + */ + protected _mk(d: string): Promise { + return new Promise((resolve, reject) => { + if (this.info.type === "file") { + return reject( + API.throwe( + __("{0} is not a directory", this.path) + ) + ); + } + const handle = `${this.path}/${d}`.asFileHandle(); + if(handle.info.type === "file") + { + handle.cache = {}; + } + resolve({result: true, error: false}); + }); + } + + /** + * Delete file/folder + * + * @protected + * @returns {Promise} + * @memberof BufferFileHandle + */ + protected _rm(): Promise { + return new Promise(async (resolve, reject) => { + try { + const parent = this.parent(); + parent.cache[this.basename] = undefined; + delete parent.cache[this.basename]; + return resolve({result: true, error: false}); + } catch (e) { + return reject(__e(e)); + } + }); + } + + setPath(p: string): void + { + super.setPath(p); + if(this.info) + this.info.path = this.path; + } + + private updatePath(path: string) + { + this.setPath(path); + if(this.info.type == "file") + { + return; + } + for(const k in this.cache) + { + const child = this.cache[k]; + child.updatePath(`${this.path}/${child.basename}`); + } + } + + /** + * Move file/folder + * + * @protected + * @param {string} d + * @returns {Promise} + * @memberof BufferFileHandle + */ + protected _mv(d: string): Promise { + return new Promise(async (resolve, reject) => { + try { + if(d.includes(this.path)) + { + return reject(API.throwe(__("Unable to move file/folder from {0} to {1}", this.path, d))); + } + const parent = this.parent() + parent.cache[this.basename] = undefined; + delete parent.cache[this.basename]; + + const dest = d.asFileHandle(); + this.updatePath(dest.path); + dest.parent().cache[dest.basename] = this; + return resolve({result: true, error: false}); + } catch (e) { + console.log(this); + return reject(__e(e)); + } + }); + } + /** * Download the buffer file @@ -1924,11 +2071,11 @@ namespace OS { promises.push(new Promise(async (resolve, reject) => { try { const file = path.asFileHandle(); - const tof = `${to}/${file.basename}`.asFileHandle(); const meta = await file.onready(); if (meta.type === "dir") { const desdir = to.asFileHandle(); await desdir.mk(file.basename); + console.log(desdir, to); const ret = await file.read(); const files = ret.result.map((v: API.FileInfoType) => v.path); if (files.length > 0) { @@ -1940,6 +2087,7 @@ namespace OS { } } else { + const tof = `${to}/${file.basename}`.asFileHandle(); const content = await file.read("binary"); await tof .setCache( diff --git a/src/packages/Files/main.ts b/src/packages/Files/main.ts index 30c2eb0..0661b53 100644 --- a/src/packages/Files/main.ts +++ b/src/packages/Files/main.ts @@ -212,12 +212,6 @@ namespace OS { if (d.error) { return reject(d.error); } - if (!dir.isRoot()) { - const p = dir.parent(); - p.filename = "[..]"; - p.type = "dir"; - d.result.unshift(p); - } this.currdir = dir; $(this.navinput).val(dir.path); (this.scheme as GUI.tag.WindowTag).apptitle = dir.path;