1+ import { FileObject } from "./fileObject" ;
2+
3+ declare var cordova : any ;
4+
5+ declare global {
6+ interface Window {
7+ NativeFileWrapper : typeof NativeFileWrapper ;
8+ }
9+ }
10+
11+ export class NativeFileWrapper implements FileObject {
12+ private readonly path : string ;
13+
14+ constructor ( absolutePath : string ) {
15+ this . path = absolutePath ;
16+ }
17+
18+ private execPlugin ( action : string , args : any [ ] = [ ] ) : Promise < any > {
19+ return new Promise ( ( resolve , reject ) => {
20+ cordova . exec (
21+ ( result : any ) => resolve ( result ) ,
22+ ( error : any ) => reject ( error ) ,
23+ 'nativeFile' ,
24+ action ,
25+ [ this . path , ...args ]
26+ ) ;
27+ } ) ;
28+ }
29+
30+ async canRead ( ) : Promise < boolean > {
31+ const result = await this . execPlugin ( 'canRead' ) ;
32+ return result === 1 ;
33+ }
34+
35+ async canWrite ( ) : Promise < boolean > {
36+ const result = await this . execPlugin ( 'canWrite' ) ;
37+ return result === 1 ;
38+ }
39+
40+ async childByNameExists ( name : string ) : Promise < boolean | null > {
41+ try {
42+ const result = await this . execPlugin ( 'childByNameExists' , [ name ] ) ;
43+ return result === 1 ;
44+ } catch ( error ) {
45+ return null ;
46+ }
47+ }
48+
49+ async createNewFile ( ) : Promise < boolean > {
50+ try {
51+ const result = await this . execPlugin ( 'createNewFile' ) ;
52+ return result === 1 ;
53+ } catch ( error ) {
54+ return false ;
55+ }
56+ }
57+
58+ async delete ( ) : Promise < boolean > {
59+ try {
60+ const result = await this . execPlugin ( 'delete' ) ;
61+ return result === 1 ;
62+ } catch ( error ) {
63+ return false ;
64+ }
65+ }
66+
67+ async exists ( ) : Promise < boolean > {
68+ try {
69+ const result = await this . execPlugin ( 'exists' ) ;
70+ return result === 1 ;
71+ } catch ( error ) {
72+ return false ;
73+ }
74+ }
75+
76+ async getChildByName ( name : string ) : Promise < FileObject | null > {
77+ try {
78+ const childPath = await this . execPlugin ( 'getChildByName' , [ name ] ) ;
79+ if ( childPath && childPath !== "" ) {
80+ return new NativeFileWrapper ( childPath ) ;
81+ }
82+ return null ;
83+ } catch ( error ) {
84+ return null ;
85+ }
86+ }
87+
88+ async getLength ( ) : Promise < number > {
89+ try {
90+ return await this . execPlugin ( 'getLength' ) ;
91+ } catch ( error ) {
92+ return 0 ;
93+ }
94+ }
95+
96+ async getName ( ) : Promise < string > {
97+ try {
98+ return await this . execPlugin ( 'getName' ) ;
99+ } catch ( error ) {
100+ throw new Error ( `Failed to read file name: ${ error } ` ) ;
101+ }
102+ }
103+
104+ async getParentFile ( ) : Promise < FileObject | null > {
105+ try {
106+ const parentPath = await this . execPlugin ( 'getParentFile' ) ;
107+ if ( parentPath && parentPath !== "" ) {
108+ return new NativeFileWrapper ( parentPath ) ;
109+ }
110+ return null ;
111+ } catch ( error ) {
112+ return null ;
113+ }
114+ }
115+
116+ async isDirectory ( ) : Promise < boolean > {
117+ try {
118+ const result = await this . execPlugin ( 'isDirectory' ) ;
119+ return result === 1 ;
120+ } catch ( error ) {
121+ return false ;
122+ }
123+ }
124+
125+ async isFile ( ) : Promise < boolean > {
126+ try {
127+ const result = await this . execPlugin ( 'isFile' ) ;
128+ return result === 1 ;
129+ } catch ( error ) {
130+ return false ;
131+ }
132+ }
133+
134+ async isLink ( ) : Promise < boolean > {
135+ try {
136+ const result = await this . execPlugin ( 'isLink' ) ;
137+ return result === 1 ;
138+ } catch ( error ) {
139+ return false ;
140+ }
141+ }
142+
143+ async isNative ( ) : Promise < boolean > {
144+ try {
145+ const result = await this . execPlugin ( 'isNative' ) ;
146+ return result === 1 ;
147+ } catch ( error ) {
148+ return true ; // Default to true for native implementation
149+ }
150+ }
151+
152+ async isUnixLike ( ) : Promise < boolean > {
153+ try {
154+ const result = await this . execPlugin ( 'isUnixLike' ) ;
155+ return result === 1 ;
156+ } catch ( error ) {
157+ return true ; // Default to true for Android
158+ }
159+ }
160+
161+ async listFiles ( ) : Promise < FileObject [ ] > {
162+ try {
163+ const paths : string [ ] = await this . execPlugin ( 'listFiles' ) ;
164+ return paths . map ( path => new NativeFileWrapper ( path ) ) ;
165+ } catch ( error ) {
166+ return [ ] ;
167+ }
168+ }
169+
170+ async mkdir ( ) : Promise < boolean > {
171+ try {
172+ const result = await this . execPlugin ( 'mkdir' ) ;
173+ return result === 1 ;
174+ } catch ( error ) {
175+ return false ;
176+ }
177+ }
178+
179+ async mkdirs ( ) : Promise < boolean > {
180+ try {
181+ const result = await this . execPlugin ( 'mkdirs' ) ;
182+ return result === 1 ;
183+ } catch ( error ) {
184+ return false ;
185+ }
186+ }
187+
188+ async readText ( encoding : string = "UTF-8" ) : Promise < string > {
189+ try {
190+ return await this . execPlugin ( 'readText' , [ encoding ] ) ;
191+ } catch ( error ) {
192+ throw new Error ( `Failed to read file: ${ error } ` ) ;
193+ }
194+ }
195+
196+ async toUri ( ) : Promise < string > {
197+ try {
198+ return await this . execPlugin ( 'toUri' ) ;
199+ } catch ( error ) {
200+ return `file://${ this . path } ` ;
201+ }
202+ }
203+
204+ async writeText ( text : string , encoding : string = "UTF-8" ) : Promise < void > {
205+ try {
206+ await this . execPlugin ( 'writeText' , [ text , encoding ] ) ;
207+ } catch ( error ) {
208+ throw new Error ( `Failed to write file: ${ error } ` ) ;
209+ }
210+ }
211+
212+ // Utility method to get the absolute path
213+ getPath ( ) : string {
214+ return this . path ;
215+ }
216+ }
0 commit comments