apache / apache/cordova-plugin-file
Shortcut to read file
- Dominant language
- JavaScript
- Stars
- 744
- Forks
- 754
- Avg merge
- 3d 4h
- Merged PRs (30d)
- 1
Description
_Originally posted by @breautek in https://github.com/apache/cordova-plugin-file/issues/426#issuecomment-1386050453_
@breautek why reading a file with this plugin needs to be so painful ;) ?
Can't we have a shortcut, such as in NodeJS, simply [readFile](https://www.geeksforgeeks.org/node-js-fs-readfile-method/)
I propose the creation of this shortcut to read a file (with Promises)
```js
export function readFile (path, options) {
options = options || {}
return new Promise(function (resolve, reject) {
window.resolveLocalFileSystemURL(path, (fileEntry) => {
fileEntry.file((file) => {
const reader = new FileReader()
reader.onloadend = () => {
resolve(reader.result)
}
switch (options.format) {
case 'arrayBuffer':
reader.readAsArrayBuffer(file)
break
case 'binaryString':
reader.readAsBinaryString(file)
break
case 'dataURL':
reader.readAsDataURL(file)
break
default:
reader.readAsText(file)
}
reader.onerror = () => {
reject(Error(`Error occurred reading file: ${path}`))
}
})
}, (error) => {
reject(Error('resolve error', error))
})
})
}
```
Contributor guide
Research direction
Start by locating the plugin's existing file-reading entry points and compare them with the proposed readFile Promise API. Check how the current implementation handles window.resolveLocalFileSystemURL, FileReader, and the listed formats; done means a documented shortcut works across the plugin's supported platforms with appropriate tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nodejs
- Domain
- mobile
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100