-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUpload.js
161 lines (142 loc) · 4.27 KB
/
Upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react'
import { throttle } from 'lodash'
import {
AppRegistry,
Button,
Platform,
StyleSheet,
Text,
View
} from 'react-native'
import Upload from 'react-native-background-upload'
import ImagePicker from 'react-native-image-picker'
export default class ReactNativeBackgroundUploadExample extends Component {
constructor(props) {
super(props)
this.state = {
isImagePickerShowing: false,
uploadId: null,
progress: null,
}
}
handleProgress = throttle((progress) => {
this.setState({ progress })
}, 200)
startUpload = (opts) => {
Upload.getFileInfo(opts.path).then((metadata) => {
const options = Object.assign({
method: 'POST',
headers: {
'content-type': metadata.mimeType // server requires a content-type header
}
}, opts)
Upload.startUpload(options).then((uploadId) => {
console.log(`Upload started with options: ${JSON.stringify(options)}`)
this.setState({ uploadId, progress: 0 })
Upload.addListener('progress', uploadId, (data) => {
this.handleProgress(+data.progress)
console.log(`Progress: ${data.progress}%`)
})
Upload.addListener('error', uploadId, (data) => {
console.log(`Error: ${data.error}%`)
})
Upload.addListener('completed', uploadId, (data) => {
console.log('Completed!')
})
}).catch(function(err) {
this.setState({ uploadId: null, progress: null })
console.log('Upload error!', err)
})
})
}
onPressUpload = (options) => {
if (this.state.isImagePickerShowing) {
return
}
this.setState({ isImagePickerShowing: true })
const imagePickerOptions = {
takePhotoButtonTitle: null,
title: 'Upload Media',
chooseFromLibraryButtonTitle: 'Choose From Library'
}
ImagePicker.showImagePicker(imagePickerOptions, (response) => {
let didChooseVideo = true
console.log('ImagePicker response: ', response)
const { customButton, didCancel, error, path, uri } = response
if (didCancel) {
didChooseVideo = false
}
if (error) {
console.warn('ImagePicker error:', response)
didChooseVideo = false
}
// TODO: Should this happen higher?
this.setState({ isImagePickerShowing: false })
if (!didChooseVideo) {
return
}
if (Platform.OS === 'android') {
if (path) { // Video is stored locally on the device
// TODO: What here?
this.startUpload(Object.assign({ path }, options))
} else { // Video is stored in google cloud
// TODO: What here?
this.props.onVideoNotFound()
}
} else {
this.startUpload(Object.assign({ path: uri }, options))
}
})
}
cancelUpload = () => {
if (!this.state.uploadid) {
console.log('Nothing to cancel!')
return
}
Upload.cancelUpload(this.state.uploadId).then((props) => {
console.log(`Upload ${this.state.uploadId} canceled`)
this.setState({ uploadId: null, progress: null })
})
}
render() {
return (
<View>
<View>
<Button
title="Tap To Upload Raw"
onPress={() => this.onPressUpload({
url: 'http://localhost:3000/upload_raw',
type: 'raw'
})}
/>
<View/>
<Button
title="Tap To Upload Multipart"
onPress={() => this.onPressUpload({
url: 'http://localhost:3000/upload_multipart',
field: 'uploaded_media',
type: 'multipart'
})}
/>
<View style={{ height: 32 }}/>
<Text style={{ textAlign: 'center' }}>
{ `Current Upload ID: ${this.state.uploadId === null ? 'none' : this.state.uploadId}` }
</Text>
<Text style={{ textAlign: 'center' }}>
{ `Progress: ${this.state.progress === null ? 'none' : `${this.state.progress}%`}` }
</Text>
<View/>
<Button
title="Tap to Cancel Upload"
onPress={this.cancelUpload}
/>
</View>
</View>
)
}
}