App.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = new 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. >
  113. <Text style={{
  114. fontSize: 25,
  115. marginBottom: 20,
  116. textAlign: 'center'
  117. }}
  118. >Uppy in React Native
  119. </Text>
  120. <View style={{ alignItems: 'center' }}>
  121. <Image
  122. style={{ width: 80, height: 78, marginBottom: 50 }}
  123. source={require('./assets/uppy-logo.png')}
  124. />
  125. </View>
  126. <SelectFiles showFilePicker={this.showFilePicker} />
  127. {this.state.info ? (
  128. <Text
  129. style={{
  130. marginBottom: 10,
  131. marginTop: 10,
  132. color: '#b8006b'
  133. }}
  134. >
  135. {this.state.info.message}
  136. </Text>
  137. ) : null}
  138. <ProgressBar progress={this.state.totalProgress} />
  139. <PauseResumeButton
  140. isPaused={this.state.isPaused}
  141. onPress={this.togglePauseResume}
  142. uploadStarted={this.state.uploadStarted}
  143. uploadComplete={this.state.uploadComplete}
  144. />
  145. <UppyFilePicker
  146. uppy={this.uppy}
  147. show={this.state.isFilePickerVisible}
  148. onRequestClose={this.hideFilePicker}
  149. companionUrl="http://localhost:3020"
  150. />
  151. <FileList uppy={this.uppy} />
  152. {/* <Text>{this.state.status ? 'Status: ' + this.state.status : null}</Text>
  153. <Text>{this.state.progress} of {this.state.total}</Text> */}
  154. </View>
  155. )
  156. }
  157. }