From c204cec8d44fa3551d30f825c3f2f8c7edac166a Mon Sep 17 00:00:00 2001 From: martin bedouret Date: Fri, 15 Nov 2019 10:42:47 -0300 Subject: [PATCH] initial version --- .../UI/InputImage/InputImage.component.js | 31 +++++------ src/cordova-util.js | 51 +++++++------------ 2 files changed, 31 insertions(+), 51 deletions(-) diff --git a/src/components/UI/InputImage/InputImage.component.js b/src/components/UI/InputImage/InputImage.component.js index 4797772c6..786d3b591 100644 --- a/src/components/UI/InputImage/InputImage.component.js +++ b/src/components/UI/InputImage/InputImage.component.js @@ -72,29 +72,26 @@ class InputImage extends PureComponent { const imageUrl = await API.uploadFile(resizedImage, file.name); onChange(imageUrl); } catch (error) { - if (isCordova()) { - 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); - } + this.saveLocalImage(file.name, resizedImage); } finally { this.setState({ loading: false }); } } else { - 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); - } + this.saveLocalImage(file.name, resizedImage); + } + }; + + saveLocalImage = async (fileName, data) => { + const { onChange } = this.props; + if (isCordova()) { + const filePath = '/Android/data/com.unicef.cboard/files/' + fileName; + const fEntry = await writeCvaFile(filePath, data); + onChange(fEntry.nativeURL); + } else { + const imageBase64 = this.blobToBase64(data); + onChange(imageBase64); } }; diff --git a/src/cordova-util.js b/src/cordova-util.js index 5e0711886..57007e46a 100644 --- a/src/cordova-util.js +++ b/src/cordova-util.js @@ -13,8 +13,8 @@ export const writeCvaFile = async (name, blob) => { fs.root.getFile( name, { create: true, exclusive: false }, - function(fileEntry) { - writeFile(fileEntry, blob); + async function(fileEntry) { + await writeFile(fileEntry, blob); resolve(fileEntry); }, onErrorCreateFile @@ -26,39 +26,22 @@ export const writeCvaFile = async (name, blob) => { } }; -export const getCvaFileEntry = async name => { - if (isCordova()) { - const fe = window.requestFileSystem( - window.LocalFileSystem.PERSISTENT, - 0, - function(fs) { - fs.root.getFile( - name, - { create: false, exclusive: false }, - function(fileEntry) { - return fileEntry; - }, - onErrorCreateFile - ); - }, - onErrorLoadFs - ); - return fe; - } - return null; -}; - const writeFile = (fileEntry, dataObj) => { - fileEntry.createWriter(function(fileWriter) { - fileWriter.onwriteend = function() {}; - fileWriter.onerror = function(e) { - console.log('Failed file write: ' + e.toString()); - }; - // If data object is not passed in, create a new Blob instead. - if (!dataObj) { - dataObj = new Blob(['some file data'], { type: 'text/plain' }); - } - fileWriter.write(dataObj); + return new Promise(function(resolve, reject) { + fileEntry.createWriter(function(fileWriter) { + fileWriter.onwriteend = function() { + resolve(); + }; + fileWriter.onerror = function(e) { + console.log('Failed file write: ' + e.toString()); + reject(e.message); + }; + // If data object is not passed in, create a new Blob instead. + if (!dataObj) { + dataObj = new Blob(['some file data'], { type: 'text/plain' }); + } + fileWriter.write(dataObj); + }); }); };