App.js 4.8 KB

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