App.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* eslint-disable */
  2. import React from 'react'
  3. import {
  4. Text,
  5. View,
  6. Button,
  7. TextInput,
  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.isReactNative = (typeof navigator !== 'undefined' &&
  28. typeof navigator.product === 'string' &&
  29. navigator.product.toLowerCase() === 'reactnative')
  30. this.startUpload = this.startUpload.bind(this)
  31. this.selectPhotoTapped = this.selectPhotoTapped.bind(this)
  32. this.addFile = this.addFile.bind(this)
  33. this.showFilePicker = this.showFilePicker.bind(this)
  34. }
  35. createAndUploadTextFileWithTus () {
  36. const string = 'Hello, how are you doing? ' + Date.now()
  37. Expo.FileSystem.writeAsStringAsync(Expo.FileSystem.documentDirectory + '/myfile.txt', string)
  38. .then(() => {
  39. const path = Expo.FileSystem.documentDirectory + '/myfile.txt'
  40. const file = {
  41. uri: path
  42. }
  43. testUploadFileWithTus(file)
  44. .then((url) => {
  45. this.setState({
  46. status: `Upload successful: ${url}`
  47. })
  48. })
  49. .catch((err) => {
  50. this.setState({
  51. status: `Error: ${err}`
  52. })
  53. })
  54. })
  55. }
  56. componentDidMount () {
  57. console.log('Is this React Native?', this.isReactNative)
  58. this.createAndUploadTextFileWithTus()
  59. // this.uppy = Uppy({ autoProceed: false, debug: true })
  60. // this.uppy.use(XHRUpload, {
  61. // // endpoint: 'http://192.168.1.7:7000/',
  62. // endpoint: 'http://api2.transloadit.com/',
  63. // })
  64. // // this.uppy.use(Tus, { endpoint: 'https://master.tus.io/files/' })
  65. // this.uppy.on('upload-progress', (file, progress) => {
  66. // this.setState({
  67. // progress: progress.bytesUploaded,
  68. // total: progress.bytesTotal
  69. // })
  70. // })
  71. // this.uppy.on('complete', (ev) => {
  72. // this.setState({
  73. // status: 'Upload complete ✅'
  74. // })
  75. // console.log('tadada!')
  76. // console.log(this.uppy.state.files)
  77. // })
  78. }
  79. addFile (file) {
  80. console.log(file)
  81. var photo = {
  82. uri: file.uri,
  83. type: file.type,
  84. name: 'photo.jpg',
  85. }
  86. this.setState({
  87. status: 'Uploading...'
  88. })
  89. testUploadFileWithTus(photo)
  90. .then((url) => {
  91. this.setState({
  92. status: `Upload successful: ${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. Permissions.askAsync(Permissions.CAMERA_ROLL).then((isAllowed) => {
  110. if (!isAllowed) return
  111. ImagePicker.launchImageLibraryAsync({
  112. mediaTypes: 'All'
  113. })
  114. .then((result) => {
  115. console.log(result)
  116. if (!result.cancelled) {
  117. this.setState({ file: result })
  118. this.addFile(result)
  119. }
  120. })
  121. })
  122. // ImagePicker.showImagePicker(options, (response) => {
  123. // console.log('Response = ', response);
  124. // if (response.didCancel) {
  125. // console.log('User cancelled photo picker');
  126. // }
  127. // else if (response.error) {
  128. // console.log('ImagePicker Error: ', response.error);
  129. // }
  130. // else if (response.customButton) {
  131. // console.log('User tapped custom button: ', response.customButton);
  132. // }
  133. // else {
  134. // let source = { uri: response.uri };
  135. // // You can also display the image using data:
  136. // // let source = { uri: 'data:image/jpeg;base64,' + response.data };
  137. // this.setState({
  138. // ImageSource: source
  139. // });
  140. // }
  141. // })
  142. }
  143. startUpload () {
  144. this.setState({
  145. status: 'Uploading...'
  146. })
  147. // this.uppy.upload()
  148. }
  149. showFilePicker (visible) {
  150. this.setState({
  151. isFilePickerVisible: visible
  152. })
  153. }
  154. render () {
  155. return (
  156. <View style={{
  157. flex: 1,
  158. backgroundColor: '#fff',
  159. alignItems: 'center',
  160. justifyContent: 'center'
  161. }}>
  162. <SelectAndUploadFileWithUppy
  163. state={this.state}
  164. selectPhotoTapped={this.selectPhotoTapped}
  165. showFilePicker={this.showFilePicker} />
  166. <FilePicker show={this.state.isFilePickerVisible} />
  167. </View>
  168. )
  169. }
  170. }
  171. function SelectAndUploadFileWithUppy (props) {
  172. return (
  173. <View style={{
  174. flex: 1,
  175. paddingVertical: 100
  176. }}>
  177. <Text>Uppy running in React Native</Text>
  178. <TouchableOpacity onPress={props.selectPhotoTapped}>
  179. { props.state.file === null
  180. ? <Text>Select a Photo</Text>
  181. : <Image
  182. style={{ width: 200, height: 200 }}
  183. source={{ uri: props.state.file.uri }} />
  184. }
  185. </TouchableOpacity>
  186. <Text>Status: {props.state.status}</Text>
  187. <Text>{props.state.progress} of {props.state.total}</Text>
  188. {/* <Button
  189. onPress={props.startUpload}
  190. title="Start Upload"
  191. color="#841584"
  192. accessibilityLabel="Start uploading a file"
  193. /> */}
  194. <TouchableHighlight
  195. onPress={() => {
  196. props.showFilePicker(true)
  197. }}>
  198. <Text>Select files from Uppy</Text>
  199. </TouchableHighlight>
  200. </View>
  201. )
  202. }
  203. // const styles = StyleSheet.create({
  204. // container: {
  205. // flex: 1,
  206. // backgroundColor: '#fff',
  207. // alignItems: 'center',
  208. // justifyContent: 'center'
  209. // }
  210. // })