App.js 5.8 KB

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