diff --git a/src/components/UI/InputImage/InputImage.component.js b/src/components/UI/InputImage/InputImage.component.js index 24dfc6ec0..4797772c6 100644 --- a/src/components/UI/InputImage/InputImage.component.js +++ b/src/components/UI/InputImage/InputImage.component.js @@ -73,10 +73,10 @@ class InputImage extends PureComponent { onChange(imageUrl); } catch (error) { if (isCordova()) { - const fileFullName = window.cordova.file.dataDirectory + file.name; - console.log(fileFullName); - const fileEntry = await writeCvaFile(fileFullName, resizedImage); - onChange(fileEntry.toURL); + const filePath = '/Android/data/com.unicef.cboard/files/' + file.name; + const fEntry = await writeCvaFile(filePath, resizedImage); + console.log(fEntry); + onChange(fEntry.nativeURL); } else { const imageBase64 = this.blobToBase64(resizedImage); onChange(imageBase64); @@ -87,8 +87,14 @@ class InputImage extends PureComponent { }); } } else { - const imageBase64 = this.blobToBase64(resizedImage); - onChange(imageBase64); + if (isCordova()) { + const filePath = '/Android/data/com.unicef.cboard/files/' + file.name; + writeCvaFile(filePath, resizedImage); + onChange(filePath); + } else { + const imageBase64 = this.blobToBase64(resizedImage); + onChange(imageBase64); + } } }; diff --git a/src/cordova-util.js b/src/cordova-util.js index 708c62db4..5e0711886 100644 --- a/src/cordova-util.js +++ b/src/cordova-util.js @@ -5,15 +5,37 @@ export const onCordovaReady = onReady => export const writeCvaFile = async (name, blob) => { if (isCordova()) { - window.requestFileSystem( + return new Promise(function(resolve, reject) { + window.requestFileSystem( + window.LocalFileSystem.PERSISTENT, + 0, + function(fs) { + fs.root.getFile( + name, + { create: true, exclusive: false }, + function(fileEntry) { + writeFile(fileEntry, blob); + resolve(fileEntry); + }, + onErrorCreateFile + ); + }, + onErrorLoadFs + ); + }); + } +}; + +export const getCvaFileEntry = async name => { + if (isCordova()) { + const fe = window.requestFileSystem( window.LocalFileSystem.PERSISTENT, 0, function(fs) { fs.root.getFile( name, - { create: true, exclusive: false }, + { create: false, exclusive: false }, function(fileEntry) { - writeFile(fileEntry, blob); return fileEntry; }, onErrorCreateFile @@ -21,7 +43,9 @@ export const writeCvaFile = async (name, blob) => { }, onErrorLoadFs ); + return fe; } + return null; }; const writeFile = (fileEntry, dataObj) => { @@ -38,8 +62,12 @@ const writeFile = (fileEntry, dataObj) => { }); }; -const onErrorCreateFile = () => {}; -const onErrorLoadFs = () => {}; +const onErrorCreateFile = e => { + console.log('Error status: ' + e.status + ' - Error message: ' + e.message); +}; +const onErrorLoadFs = e => { + console.log('Error status: ' + e.status + ' - Error message: ' + e.message); +}; export const fileCvaOpen = (filePath, type) => { if (isCordova()) {