|
|
|
@ -11,7 +11,7 @@ function getEndpoint (path) {
|
|
|
|
|
|
|
|
|
|
const endPoints = {
|
|
|
|
|
get upload () {
|
|
|
|
|
return getEndpoint('upload')
|
|
|
|
|
return getEndpoint('upload/')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -27,22 +27,24 @@ const apiEndpoint = function() {
|
|
|
|
|
|
|
|
|
|
class Upload {
|
|
|
|
|
|
|
|
|
|
constructor(files) {
|
|
|
|
|
this.files = Array.from(files)
|
|
|
|
|
constructor(filesMetadata, fileObjects) {
|
|
|
|
|
this.filesMetadata = Array.from(filesMetadata)
|
|
|
|
|
this.fileObjects = fileObjects
|
|
|
|
|
this.timestamp = new Date()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get payload() {
|
|
|
|
|
get uploadMetadata() {
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
files: this.files,
|
|
|
|
|
files: this.filesMetadata,
|
|
|
|
|
timestamp: this.timestamp,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create() {
|
|
|
|
|
let req = new Request(endPoints.upload, {
|
|
|
|
|
let req = new Request(endPoints.upload , {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(this.payload)
|
|
|
|
|
body: JSON.stringify(this.uploadMetadata)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//let result = await fetch(req);
|
|
|
|
@ -54,7 +56,35 @@ class Upload {
|
|
|
|
|
throw(`${res.status}: ` + (await res.json()).error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.json()
|
|
|
|
|
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) {
|
|
|
|
|
console.log(file)
|
|
|
|
|
formData.append('upload[]', file)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let req = new Request(endPoints.upload + this.uploadId, {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
body: formData
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
let res = await fetch(req)
|
|
|
|
|
.catch((e) => {console.error(e)})
|
|
|
|
|
|
|
|
|
|
if (!res.ok){
|
|
|
|
|
throw(`${res.status}: ` + (await res.json()).error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|