tusFileReader.js 721 B

12345678910111213141516171819202122232425262728293031
  1. import * as Expo from 'expo'
  2. import base64 from 'base64-js'
  3. export default function getTusFileReader (file, chunkSize, cb) {
  4. Expo.FileSystem.getInfoAsync(file.uri, { size: true }).then((info) => {
  5. cb(null, new TusFileReader(file, info.size))
  6. }).catch(cb)
  7. }
  8. class TusFileReader {
  9. constructor (file, size) {
  10. this.file = file
  11. this.size = size
  12. }
  13. slice (start, end, cb) {
  14. end = Math.min(end, this.size)
  15. const options = {
  16. encoding: Expo.FileSystem.EncodingTypes.Base64,
  17. length: end - start,
  18. position: start,
  19. }
  20. Expo.FileSystem.readAsStringAsync(this.file.uri, options).then((data) => {
  21. cb(null, base64.toByteArray(data))
  22. }).catch(cb)
  23. }
  24. close () {
  25. }
  26. }