App.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 } from 'react-native'
  11. import Uppy from '@uppy/core'
  12. import Tus from '@uppy/tus'
  13. import UppyFilePicker from './react-native/file-picker'
  14. export default class App extends React.Component {
  15. constructor () {
  16. super()
  17. this.state = {
  18. progress: 0,
  19. total: 0,
  20. file: null,
  21. uploadURL: null,
  22. isFilePickerVisible: false
  23. }
  24. this.isReactNative = (typeof navigator !== 'undefined' &&
  25. typeof navigator.product === 'string' &&
  26. navigator.product.toLowerCase() === 'reactnative')
  27. this.startUpload = this.startUpload.bind(this)
  28. this.selectPhotoTapped = this.selectPhotoTapped.bind(this)
  29. this.showFilePicker = this.showFilePicker.bind(this)
  30. this.hideFilePicker = this.hideFilePicker.bind(this)
  31. console.log('Is this React Native?', this.isReactNative)
  32. this.uppy = Uppy({ autoProceed: true, debug: true })
  33. this.uppy.use(Tus, { endpoint: 'https://master.tus.io/files/' })
  34. this.uppy.on('upload-progress', (file, progress) => {
  35. this.setState({
  36. progress: progress.bytesUploaded,
  37. total: progress.bytesTotal
  38. })
  39. })
  40. this.uppy.on('complete', (result) => {
  41. this.setState({
  42. status: 'Upload complete ✅',
  43. uploadURL: result.successful[0].uploadURL
  44. })
  45. console.log('Upload complete:')
  46. console.log(this.uppy.state.files)
  47. })
  48. }
  49. selectPhotoTapped () {
  50. console.log('Selecting photo...')
  51. Expo.Permissions.askAsync(Expo.Permissions.CAMERA_ROLL).then((isAllowed) => {
  52. if (!isAllowed) return
  53. Expo.ImagePicker.launchImageLibraryAsync({
  54. mediaTypes: 'All'
  55. })
  56. .then((result) => {
  57. console.log(result)
  58. if (!result.cancelled) {
  59. this.setState({ file: result })
  60. this.addFile(result)
  61. }
  62. })
  63. })
  64. }
  65. startUpload () {
  66. this.setState({
  67. status: 'Uploading...'
  68. })
  69. }
  70. showFilePicker () {
  71. this.setState({
  72. isFilePickerVisible: true
  73. })
  74. }
  75. hideFilePicker () {
  76. this.setState({
  77. isFilePickerVisible: false
  78. })
  79. }
  80. render () {
  81. return (
  82. <View style={{
  83. flex: 1,
  84. backgroundColor: '#fff',
  85. alignItems: 'center',
  86. justifyContent: 'center'
  87. }}>
  88. <SelectAndUploadFileWithUppy
  89. state={this.state}
  90. selectPhotoTapped={this.selectPhotoTapped}
  91. showFilePicker={this.showFilePicker} />
  92. <UppyFilePicker
  93. show={this.state.isFilePickerVisible}
  94. uppy={this.uppy}
  95. onRequestClose={this.hideFilePicker}
  96. serverUrl="http://localhost:3020" />
  97. </View>
  98. )
  99. }
  100. }
  101. function SelectAndUploadFileWithUppy (props) {
  102. return (
  103. <View style={{
  104. flex: 1,
  105. paddingVertical: 100
  106. }}>
  107. <Text>Uppy running in React Native</Text>
  108. <TouchableOpacity onPress={props.selectPhotoTapped}>
  109. { props.state.file === null
  110. ? <Text>Select a Photo</Text>
  111. : <Image
  112. style={{ width: 200, height: 200 }}
  113. source={{ uri: props.state.file.uri }} />
  114. }
  115. { props.state.uploadURL !== null &&
  116. <Button
  117. onPress={(ev) => {
  118. Linking.openURL(props.state.uploadURL)
  119. }}
  120. title="Show Uploaded File"
  121. accessibilityLabel="Open uploaded file"
  122. />
  123. }
  124. </TouchableOpacity>
  125. <Text>Status: {props.state.status}</Text>
  126. <Text>{props.state.progress} of {props.state.total}</Text>
  127. <TouchableHighlight
  128. onPress={() => {
  129. props.showFilePicker(true)
  130. }}>
  131. <Text>Select files from Uppy</Text>
  132. </TouchableHighlight>
  133. </View>
  134. )
  135. }