@@ -8,19 +8,40 @@ import helpers from "utils/helpers";
88
99const internalFs = {
1010 /**
11- *
11+ * List files from a Directory (not recursive)
1212 * @param {string } url
1313 * @returns {Promise }
1414 */
1515 listDir ( url ) {
1616 return new Promise ( ( resolve , reject ) => {
1717 reject = setMessage ( reject ) ;
18- window . resolveLocalFileSystemURL ( url , success , reject ) ;
1918
20- function success ( fs ) {
21- const reader = fs . createReader ( ) ;
22- reader . readEntries ( resolve , reject ) ;
23- }
19+ Filesystem . readdir ( { path : url } )
20+ . then ( ( result ) => {
21+ console . log (
22+ `Listed files/directories successfully for url: ${ url } , Result: ` ,
23+ result ,
24+ ) ;
25+ resolve (
26+ result . files . map ( ( file ) => ( {
27+ name : file . name ,
28+ url : file . uri ,
29+ size : file . size ,
30+ ctime : file . ctime ,
31+ mtime : file . mtime ,
32+ isFile : file . type === "file" ,
33+ isDirectory : file . type === "directory" ,
34+ // TODO: Link(symlink/hardlink) file detection if possible.
35+ isLink : false ,
36+ } ) ) ,
37+ ) ;
38+ } )
39+ . catch ( ( error ) => {
40+ console . log (
41+ `Error while listing Directory for url: ${ url } , error:` ,
42+ error ,
43+ ) ;
44+ } ) ;
2445 } ) ;
2546 } ,
2647
@@ -34,33 +55,33 @@ const internalFs = {
3455 * @param {boolean } exclusive If true, and the create option is also true,
3556 * the file must not exist prior to issuing the call.
3657 * Instead, it must be possible for it to be created newly at call time. The default is true.
37- * @returns {Promise }
58+ * @returns {Promise<string> } URL where the file was written into.
3859 */
3960 writeFile ( filename , data , create = false , exclusive = true ) {
4061 exclusive = create ? exclusive : false ;
4162 const name = filename . split ( "/" ) . pop ( ) ;
42- const dirname = Url . dirname ( filename ) ;
4363
4464 return new Promise ( ( resolve , reject ) => {
4565 reject = setMessage ( reject ) ;
46- window . resolveLocalFileSystemURL (
47- dirname ,
48- ( entry ) => {
49- entry . getFile (
50- name ,
51- { create, exclusive } ,
52- ( fileEntry ) => {
53- fileEntry . createWriter ( ( file ) => {
54- file . onwriteend = ( res ) => resolve ( filename ) ;
55- file . onerror = ( err ) => reject ( err . target . error ) ;
56- file . write ( data ) ;
57- } ) ;
58- } ,
59- reject ,
66+ Filesystem . writeFile ( {
67+ path : filename ,
68+ data : data ,
69+ encoding : Encoding . UTF8 ,
70+ recursive : create ,
71+ } )
72+ . then ( ( file ) => {
73+ console . log (
74+ `Successfully written into (name: ${ name } ) ${ filename } file` ,
6075 ) ;
61- } ,
62- reject ,
63- ) ;
76+ resolve ( file . uri ) ;
77+ } )
78+ . catch ( ( error ) => {
79+ console . error (
80+ `Failed to write into (name: ${ name } ) ${ filename } file, error: ` ,
81+ error ,
82+ ) ;
83+ reject ( error ) ;
84+ } ) ;
6485 } ) ;
6586 } ,
6687
@@ -102,33 +123,19 @@ const internalFs = {
102123 readFile ( filename ) {
103124 return new Promise ( ( resolve , reject ) => {
104125 reject = setMessage ( reject ) ;
105- window . resolveLocalFileSystemURL (
106- filename ,
107- ( fileEntry ) => {
108- ( async ( ) => {
109- const url = fileEntry . toInternalURL ( ) ;
110- try {
111- const data = await ajax ( {
112- url : url ,
113- responseType : "arraybuffer" ,
114- } ) ;
115-
116- resolve ( { data } ) ;
117- } catch ( error ) {
118- fileEntry . file ( ( file ) => {
119- const fileReader = new FileReader ( ) ;
120- fileReader . onerror = reject ;
121- fileReader . readAsArrayBuffer ( file ) ;
122- fileReader . onloadend = ( ) => {
123- resolve ( { data : fileReader . result } ) ;
124- } ;
125- } , reject ) ;
126- }
127- } ) ( ) ;
128- } ,
129- reject ,
130- ) ;
131- } ) ;
126+ Filesystem . readFile ( { path : filename , encoding : Encoding . UTF8 } ) . then ( ( readFileResult ) => {
127+ const fileReader = new FileReader ( )
128+ fileReader . onerror = reject ;
129+ fileReader . readAsArrayBuffer ( readFileResult . data )
130+ fileReader . onloadend = ( ) => {
131+ console . log ( `Successfully Read File contents for "${ filename } " file.` )
132+ resolve ( { data : fileReader . result } )
133+ }
134+ } ) . catch ( ( error ) => {
135+ console . error ( `Failed to Read File contents of "${ filename } ", error: ` , error )
136+ reject ( error )
137+ } )
138+ } )
132139 } ,
133140
134141 /**
@@ -169,21 +176,26 @@ const internalFs = {
169176 createDir ( path , dirname ) {
170177 return new Promise ( ( resolve , reject ) => {
171178 reject = setMessage ( reject ) ;
172- window . resolveLocalFileSystemURL (
173- path ,
174- ( fs ) => {
175- fs . getDirectory (
176- dirname ,
177- { create : true } ,
178- async ( ) => {
179- const stats = await this . stats ( Url . join ( path , dirname ) ) ;
180- resolve ( stats . url ) ;
181- } ,
182- reject ,
179+ // TODO!: ask about `recursive` option
180+ Filesystem . mkdir ( {
181+ path : `${ path } ${ dirname } ` ,
182+ recursive : false ,
183+ } )
184+ . then ( ( ) => {
185+ console . log (
186+ `Created "${ dirname } " Directory successfully on path: ${ path } ` ,
183187 ) ;
184- } ,
185- reject ,
186- ) ;
188+ Filesystem . stat ( { path : `${ path } ${ dirname } ` } )
189+ . then ( ( stats ) => resolve ( stats . url ) )
190+ . catch ( reject ) ;
191+ } )
192+ . catch ( ( error ) => {
193+ console . error (
194+ `Failed to create ${ dirname } directory in path: ${ path } , error:` ,
195+ error ,
196+ ) ;
197+ reject ( error ) ;
198+ } ) ;
187199 } ) ;
188200 } ,
189201
@@ -219,14 +231,29 @@ const internalFs = {
219231 reject = setMessage ( reject ) ;
220232 this . verify ( src , dest )
221233 . then ( ( res ) => {
222- const { src, dest } = res ;
223-
224- src [ action ] (
225- dest ,
226- undefined ,
227- ( entry ) => resolve ( decodeURIComponent ( entry . nativeURL ) ) ,
228- reject ,
229- ) ;
234+ if ( action === "copyTO" ) {
235+ Filesystem . copy ( {
236+ from : src ,
237+ to : dest
238+ } ) . then ( ( copyResult ) => {
239+ console . log ( `Successfully copied from "${ src } " to "${ dest } "` )
240+ resolve ( copyResult . uri )
241+ } ) . catch ( ( error ) => {
242+ console . error ( `Failed to copy from "${ src } " to "${ dest } "` )
243+ reject ( error )
244+ } )
245+ } else if ( action === "moveTO" ) {
246+ Filesystem . rename ( {
247+ from : src ,
248+ to : dest
249+ } ) . then ( ( moveResult ) => {
250+ console . log ( `Successfully moved from "${ src } " to "${ dest } "` )
251+ resolve ( dest )
252+ } ) . catch ( ( error ) => {
253+ console . error ( `Failed to move from "${ src } " to "${ dest } "` )
254+ reject ( error )
255+ } )
256+ }
230257 } )
231258 . catch ( reject ) ;
232259 } ) ;
@@ -240,11 +267,14 @@ const internalFs = {
240267 stats ( filename ) {
241268 return new Promise ( ( resolve , reject ) => {
242269 reject = setMessage ( reject ) ;
243- window . resolveLocalFileSystemURL (
244- filename ,
245- ( entry ) => {
270+ Filesystem . stat ( { path : filename } )
271+ . then ( ( entry ) => {
272+ console . log (
273+ `Successfully returned stats for "${ filename } ", Result: ` ,
274+ entry ,
275+ ) ;
246276 sdcard . stats (
247- entry . nativeURL ,
277+ entry . uri ,
248278 ( stats ) => {
249279 helpers . defineDeprecatedProperty (
250280 stats ,
@@ -261,13 +291,19 @@ const internalFs = {
261291 } ,
262292 reject ,
263293 ) ;
264- } ,
265- reject ,
266- ) ;
294+ } )
295+ . catch ( ( error ) => {
296+ console . error (
297+ `Failed to show stats for "${ filename } ", error:` ,
298+ error ,
299+ ) ;
300+ reject ( error ) ;
301+ } ) ;
267302 } ) ;
268303 } ,
269304
270305 /**
306+ * TODO: check this function with Rohit.
271307 * Verify if a file or directory exists
272308 * @param {string } src
273309 * @param {string } dest
@@ -276,36 +312,48 @@ const internalFs = {
276312 verify ( src , dest ) {
277313 return new Promise ( ( resolve , reject ) => {
278314 reject = setMessage ( reject ) ;
279- window . resolveLocalFileSystemURL (
280- src ,
281- ( srcEntry ) => {
282- window . resolveLocalFileSystemURL (
283- dest ,
284- ( destEntry ) => {
285- window . resolveLocalFileSystemURL (
286- Url . join ( destEntry . nativeURL , srcEntry . name ) ,
287- ( res ) => {
288- reject ( {
289- code : 12 ,
290- } ) ;
291- } ,
292- ( err ) => {
293- if ( err . code === 1 ) {
294- resolve ( {
295- src : srcEntry ,
296- dest : destEntry ,
297- } ) ;
298- } else {
299- reject ( err ) ;
300- }
301- } ,
315+
316+ // check if source exists
317+ Filesystem . stat ( {
318+ path : src ,
319+ } )
320+ . then ( ( srcStat ) => {
321+ console . log (
322+ `" ${ src } " source dir/file verified successful, checking if source dir/file already exists in " ${ dest } " destination file/dir` ,
323+ ) ;
324+ // Check if file/folder already exists at the destination
325+ Filesystem . stat ( {
326+ path : ` ${ dest } / ${ srcStat . name } ` ,
327+ } )
328+ . then ( ( ) => {
329+ // File already exists error.
330+ reject ( {
331+ code : 12 ,
332+ } ) ;
333+ } )
334+ . catch ( ( fileExistsErr ) => {
335+ console . error (
336+ "Failed to verify source in destination, error: " ,
337+ error ,
302338 ) ;
303- } ,
304- reject ,
339+ // if we get a "not found" error (code 1), that's good - we can copy
340+ if ( fileExistsErr . code === 1 ) {
341+ resolve ( {
342+ src : { path : src } ,
343+ dest : { path : dest } ,
344+ } ) ;
345+ } else {
346+ reject ( fileExistsErr ) ;
347+ }
348+ } ) ;
349+ } )
350+ . catch ( ( error ) => {
351+ console . error (
352+ `Failed to verify "${ src } " source dir/file, error: ` ,
353+ error ,
305354 ) ;
306- } ,
307- reject ,
308- ) ;
355+ reject ( error ) ;
356+ } ) ;
309357 } ) ;
310358 } ,
311359
@@ -316,16 +364,21 @@ const internalFs = {
316364 exists ( url ) {
317365 return new Promise ( ( resolve , reject ) => {
318366 reject = setMessage ( reject ) ;
319- window . resolveLocalFileSystemURL (
320- url ,
321- ( entry ) => {
367+ Filesystem . stat ( {
368+ path : url ,
369+ } )
370+ . then ( ( stats ) => {
371+ if ( ! stats . uri ) return resolve ( false ) ;
372+ console . log (
373+ `Successfully found (name: ${ stats . name || "name not found" } ) "${ url } " existing` ,
374+ ) ;
322375 resolve ( true ) ;
323- } ,
324- ( err ) => {
325- if ( err . code === 1 ) resolve ( false ) ;
326- reject ( err ) ;
327- } ,
328- ) ;
376+ } )
377+ . catch ( ( err ) => {
378+ // on-error defaulting to false,
379+ // as capacitor doesn't emit error codes, for error types(file not found, etc)
380+ resolve ( false ) ;
381+ } ) ;
329382 } ) ;
330383 } ,
331384
0 commit comments