App.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import 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 XHRUpload from '@uppy/xhr-upload'
  13. // import Tus from '@uppy/tus'
  14. import UppyFilePicker from './react-native/file-picker'
  15. import testUploadFileWithTus from './tus-test.js'
  16. export default class App extends React.Component {
  17. constructor () {
  18. super()
  19. this.state = {
  20. progress: 0,
  21. total: 0,
  22. file: null,
  23. uploadURL: null,
  24. isFilePickerVisible: false
  25. }
  26. this.isReactNative = (typeof navigator !== 'undefined' &&
  27. typeof navigator.product === 'string' &&
  28. navigator.product.toLowerCase() === 'reactnative')
  29. this.startUpload = this.startUpload.bind(this)
  30. this.selectPhotoTapped = this.selectPhotoTapped.bind(this)
  31. this.addFile = this.addFile.bind(this)
  32. this.showFilePicker = this.showFilePicker.bind(this)
  33. this.hideFilePicker = this.hideFilePicker.bind(this)
  34. console.log('Is this React Native?', this.isReactNative)
  35. // this.createAndUploadTextFileWithTus()
  36. this.uppy = Uppy({ autoProceed: false, debug: true })
  37. this.uppy.use(XHRUpload, {
  38. // endpoint: 'http://192.168.1.7:7000/',
  39. endpoint: 'http://api2.transloadit.com/'
  40. })
  41. // this.uppy.use(Tus, { endpoint: 'https://master.tus.io/files/' })
  42. this.uppy.on('upload-progress', (file, progress) => {
  43. this.setState({
  44. progress: progress.bytesUploaded,
  45. total: progress.bytesTotal
  46. })
  47. })
  48. this.uppy.on('complete', (result) => {
  49. this.setState({
  50. status: 'Upload complete ✅',
  51. uploadURL: result.successful[0].uploadURL
  52. })
  53. console.log('Upload complete:')
  54. console.log(this.uppy.state.files)
  55. })
  56. }
  57. createAndUploadTextFileWithTus () {
  58. const string = 'Hello, how are you doing? ' + Date.now()
  59. Expo.FileSystem.writeAsStringAsync(Expo.FileSystem.documentDirectory + '/myfile.txt', string)
  60. .then(() => {
  61. const path = Expo.FileSystem.documentDirectory + '/myfile.txt'
  62. const file = {
  63. uri: path
  64. }
  65. testUploadFileWithTus(file)
  66. .then((url) => {
  67. this.setState({
  68. status: `Upload successful: ${url}`
  69. })
  70. })
  71. .catch((err) => {
  72. this.setState({
  73. status: `Error: ${err}`
  74. })
  75. })
  76. })
  77. }
  78. addFile (file) {
  79. console.log(file)
  80. var photo = {
  81. uri: file.uri,
  82. type: file.type,
  83. name: 'photo.jpg'
  84. }
  85. this.setState({
  86. status: 'Uploading...'
  87. })
  88. testUploadFileWithTus(photo)
  89. .then((url) => {
  90. this.setState({
  91. status: `Upload successful`,
  92. uploadUrl: url
  93. })
  94. })
  95. .catch((err) => {
  96. this.setState({
  97. status: `Error: ${err}`
  98. })
  99. })
  100. // this.uppy.addFile({
  101. // source: 'React Native',
  102. // name: 'photo.jpg',
  103. // type: file.type,
  104. // data: photo
  105. // })
  106. }
  107. selectPhotoTapped () {
  108. console.log('Selecting photo...')
  109. // Expo.DocumentPicker.getDocumentAsync({
  110. // copyToCacheDirectory: false
  111. // })
  112. // .then((result) => {
  113. // console.log(result)
  114. // if (!result.cancelled) {
  115. // this.setState({ file: result })
  116. // this.addFile(result)
  117. // }
  118. // })
  119. Permissions.askAsync(Permissions.CAMERA_ROLL).then((isAllowed) => {
  120. if (!isAllowed) return
  121. Expo.ImagePicker.launchImageLibraryAsync({
  122. mediaTypes: 'All'
  123. })
  124. .then((result) => {
  125. console.log(result)
  126. if (!result.cancelled) {
  127. this.setState({ file: result })
  128. this.addFile(result)
  129. }
  130. })
  131. })
  132. }
  133. startUpload () {
  134. this.setState({
  135. status: 'Uploading...'
  136. })
  137. }
  138. showFilePicker () {
  139. this.setState({
  140. isFilePickerVisible: true
  141. })
  142. }
  143. hideFilePicker () {
  144. this.setState({
  145. isFilePickerVisible: false
  146. })
  147. }
  148. render () {
  149. return (
  150. <View style={{
  151. flex: 1,
  152. backgroundColor: '#fff',
  153. alignItems: 'center',
  154. justifyContent: 'center'
  155. }}>
  156. <SelectAndUploadFileWithUppy
  157. state={this.state}
  158. selectPhotoTapped={this.selectPhotoTapped}
  159. showFilePicker={this.showFilePicker} />
  160. <UppyFilePicker
  161. show={this.state.isFilePickerVisible}
  162. uppy={this.uppy}
  163. onRequestClose={this.hideFilePicker} />
  164. </View>
  165. )
  166. }
  167. }
  168. function SelectAndUploadFileWithUppy (props) {
  169. return (
  170. <View style={{
  171. flex: 1,
  172. paddingVertical: 100
  173. }}>
  174. <Text>Uppy running in React Native</Text>
  175. <TouchableOpacity onPress={props.selectPhotoTapped}>
  176. { props.state.file === null
  177. ? <Text>Select a Photo</Text>
  178. : <Image
  179. style={{ width: 200, height: 200 }}
  180. source={{ uri: props.state.file.uri }} />
  181. }
  182. { props.state.uploadURL !== null &&
  183. <Button
  184. onPress={(ev) => {
  185. Linking.openURL(props.state.uploadURL)
  186. }}
  187. title="Show Uploaded File"
  188. accessibilityLabel="Open uploaded file"
  189. />
  190. }
  191. </TouchableOpacity>
  192. <Text>Status: {props.state.status}</Text>
  193. <Text>{props.state.progress} of {props.state.total}</Text>
  194. {/* <Button
  195. onPress={props.startUpload}
  196. title="Start Upload"
  197. color="#841584"
  198. accessibilityLabel="Start uploading a file"
  199. /> */}
  200. <TouchableHighlight
  201. onPress={() => {
  202. props.showFilePicker(true)
  203. }}>
  204. <Text>Select files from Uppy</Text>
  205. </TouchableHighlight>
  206. </View>
  207. )
  208. }