App.js 4.4 KB

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