|
|
|
import 'babel-polyfill';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const endPoints = {
|
|
|
|
get upload () {
|
|
|
|
return '/api/upload'
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Upload {
|
|
|
|
|
|
|
|
constructor(filesMetadata, fileObjects) {
|
|
|
|
this.filesMetadata = Array.from(filesMetadata)
|
|
|
|
this.fileObjects = fileObjects
|
|
|
|
this.timestamp = new Date()
|
|
|
|
}
|
|
|
|
|
|
|
|
get uploadMetadata() {
|
|
|
|
|
|
|
|
return {
|
|
|
|
files: this.filesMetadata,
|
|
|
|
timestamp: this.timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async create() {
|
|
|
|
let req = new Request(endPoints.upload , {
|
|
|
|
method: 'POST',
|
|
|
|
credentials: 'same-origin',
|
|
|
|
body: JSON.stringify(this.uploadMetadata)
|
|
|
|
})
|
|
|
|
|
|
|
|
//let result = await fetch(req);
|
|
|
|
let res = await fetch(req)
|
|
|
|
.catch((e) => { console.error(e) })
|
|
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw(`${res.status}: ` + (await res.json()).error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json().then(data => {
|
|
|
|
let { result: { id: id } } = data
|
|
|
|
this.uploadId = id
|
|
|
|
return data
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async send(){
|
|
|
|
console.log(`Sending with id ${this.uploadId}`)
|
|
|
|
|
|
|
|
let formData = new FormData();
|
|
|
|
for (let file of this.fileObjects) {
|
|
|
|
formData.append('upload[]', file)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let req = new Request(endPoints.upload + '/' + this.uploadId, {
|
|
|
|
method: 'PUT',
|
|
|
|
credentials: 'same-origin',
|
|
|
|
body: formData
|
|
|
|
})
|
|
|
|
|
|
|
|
let res = await fetch(req)
|
|
|
|
.catch((e) => {console.error(e)})
|
|
|
|
|
|
|
|
if (!res.ok){
|
|
|
|
throw(`${res.status}: ` + (await res.json()).error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
endPoints: endPoints,
|
|
|
|
Upload: Upload,
|
|
|
|
}
|