Skip to content

Commit

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

Expand Down
51 changes: 17 additions & 34 deletions src/cordova-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});
});
};

Expand Down

0 comments on commit c204cec

Please sign in to comment.