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 './react-native/file-picker'
  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. }
  54. this.isReactNative = (typeof navigator !== 'undefined' &&
  55. typeof navigator.product === 'string' &&
  56. navigator.product.toLowerCase() === 'reactnative')
  57. this.showFilePicker = this.showFilePicker.bind(this)
  58. this.hideFilePicker = this.hideFilePicker.bind(this)
  59. this.togglePauseResume = this.togglePauseResume.bind(this)
  60. console.log('Is this React Native?', this.isReactNative)
  61. this.uppy = Uppy({ autoProceed: true, debug: true })
  62. this.uppy.use(Tus, {
  63. endpoint: 'https://master.tus.io/files/',
  64. urlStorage: AsyncStorage,
  65. fingerprint: customFingerprint
  66. })
  67. this.uppy.on('upload-progress', (file, progress) => {
  68. this.setState({
  69. progress: progress.bytesUploaded,
  70. total: progress.bytesTotal,
  71. uploadStarted: true
  72. })
  73. })
  74. this.uppy.on('upload-success', (file, response) => {
  75. // console.log(file.name, response)
  76. })
  77. this.uppy.on('complete', (result) => {
  78. this.setState({
  79. status: 'Upload complete ✅',
  80. uploadURL: result.successful[0] ? result.successful[0].uploadURL : null,
  81. uploadComplete: true,
  82. uploadStarted: false
  83. })
  84. console.log('Upload complete:', result)
  85. })
  86. this.uppy.on('info-visible', () => {
  87. const info = this.uppy.getState().info
  88. this.setState({
  89. info: info
  90. })
  91. console.log('uppy-info:', info)
  92. })
  93. this.uppy.on('info-hidden', () => {
  94. this.setState({
  95. info: null
  96. })
  97. })
  98. }
  99. showFilePicker () {
  100. this.setState({
  101. isFilePickerVisible: true,
  102. uploadStarted: false,
  103. uploadComplete: false
  104. })
  105. }
  106. hideFilePicker () {
  107. this.setState({
  108. isFilePickerVisible: false
  109. })
  110. }
  111. togglePauseResume () {
  112. if (this.state.isPaused) {
  113. this.uppy.resumeAll()
  114. this.setState({
  115. isPaused: false
  116. })
  117. } else {
  118. this.uppy.pauseAll()
  119. this.setState({
  120. isPaused: true
  121. })
  122. }
  123. }
  124. render () {
  125. return (
  126. <View style={{
  127. paddingTop: 100,
  128. paddingLeft: 50,
  129. paddingRight: 50,
  130. flex: 1
  131. }}>
  132. <Text style={{
  133. fontSize: 25,
  134. marginBottom: 20,
  135. textAlign: 'center'
  136. }}>Uppy in React Native</Text>
  137. <View style={{alignItems: 'center'}}>
  138. <Image
  139. style={{width: 80, height: 78, marginBottom: 50}}
  140. source={require('./assets/uppy-logo.png')}
  141. />
  142. </View>
  143. <SelectFiles showFilePicker={this.showFilePicker} />
  144. {this.state.info
  145. ? <Text style={{
  146. marginBottom: 10,
  147. marginTop: 10,
  148. color: '#b8006b'}}>{this.state.info.message}</Text>
  149. : null
  150. }
  151. <ProgressBar
  152. progress={this.state.progress}
  153. total={this.state.total}
  154. />
  155. <PauseResumeButton
  156. isPaused={this.state.isPaused}
  157. onPress={this.togglePauseResume}
  158. uploadStarted={this.state.uploadStarted}
  159. uploadComplete={this.state.uploadComplete} />
  160. <UppyFilePicker
  161. show={this.state.isFilePickerVisible}
  162. uppy={this.uppy}
  163. onRequestClose={this.hideFilePicker}
  164. serverUrl="http://localhost:3020" />
  165. <FileList uppy={this.uppy} />
  166. {/* <Text>{this.state.status ? 'Status: ' + this.state.status : null}</Text>
  167. <Text>{this.state.progress} of {this.state.total}</Text> */}
  168. </View>
  169. )
  170. }
  171. }