App.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.uppy.addFile({
  61. source: 'React Native',
  62. name: 'photo.jpg',
  63. type: result.type,
  64. data: result
  65. })
  66. }
  67. })
  68. })
  69. }
  70. startUpload () {
  71. this.setState({
  72. status: 'Uploading...'
  73. })
  74. }
  75. showFilePicker () {
  76. this.setState({
  77. isFilePickerVisible: true
  78. })
  79. }
  80. hideFilePicker () {
  81. this.setState({
  82. isFilePickerVisible: false
  83. })
  84. }
  85. render () {
  86. return (
  87. <View style={{
  88. flex: 1,
  89. backgroundColor: '#fff',
  90. alignItems: 'center',
  91. justifyContent: 'center'
  92. }}>
  93. <SelectAndUploadFileWithUppy
  94. state={this.state}
  95. selectPhotoTapped={this.selectPhotoTapped}
  96. showFilePicker={this.showFilePicker} />
  97. <UppyFilePicker
  98. show={this.state.isFilePickerVisible}
  99. uppy={this.uppy}
  100. onRequestClose={this.hideFilePicker}
  101. serverUrl="http://localhost:3020" />
  102. </View>
  103. )
  104. }
  105. }
  106. function SelectAndUploadFileWithUppy (props) {
  107. return (
  108. <View style={{
  109. flex: 1,
  110. paddingVertical: 100
  111. }}>
  112. <Text>Uppy running in React Native</Text>
  113. <TouchableOpacity onPress={props.selectPhotoTapped}>
  114. { props.state.file === null
  115. ? <Text>Select a Photo</Text>
  116. : <Image
  117. style={{ width: 200, height: 200 }}
  118. source={{ uri: props.state.file.uri }} />
  119. }
  120. { props.state.uploadURL !== null &&
  121. <Button
  122. onPress={(ev) => {
  123. Linking.openURL(props.state.uploadURL)
  124. }}
  125. title="Show Uploaded File"
  126. accessibilityLabel="Open uploaded file"
  127. />
  128. }
  129. </TouchableOpacity>
  130. <Text>Status: {props.state.status}</Text>
  131. <Text>{props.state.progress} of {props.state.total}</Text>
  132. <TouchableHighlight
  133. onPress={() => {
  134. props.showFilePicker(true)
  135. }}>
  136. <Text>Select files from Uppy</Text>
  137. </TouchableHighlight>
  138. </View>
  139. )
  140. }