1- import { FileObject } from "./fileObject" ;
1+ import { FileObject } from "./fileObject" ;
22
33declare var cordova : any ;
44
5+
6+ //alternative for internalFs.js
57export class NativeFileWrapper implements FileObject {
6- private readonly path : string ;
8+ private path : string | undefined ;
9+
10+ //always check if fileobject is ready before calling any class function
11+ ready : Promise < void > ;
12+
13+
714
8- constructor ( absolutePath : string ) {
9- this . path = absolutePath ;
10- //console.log(`[NativeFileWrapper] Created for path: ${absolutePath}`);
15+ private removePrefix ( str : string , prefix : string ) : string {
16+ return str . startsWith ( prefix ) ? str . slice ( prefix . length ) : str ;
1117 }
1218
19+
20+ constructor ( absolutePathOrUri : string , onReady :( obj :NativeFileWrapper ) => void ) {
21+ this . ready = ( async ( ) => {
22+ let temp = absolutePathOrUri ;
23+
24+ if ( absolutePathOrUri . startsWith ( "cdvfile://" ) ) {
25+ temp = await new Promise < string > ( ( resolve , reject ) => {
26+ // @ts -ignore
27+ window . resolveLocalFileSystemURL (
28+ absolutePathOrUri ,
29+ ( entry : any ) => resolve ( entry . toURL ( ) ) ,
30+ reject
31+ ) ;
32+ } ) ;
33+ }
34+
35+ this . path = this . removePrefix ( temp , "file://" ) ;
36+
37+ onReady ( this )
38+ } ) ( ) ;
39+ }
40+
41+
42+
43+
44+
1345 private execPlugin ( action : string , args : any [ ] = [ ] ) : Promise < any > {
1446 //console.log(`[NativeFileWrapper] execPlugin called: action=${action}, args=${JSON.stringify(args)}`);
1547 return new Promise ( ( resolve , reject ) => {
@@ -90,7 +122,7 @@ export class NativeFileWrapper implements FileObject {
90122 const childPath = await this . execPlugin ( 'getChildByName' , [ name ] ) ;
91123 //console.log(`[getChildByName] path=${this.path}, name=${name}, childPath=${childPath}`);
92124 if ( childPath && childPath !== "" ) {
93- return new NativeFileWrapper ( childPath ) ;
125+ return new NativeFileWrapper ( childPath , ( ) => { } ) ;
94126 }
95127 return null ;
96128 } catch ( error ) {
@@ -126,7 +158,7 @@ export class NativeFileWrapper implements FileObject {
126158 const parentPath = await this . execPlugin ( 'getParentFile' ) ;
127159 //console.log(`[getParentFile] path=${this.path}, parentPath=${parentPath}`);
128160 if ( parentPath && parentPath !== "" ) {
129- return new NativeFileWrapper ( parentPath ) ;
161+ return new NativeFileWrapper ( parentPath , ( ) => { } ) ;
130162 }
131163 return null ;
132164 } catch ( error ) {
@@ -194,7 +226,7 @@ export class NativeFileWrapper implements FileObject {
194226 try {
195227 const paths : string [ ] = await this . execPlugin ( 'listFiles' ) ;
196228 //console.log(`[listFiles] path=${this.path}, files=${JSON.stringify(paths)}`);
197- return paths . map ( path => new NativeFileWrapper ( path ) ) ;
229+ return paths . map ( path => new NativeFileWrapper ( path , ( ) => { } ) ) ;
198230 } catch ( error ) {
199231 console . error ( `[listFiles] path=${ this . path } , error=${ error } ` ) ;
200232 return [ ] ;
@@ -236,9 +268,8 @@ export class NativeFileWrapper implements FileObject {
236268
237269 async toUri ( ) : Promise < string > {
238270 try {
239- const uri = await this . execPlugin ( 'toUri' ) ;
240271 //console.log(`[toUri] path=${this.path}, uri=${uri}`);
241- return uri ;
272+ return await this . execPlugin ( 'toUri' ) ;
242273 } catch ( error ) {
243274 console . error ( `[toUri] path=${ this . path } , error=${ error } ` ) ;
244275 return `file://${ this . path } ` ;
@@ -257,6 +288,6 @@ export class NativeFileWrapper implements FileObject {
257288
258289 getPath ( ) : string {
259290 //console.log(`[getPath] returning path=${this.path}`);
260- return this . path ;
291+ return this . path ! ! ;
261292 }
262293}
0 commit comments