App.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // import * as Expo from 'expo'
  2. import React from 'react'
  3. import {
  4. Text,
  5. View,
  6. AsyncStorage,
  7. Image
  8. } from 'react-native'
  9. import Uppy from '@uppy/core'
  10. import Tus from '@uppy/tus'
  11. import UppyFilePicker from '@uppy/react-native'
  12. import FileList from './FileList'
  13. import PauseResumeButton from './PauseResumeButton'
  14. import ProgressBar from './ProgressBar'
  15. import SelectFiles from './SelectFilesButton'
  16. import getTusFileReader from './tusFileReader'
  17. export default class App extends React.Component {
  18. constructor () {
  19. super()
  20. this.state = {
  21. progress: 0,
  22. total: 0,
  23. file: null,
  24. uploadURL: null,
  25. isFilePickerVisible: false,
  26. isPaused: false,
  27. uploadStarted: false,
  28. uploadComplete: false,
  29. info: null,
  30. totalProgress: 0
  31. }
  32. this.isReactNative = (typeof navigator !== 'undefined' &&
  33. typeof navigator.product === 'string' &&
  34. navigator.product.toLowerCase() === 'reactnative')
  35. this.showFilePicker = this.showFilePicker.bind(this)
  36. this.hideFilePicker = this.hideFilePicker.bind(this)
  37. this.togglePauseResume = this.togglePauseResume.bind(this)
  38. console.log('Is this React Native?', this.isReactNative)
  39. this.uppy = Uppy({ autoProceed: true, debug: true })
  40. this.uppy.use(Tus, {
  41. endpoint: 'https://master.tus.io/files/',
  42. urlStorage: AsyncStorage,
  43. fileReader: getTusFileReader,
  44. chunkSize: 10 * 1024 * 1024 // keep the chunk size small to avoid memory exhaustion
  45. })
  46. this.uppy.on('upload-progress', (file, progress) => {
  47. this.setState({
  48. progress: progress.bytesUploaded,
  49. total: progress.bytesTotal,
  50. totalProgress: this.uppy.state.totalProgress,
  51. uploadStarted: true
  52. })
  53. })
  54. this.uppy.on('upload-success', (file, response) => {
  55. // console.log(file.name, response)
  56. })
  57. this.uppy.on('complete', (result) => {
  58. this.setState({
  59. status: 'Upload complete ✅',
  60. uploadURL: result.successful[0] ? result.successful[0].uploadURL : null,
  61. uploadComplete: true,
  62. uploadStarted: false
  63. })
  64. console.log('Upload complete:', result)
  65. })
  66. this.uppy.on('info-visible', () => {
  67. const info = this.uppy.getState().info
  68. this.setState({
  69. info: info
  70. })
  71. console.log('uppy-info:', info)
  72. })
  73. this.uppy.on('info-hidden', () => {
  74. this.setState({
  75. info: null
  76. })
  77. })
  78. }
  79. showFilePicker () {
  80. this.setState({
  81. isFilePickerVisible: true,
  82. uploadStarted: false,
  83. uploadComplete: false
  84. })
  85. }
  86. hideFilePicker () {
  87. this.setState({
  88. isFilePickerVisible: false
  89. })
  90. }
  91. togglePauseResume () {
  92. if (this.state.isPaused) {
  93. this.uppy.resumeAll()
  94. this.setState({
  95. isPaused: false
  96. })
  97. } else {
  98. this.uppy.pauseAll()
  99. this.setState({
  100. isPaused: true
  101. })
  102. }
  103. }
  104. render () {
  105. return (
  106. <View style={{
  107. paddingTop: 100,
  108. paddingLeft: 50,
  109. paddingRight: 50,
  110. flex: 1
  111. }}>
  112. <Text style={{
  113. fontSize: 25,
  114. marginBottom: 20,
  115. textAlign: 'center'
  116. }}>Uppy in React Native</Text>
  117. <View style={{alignItems: 'center'}}>
  118. <Image
  119. style={{width: 80, height: 78, marginBottom: 50}}
  120. source={require('./assets/uppy-logo.png')}
  121. />
  122. </View>
  123. <SelectFiles showFilePicker={this.showFilePicker} />
  124. {this.state.info
  125. ? <Text style={{
  126. marginBottom: 10,
  127. marginTop: 10,
  128. color: '#b8006b'}}>{this.state.info.message}</Text>
  129. : null
  130. }
  131. <ProgressBar progress={this.state.totalProgress} />
  132. <PauseResumeButton
  133. isPaused={this.state.isPaused}
  134. onPress={this.togglePauseResume}
  135. uploadStarted={this.state.uploadStarted}
  136. uploadComplete={this.state.uploadComplete} />
  137. <UppyFilePicker
  138. uppy={this.uppy}
  139. show={this.state.isFilePickerVisible}
  140. onRequestClose={this.hideFilePicker}
  141. companionUrl="http://localhost:3020" />
  142. <FileList uppy={this.uppy} />
  143. {/* <Text>{this.state.status ? 'Status: ' + this.state.status : null}</Text>
  144. <Text>{this.state.progress} of {this.state.total}</Text> */}
  145. </View>
  146. )
  147. }
  148. }