App.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* eslint-disable */
  2. import React from 'react'
  3. import {
  4. StyleSheet,
  5. Text,
  6. View,
  7. Button,
  8. TouchableOpacity,
  9. Image } from 'react-native'
  10. import Uppy from '@uppy/core'
  11. import XHRUpload from '@uppy/xhr-upload'
  12. import { ImagePicker, Permissions } from 'expo'
  13. // import ImagePicker from 'react-native-image-picker'
  14. // import Tus from '@uppy/tus'
  15. function urlToBlob (url) {
  16. return new Promise((resolve, reject) => {
  17. var xhr = new XMLHttpRequest()
  18. xhr.onerror = reject
  19. xhr.onreadystatechange = () => {
  20. if (xhr.readyState === 4) {
  21. resolve(xhr.response)
  22. }
  23. };
  24. xhr.open('GET', url)
  25. xhr.responseType = 'blob' // convert type
  26. xhr.send()
  27. })
  28. }
  29. export default class App extends React.Component {
  30. constructor () {
  31. super()
  32. this.state = {
  33. progress: 0,
  34. total: 0,
  35. file: null
  36. }
  37. this.startUpload = this.startUpload.bind(this)
  38. this.selectPhotoTapped = this.selectPhotoTapped.bind(this)
  39. this.addFileToUppy = this.addFileToUppy.bind(this)
  40. }
  41. componentDidMount () {
  42. this.uppy = Uppy({ autoProceed: false, debug: true })
  43. this.uppy.use(XHRUpload, {
  44. endpoint: 'http://192.168.1.7:7000/',
  45. })
  46. // this.uppy.use(Tus, { endpoint: 'https://master.tus.io/files/' })
  47. this.uppy.on('upload-progress', (file, progress) => {
  48. this.setState({
  49. progress: progress.bytesUploaded,
  50. total: progress.bytesTotal
  51. })
  52. })
  53. this.uppy.on('complete', (ev) => {
  54. this.setState({
  55. status: 'Upload complete ✅'
  56. })
  57. console.log('tadada!')
  58. console.log(this.uppy.state.files)
  59. })
  60. }
  61. addFileToUppy (file) {
  62. console.log(file)
  63. var photo = {
  64. uri: file.uri,
  65. type: file.type,
  66. name: 'photo.jpg',
  67. }
  68. this.uppy.addFile({
  69. source: 'React Native',
  70. name: 'photo.jpg',
  71. type: file.type,
  72. data: photo
  73. })
  74. }
  75. // uploadFileDirecrly (file) {
  76. // var photo = {
  77. // uri: file.uri,
  78. // type: file.type,
  79. // name: 'photo.jpg',
  80. // };
  81. // var data = new FormData();
  82. // data.append('photo', photo);
  83. // // Create the config object for the POST
  84. // // You typically have an OAuth2 token that you use for authentication
  85. // const config = {
  86. // method: 'POST',
  87. // headers: {
  88. // Accept: 'application/json',
  89. // 'Content-Type': 'multipart/form-data;'
  90. // },
  91. // body: data
  92. // };
  93. // fetch('http://192.168.1.7:7000/', config)
  94. // .then(responseData => {
  95. // // Log the response form the server
  96. // // Here we get what we sent to Postman back
  97. // console.log(responseData);
  98. // })
  99. // .catch(err => {
  100. // console.log(err);
  101. // });
  102. // }
  103. selectPhotoTapped () {
  104. console.log('SELECT PHOTO')
  105. Permissions.askAsync(Permissions.CAMERA_ROLL).then((isAllowed) => {
  106. if (!isAllowed) return
  107. ImagePicker.launchImageLibraryAsync({})
  108. .then((result) => {
  109. console.log(result)
  110. if (!result.cancelled) {
  111. this.setState({ file: result })
  112. // this.uploadFileDirecrly(result)
  113. this.addFileToUppy(result)
  114. }
  115. })
  116. })
  117. // ImagePicker.showImagePicker(options, (response) => {
  118. // console.log('Response = ', response);
  119. // if (response.didCancel) {
  120. // console.log('User cancelled photo picker');
  121. // }
  122. // else if (response.error) {
  123. // console.log('ImagePicker Error: ', response.error);
  124. // }
  125. // else if (response.customButton) {
  126. // console.log('User tapped custom button: ', response.customButton);
  127. // }
  128. // else {
  129. // let source = { uri: response.uri };
  130. // // You can also display the image using data:
  131. // // let source = { uri: 'data:image/jpeg;base64,' + response.data };
  132. // this.setState({
  133. // ImageSource: source
  134. // });
  135. // }
  136. // })
  137. }
  138. startUpload () {
  139. this.setState({
  140. status: 'Uploading...'
  141. })
  142. this.uppy.upload()
  143. }
  144. render () {
  145. return (
  146. <View style={styles.container}>
  147. <Text>Uppy running in React Native</Text>
  148. <TouchableOpacity onPress={this.selectPhotoTapped}>
  149. { this.state.file === null
  150. ? <Text>Select a Photo</Text>
  151. : <Image
  152. style={{ width: 200, height: 200 }}
  153. source={{ uri: this.state.file.uri }}
  154. />
  155. }
  156. </TouchableOpacity>
  157. <Text>Status: {this.state.status}</Text>
  158. <Text>{this.state.progress} of {this.state.total}</Text>
  159. <Button
  160. onPress={this.startUpload}
  161. title="Start Upload"
  162. color="#841584"
  163. accessibilityLabel="Start uploading a file"
  164. />
  165. </View>
  166. )
  167. }
  168. }
  169. const styles = StyleSheet.create({
  170. container: {
  171. flex: 1,
  172. backgroundColor: '#fff',
  173. alignItems: 'center',
  174. justifyContent: 'center'
  175. }
  176. })