I am building a simple app that will download modules into internal storage in order to extend my app’s capabilities.
When my app initializes I check to see if the main storage folders need to be created, and create them if needed.
Here are the code snippets:
Personal service file for Filesystem commands (core.service.ts):
import { Injectable } from '@angular/core';
import { Filesystem, FilesystemDirectory, FilesystemEncoding } from '@capacitor/core';
import { Zip } from '@ionic-native/zip/ngx';
declare var require: any;
@Injectable({
providedIn: 'root'
})
export class CoreService {
constructor(private zip: Zip) {}
async createSystemDirectories() {
this.makePrivateDirectory();
console.log('***** Done adding private directory');
this.makeDirectory('books');
console.log('***** Done adding books directory');
this.makeDirectory('modules');
console.log('***** one adding modules directory');
let ls = this.readdir('private');
console.log('***** Read private folder: ', ls);
}
public makePrivateDirectory() {
try {
console.log('***** Creating private directory ('+FilesystemDirectory.Documents+'/private)');
return Filesystem.mkdir({
path: 'private',
directory: FilesystemDirectory.Documents,
recursive: false,
});
} catch (error) {
console.error('***** Could not create directory ('+FilesystemDirectory.Documents+'/private): ' + error, error);
}
}
public makeDirectory(path) {
try {
console.log('***** Creating directory directory ('+FilesystemDirectory.Documents+'/private/'+ path +')');
return Filesystem.mkdir({
path: 'private/' + path,
directory: FilesystemDirectory.Documents,
recursive: false,
});
} catch (error) {
console.error('***** Could not create directory ('+FilesystemDirectory.Documents+'/private/' + path + '): ' + error, error);
}
}
public readdir(dir) {
try {console.log('***** Reading directory ('+FilesystemDirectory.Documents+'/private/'+ dir +')');
return Filesystem.readdir({
path: dir,
directory: FilesystemDirectory.Documents
});
} catch(e) {
console.error('***** Unable to read directory ('+ FilesystemDirectory.Documents +'/private/'+ dir +')', e);
}
}
...
}
Here is the log output based on my personal logging:
2020-11-03 15:04:49.121 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 203 - Msg: ***** Creating directory directory (DOCUMENTS/private)
2020-11-03 15:04:49.122 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 257 - Msg: ***** Done adding private directory
2020-11-03 15:04:49.122 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 216 - Msg: ***** Creating directory directory (DOCUMENTS/private/books)
2020-11-03 15:04:49.123 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 259 - Msg: ***** Done adding books directory
2020-11-03 15:04:49.123 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 216 - Msg: ***** Creating directory directory (DOCUMENTS/private/modules)
2020-11-03 15:04:49.124 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 261 - Msg: ***** one adding modules directory
2020-11-03 15:04:49.124 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 244 - Msg: ***** Reading directory (DOCUMENTS/private)
2020-11-03 15:04:49.126 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 263 - Msg: ***** Read private folder:
2020-11-03 15:05:43.822 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 203 - Msg: ***** Creating directory directory (DOCUMENTS/private)
2020-11-03 15:05:43.823 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 257 - Msg: ***** Done adding private directory
2020-11-03 15:05:43.823 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 216 - Msg: ***** Creating directory directory (DOCUMENTS/private/books)
2020-11-03 15:05:43.824 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 259 - Msg: ***** Done adding books directory
2020-11-03 15:05:43.824 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 216 - Msg: ***** Creating directory directory (DOCUMENTS/private/modules)
2020-11-03 15:05:43.824 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 261 - Msg: ***** one adding modules directory
2020-11-03 15:05:43.824 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 244 - Msg: ***** Reading directory (DOCUMENTS/private)
2020-11-03 15:05:43.825 3240-3240/com.myapp.app I/Capacitor/Console: File: http://192.168.5.101:8100/main.js - Line 263 - Msg: ***** Read private folder:
Notice the first attempt at creating the main ‘private’ directory does not return any errors.
Then when I have the code do a second check to see if the directories need to be made again I get the same result: no error, no created directories…
How can I figure out what’s happening here, and get it to work?
Any ideas?..