App.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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('upload-success', (file, response) => {
  41. console.log(file.name, response)
  42. })
  43. this.uppy.on('complete', (result) => {
  44. this.setState({
  45. status: 'Upload complete ✅',
  46. uploadURL: result.successful[0].uploadURL
  47. })
  48. console.log('Upload complete:')
  49. console.log(result)
  50. })
  51. }
  52. selectPhotoTapped () {
  53. console.log('Selecting photo...')
  54. Expo.Permissions.askAsync(Expo.Permissions.CAMERA_ROLL).then((isAllowed) => {
  55. if (!isAllowed) return
  56. Expo.ImagePicker.launchImageLibraryAsync({
  57. mediaTypes: 'All'
  58. })
  59. .then((result) => {
  60. console.log(result)
  61. if (!result.cancelled) {
  62. this.setState({ file: result })
  63. this.uppy.addFile({
  64. source: 'React Native',
  65. name: 'photo.jpg',
  66. type: result.type,
  67. data: result
  68. })
  69. }
  70. })
  71. })
  72. }
  73. startUpload () {
  74. this.setState({
  75. status: 'Uploading...'
  76. })
  77. }
  78. showFilePicker () {
  79. this.setState({
  80. isFilePickerVisible: true
  81. })
  82. }
  83. hideFilePicker () {
  84. this.setState({
  85. isFilePickerVisible: false
  86. })
  87. }
  88. render () {
  89. return (
  90. <View style={{
  91. flex: 1,
  92. backgroundColor: '#fff',
  93. alignItems: 'center',
  94. justifyContent: 'center'
  95. }}>
  96. <SelectAndUploadFileWithUppy
  97. state={this.state}
  98. selectPhotoTapped={this.selectPhotoTapped}
  99. showFilePicker={this.showFilePicker} />
  100. <UppyFilePicker
  101. show={this.state.isFilePickerVisible}
  102. uppy={this.uppy}
  103. onRequestClose={this.hideFilePicker}
  104. serverUrl="http://localhost:3020" />
  105. </View>
  106. )
  107. }
  108. }
  109. function SelectAndUploadFileWithUppy (props) {
  110. return (
  111. <View style={{
  112. flex: 1,
  113. paddingVertical: 100
  114. }}>
  115. <Text>Uppy running in React Native</Text>
  116. <TouchableOpacity onPress={props.selectPhotoTapped}>
  117. { props.state.file === null
  118. ? <Text>Select a Photo</Text>
  119. : <Image
  120. style={{ width: 200, height: 200 }}
  121. source={{ uri: props.state.file.uri }} />
  122. }
  123. { props.state.uploadURL !== null &&
  124. <Button
  125. onPress={(ev) => {
  126. Linking.openURL(props.state.uploadURL)
  127. }}
  128. title="Show Uploaded File"
  129. accessibilityLabel="Open uploaded file"
  130. />
  131. }
  132. </TouchableOpacity>
  133. <Text>Status: {props.state.status}</Text>
  134. <Text>{props.state.progress} of {props.state.total}</Text>
  135. <TouchableHighlight
  136. onPress={() => {
  137. props.showFilePicker(true)
  138. }}>
  139. <Text>Select files from Uppy</Text>
  140. </TouchableHighlight>
  141. </View>
  142. )
  143. }