Skip to content

Commit

Permalink
because RNFetchBlob.fs.unlink throw exception, wrraper it with a func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Wonday committed Jul 18, 2018
1 parent 8fe8a8f commit 45cb894
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ export default class Pdf extends Component {
const cacheFile = RNFetchBlob.fs.dirs.CacheDir + '/' + SHA1(uri) + '.pdf';

// delete old cache file
await RNFetchBlob.fs.unlink(cacheFile)

this._unlinkFile(cacheFile);
if (isNetwork) {
this._downloadFile(source, cacheFile);
} else if (isAsset) {
Expand All @@ -187,7 +187,7 @@ export default class Pdf extends Component {
this.setState({path: cacheFile, isDownloaded: true});
})
.catch(async (error) => {
await RNFetchBlob.fs.unlink(cacheFile);
this._unlinkFile(cacheFile);
this._onError(error);
})
} else if (isBase64) {
Expand All @@ -199,7 +199,7 @@ export default class Pdf extends Component {
this.setState({path: cacheFile, isDownloaded: true});
})
.catch(async (error) => {
await RNFetchBlob.fs.unlink(cacheFile);
this._unlinkFile(cacheFile);
this._onError(error)
});
} else {
Expand Down Expand Up @@ -228,7 +228,7 @@ export default class Pdf extends Component {
}

const tempCacheFile = cacheFile + '.tmp';
await RNFetchBlob.fs.unlink(tempCacheFile);
this._unlinkFile(tempCacheFile);

this.lastRNBFTask = RNFetchBlob.config({
// response data will be saved to this path if it has access right.
Expand Down Expand Up @@ -284,20 +284,28 @@ export default class Pdf extends Component {
break;
}
default:
await RNFetchBlob.fs.unlink(tempCacheFile);
await RNFetchBlob.fs.unlink(cacheFile);
this._unlinkFile(tempCacheFile);
this._unlinkFile(cacheFile);
this._onError(new Error(`load pdf failed with code ${status}`));
break;
}
})
.catch(async (error) => {
await RNFetchBlob.fs.unlink(tempCacheFile);
await RNFetchBlob.fs.unlink(cacheFile);
this._unlinkFile(tempCacheFile);
this._unlinkFile(cacheFile);
this._onError(error);
});

};

_unlinkFile = async (file) => {
try {
await RNFetchBlob.fs.unlink(file);
}catch (e) {

}
}

setNativeProps = nativeProps => {

this._root.setNativeProps(nativeProps);
Expand Down Expand Up @@ -403,4 +411,4 @@ const styles = StyleSheet.create({
width: 200,
height: 2
}
});
});

0 comments on commit 45cb894

Please sign in to comment.