App.js 5.7 KB

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