-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathindex.js
124 lines (109 loc) · 3.41 KB
/
index.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
let upload = null
let uploadIsRunning = false
let file = null
const uploadButton = document.querySelector('#js-upload-button')
const fileInput = document.querySelector('#js-upload-file')
const fileLink = document.querySelector('#js-file-link')
const progressBar = document.querySelector('#js-upload-progress')
const progressText = document.querySelector('#js-upload-progress-text')
const uploadLink = document.querySelector('#js-upload-link')
fileInput.addEventListener('click', openFilePicker)
function resetUpload() {
if (upload) {
upload.abort()
upload = null
}
uploadButton.textContent = 'Start Upload'
uploadLink.textContent = 'not available yet'
progressText.textContent = ''
progressBar.removeAttribute('value')
}
uploadButton.addEventListener('click', toggleUpload)
function openFilePicker() {
resetUpload()
const options = {
// Camera is a global cordova-specific object used for configuring camera access.
// See https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/#module_Camera
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
encodingType: Camera.EncodingType.JPEG,
mediaType: Camera.MediaType.PICTURE,
// Allow simple editing of image before selection.
allowEdit: true,
// Rotate the image to correct for the orientation of the device during
// capture to fix orientation quirks on Android
correctOrientation: true,
}
navigator.camera.getPicture(
getFileEntry,
(error) => {
window.alert(`Unable to obtain picture: ${error}`)
},
options,
)
}
function getFileEntry(imgUri) {
window.resolveLocalFileSystemURL(
imgUri,
(fileEntry) => {
fileEntry.file((fileObj) => {
file = fileObj
fileLink.textContent = file.name
})
},
(error) => {
window.alert(`Could not create FileEntry: ${error}`)
},
)
}
function toggleUpload() {
if (!upload) {
if (!file) return
const options = {
endpoint: 'https://tusd.tusdemo.net/files/',
retryDelays: [0, 1000, 3000, 5000],
metadata: {
filename: file.name,
filetype: file.type,
},
onError(error) {
if (error.originalRequest) {
if (window.confirm(`Failed because: ${error}\nDo you want to retry?`)) {
upload.start()
uploadIsRunning = true
return
}
} else {
window.alert(`Failed because: ${error}`)
}
resetUpload()
},
onProgress(bytesUploaded, bytesTotal) {
const progress = bytesUploaded / bytesTotal
const percentage = `${(progress * 100).toFixed(2)}%`
progressBar.value = progress
progressText.textContent = percentage
},
onSuccess() {
const anchor = document.createElement('a')
anchor.textContent = `Download ${upload.file.name} (${upload.file.size} bytes)`
anchor.target = '_blank'
anchor.href = upload.url
uploadLink.innerHTML = ''
uploadLink.appendChild(anchor)
},
}
upload = new tus.Upload(file, options)
upload.start()
uploadIsRunning = true
uploadButton.textContent = 'Pause Upload'
} else if (uploadIsRunning) {
upload.abort()
uploadButton.textContent = 'Resume Upload'
uploadIsRunning = false
} else {
upload.start()
uploadButton.textContent = 'Pause Upload'
uploadIsRunning = true
}
}