Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbedouret committed Nov 14, 2019
1 parent e6eff84 commit bbdf298
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
18 changes: 12 additions & 6 deletions src/components/UI/InputImage/InputImage.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
};

Expand Down
38 changes: 33 additions & 5 deletions src/cordova-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,47 @@ 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
);
},
onErrorLoadFs
);
return fe;
}
return null;
};

const writeFile = (fileEntry, dataObj) => {
Expand All @@ -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()) {
Expand Down

0 comments on commit bbdf298

Please sign in to comment.