App.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // import * as Expo from 'expo'
  2. import React from 'react'
  3. import {
  4. Text,
  5. View,
  6. Button,
  7. // TouchableOpacity,
  8. TouchableHighlight
  9. // Image,
  10. // Linking
  11. } from 'react-native'
  12. import Uppy from '@uppy/core'
  13. import Tus from '@uppy/tus'
  14. import UppyFilePicker from './react-native/file-picker'
  15. export default class App extends React.Component {
  16. constructor () {
  17. super()
  18. this.state = {
  19. progress: 0,
  20. total: 0,
  21. file: null,
  22. uploadURL: null,
  23. isFilePickerVisible: false,
  24. isPaused: false,
  25. uploadStarted: false,
  26. uploadComplete: false
  27. }
  28. this.isReactNative = (typeof navigator !== 'undefined' &&
  29. typeof navigator.product === 'string' &&
  30. navigator.product.toLowerCase() === 'reactnative')
  31. this.startUpload = this.startUpload.bind(this)
  32. // this.selectPhotoTapped = this.selectPhotoTapped.bind(this)
  33. this.showFilePicker = this.showFilePicker.bind(this)
  34. this.hideFilePicker = this.hideFilePicker.bind(this)
  35. this.togglePauseResume = this.togglePauseResume.bind(this)
  36. console.log('Is this React Native?', this.isReactNative)
  37. this.uppy = Uppy({ autoProceed: true, debug: true })
  38. this.uppy.use(Tus, { endpoint: 'https://master.tus.io/files/' })
  39. this.uppy.on('upload-progress', (file, progress) => {
  40. this.setState({
  41. progress: progress.bytesUploaded,
  42. total: progress.bytesTotal,
  43. uploadStarted: true
  44. })
  45. })
  46. this.uppy.on('upload-success', (file, response) => {
  47. console.log(file.name, response)
  48. })
  49. this.uppy.on('complete', (result) => {
  50. this.setState({
  51. status: 'Upload complete ✅',
  52. uploadURL: result.successful[0].uploadURL,
  53. uploadComplete: true,
  54. uploadStarted: false
  55. })
  56. console.log('Upload complete:')
  57. console.log(result)
  58. })
  59. }
  60. // selectPhotoTapped () {
  61. // console.log('Selecting photo...')
  62. // Expo.Permissions.askAsync(Expo.Permissions.CAMERA_ROLL).then((isAllowed) => {
  63. // if (!isAllowed) return
  64. // Expo.ImagePicker.launchImageLibraryAsync({
  65. // mediaTypes: 'All'
  66. // })
  67. // .then((result) => {
  68. // console.log(result)
  69. // if (!result.cancelled) {
  70. // this.setState({ file: result })
  71. // this.uppy.addFile({
  72. // source: 'React Native',
  73. // name: 'photo.jpg',
  74. // type: result.type,
  75. // data: result
  76. // })
  77. // }
  78. // })
  79. // })
  80. // }
  81. startUpload () {
  82. this.setState({
  83. status: 'Uploading...'
  84. })
  85. }
  86. showFilePicker () {
  87. this.setState({
  88. isFilePickerVisible: true,
  89. uploadStarted: false,
  90. uploadComplete: false
  91. })
  92. }
  93. hideFilePicker () {
  94. this.setState({
  95. isFilePickerVisible: false
  96. })
  97. }
  98. togglePauseResume () {
  99. if (this.state.isPaused) {
  100. this.uppy.resumeAll()
  101. this.setState({
  102. isPaused: false
  103. })
  104. } else {
  105. this.uppy.pauseAll()
  106. this.setState({
  107. isPaused: true
  108. })
  109. }
  110. }
  111. resumeAll () {
  112. this.uppy.resumeAll()
  113. this.setState({
  114. isPaused: false
  115. })
  116. }
  117. render () {
  118. return (
  119. <View style={{
  120. flex: 1,
  121. backgroundColor: '#fff',
  122. alignItems: 'center',
  123. justifyContent: 'center'
  124. }}>
  125. <SelectAndUploadFileWithUppy
  126. state={this.state}
  127. selectPhotoTapped={this.selectPhotoTapped}
  128. showFilePicker={this.showFilePicker}
  129. togglePauseResume={this.togglePauseResume} />
  130. <UppyFilePicker
  131. show={this.state.isFilePickerVisible}
  132. uppy={this.uppy}
  133. onRequestClose={this.hideFilePicker}
  134. serverUrl="http://localhost:3020" />
  135. </View>
  136. )
  137. }
  138. }
  139. function ProgressBar (props) {
  140. const progress = props.progress || 0
  141. const total = props.total || 0
  142. const percentage = Math.round(progress / total * 100)
  143. const colorGreen = '#0b8600'
  144. const colorBlue = '#006bb7'
  145. return (
  146. <View style={{
  147. marginTop: 15,
  148. marginBottom: 15
  149. }}>
  150. <View
  151. style={{
  152. height: 5,
  153. overflow: 'hidden',
  154. backgroundColor: '#dee1e3'
  155. }}>
  156. <View style={{
  157. height: 5,
  158. backgroundColor: percentage === 100 ? colorGreen : colorBlue,
  159. width: percentage + '%'
  160. }} />
  161. </View>
  162. <Text>{percentage ? percentage + '%' : null}</Text>
  163. </View>
  164. )
  165. }
  166. function PauseResumeButton (props) {
  167. if (!props.uploadStarted || props.uploadComplete) {
  168. return null
  169. }
  170. return (
  171. <Button
  172. onPress={props.onPress}
  173. color="#bb00cc"
  174. title={props.isPaused ? 'Resume' : 'Pause'}
  175. accessibilityLabel={props.isPaused ? 'Resume' : 'Pause'}
  176. />
  177. )
  178. }
  179. function SelectAndUploadFileWithUppy (props) {
  180. return (
  181. <View style={{
  182. flex: 1,
  183. paddingVertical: 100
  184. }}>
  185. <Text>Uppy running in React Native</Text>
  186. {/* <TouchableOpacity onPress={props.selectPhotoTapped}>
  187. { props.state.file === null
  188. ? <Text>Select a Photo</Text>
  189. : <Image
  190. style={{ width: 200, height: 200 }}
  191. source={{ uri: props.state.file.uri }} />
  192. }
  193. { props.state.uploadURL !== null &&
  194. <Button
  195. onPress={(ev) => {
  196. Linking.openURL(props.state.uploadURL)
  197. }}
  198. title="Show Uploaded File"
  199. accessibilityLabel="Open uploaded file"
  200. />
  201. }
  202. </TouchableOpacity> */}
  203. <Text>Status: {props.state.status}</Text>
  204. <Text>{props.state.progress} of {props.state.total}</Text>
  205. <ProgressBar
  206. progress={props.state.progress}
  207. total={props.state.total}
  208. />
  209. <PauseResumeButton
  210. isPaused={props.state.isPaused}
  211. onPress={props.togglePauseResume}
  212. uploadStarted={props.state.uploadStarted}
  213. uploadComplete={props.state.uploadComplete} />
  214. <TouchableHighlight
  215. onPress={props.showFilePicker}>
  216. <Text>Select files from Uppy</Text>
  217. </TouchableHighlight>
  218. </View>
  219. )
  220. }