Skip to content

Commit

Permalink
fix(file): readAsText() and adds readAsDataURL() in File plugin (#346)
Browse files Browse the repository at this point in the history
* Updates docs for CameraPreview, Base64ToGallery and Developer.MD

-DEVELOPER.md: Corrects 'npm run tslint' instruction to 'npm run lint'
-CameraPreview: Fixes syntax error in hide
-Base64ToGallery: Adds desc of options obj props

* Fixes resolveLocalFileSystemURL bug in File.readAsText() and adds File.readAsDataUrl()

- File.readAsText(): window.resolveLocalFileSystemURL() was incorrectly implemented

* Updates callback arg in File.checkFile()

Callback arg of window.resolveLocalFileSystemURL was fileSystem, but it actually returns fileEntry

* Corrects repo link and comments out setFlashMode()

- Repo prop and link in desc were not pointing to the same gh repo as the plugin
- setFlashMode() is not currently available in the repo that gets pulled by the corodova plugin
  • Loading branch information
amuramoto authored and ihadeed committed Jul 22, 2016
1 parent b95191a commit 77d31cd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/plugins/camera-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export interface CameraPreviewSize {
* @description
* Showing camera preview in HTML
*
* Requires {@link module:driftyco/ionic-native} and the Cordova plugin: `cordova-plugin-camera-preview`. For more info, please see the [Cordova Camera Preview Plugin Docs](https://github.com/westonganger/cordova-plugin-camera-preview).
* Requires {@link module:driftyco/ionic-native} and the Cordova plugin: `cordova-plugin-camera-preview`. For more info, please see the [Cordova Camera Preview Plugin Docs](https://github.com/cordova-plugin-camera-preview/cordova-plugin-camera-preview).
*
*/
@Plugin({
plugin: 'cordova-plugin-camera-preview',
pluginRef: 'cordova.plugins.camerapreview',
repo: 'https://github.com/westonganger/cordova-plugin-camera-preview',
repo: 'https://github.com/cordova-plugin-camera-preview/cordova-plugin-camera-preview',
platforms: ['Android', 'iOS']
})
export class CameraPreview {
Expand Down Expand Up @@ -104,10 +104,10 @@ export class CameraPreview {
/**
* Set the default mode for the Flash.
*/
@Cordova({
sync: true
})
static setFlashMode(mode: number): void { };
// @Cordova({
// sync: true
// })
// static setFlashMode(mode: number): void { };

/**
* Set camera color effect.
Expand Down
68 changes: 53 additions & 15 deletions src/plugins/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,9 @@ export class File {
try {
var directory = path + file;

window.resolveLocalFileSystemURL(directory, function (fileSystem) {
if (fileSystem.isFile === true) {
resolveFn(fileSystem);
window.resolveLocalFileSystemURL(directory, function (fileEntry) {
if (fileEntry.isFile === true) {
resolveFn(fileEntry);
} else {
rejectFn({code: 13, message: 'input is not a file'});
}
Expand Down Expand Up @@ -474,21 +474,62 @@ export class File {
/**
* Read a file as string.
*
* @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
* @param {string} fileName Name of file to move
* @param {string} uri Base FileSystem. Please refer to the iOS and Android filesystems above
* @return Returns a Promise that resolves or rejects with an error.
*/
static readAsText(path: string, fileName: string): Promise<any> {
static readAsText(file_uri: string): Promise<any> {
let resolveFn, rejectFn;
let promise = new Promise((resolve, reject) => {resolveFn = resolve; rejectFn = reject; });

if ((/^\//.test(fileName))) {
rejectFn('file-name cannot start with \/');
try {

window.resolveLocalFileSystemURL(file_uri, function (fileEntry) {

fileEntry.file(function (file) {
var reader = new FileReader();

reader.onloadend = function(e) {
if (this.result !== undefined && this.result !== null) {
resolveFn(this.result);
} else if (this.error !== undefined && this.error !== null) {
rejectFn(this.error);
} else {
rejectFn({code: null, message: 'READER_ONLOADEND_ERR'});
}
};

reader.readAsText(file);

}, function (err) {
err.message = File.cordovaFileError[err.code];
rejectFn(err);
});
}, function (er) {
er.message = File.cordovaFileError[er.code];
rejectFn(er);
});
} catch (e) {
e.message = File.cordovaFileError[e.code];
rejectFn(e);
}

return promise;
}

/**
* Read a file as string.
*
* @param {string} uri Base FileSystem. Please refer to the iOS and Android filesystems above
* @return Returns a Promise that resolves or rejects with an error.
*/
static readAsDataURL(file_uri: string): Promise<any> {
let resolveFn, rejectFn;
let promise = new Promise((resolve, reject) => {resolveFn = resolve; rejectFn = reject; });

try {
window.resolveLocalFileSystemURL(path, function (fileSystem) {
fileSystem.getFile(fileName, {create: false}, function (fileEntry) {

window.resolveLocalFileSystemURL(file_uri, function (fileEntry) {

fileEntry.file(function (file) {
var reader = new FileReader();

Expand All @@ -502,11 +543,8 @@ export class File {
}
};

reader.readAsText(file);
}, function (error) {
error.message = File.cordovaFileError[error.code];
rejectFn(error);
});
reader.readAsDataURL(file);

}, function (err) {
err.message = File.cordovaFileError[err.code];
rejectFn(err);
Expand Down

0 comments on commit 77d31cd

Please sign in to comment.