Manual server file uploads implemented
This commit is contained in:
parent
c8308664d3
commit
70b5285e16
10 changed files with 329 additions and 26 deletions
|
|
@ -15,18 +15,62 @@ export class API {
|
|||
const res = await fetch(path, { method, headers, body })
|
||||
// An HTTP communication level error occured.
|
||||
if (!res.ok || res.status != 200)
|
||||
throw new Error('HTTP error', { cause: { type: 'http', error: res, }})
|
||||
throw new Error('HTTP error', { cause: { type: 'http', error: res, } })
|
||||
|
||||
// Application level response are handled here.
|
||||
const json = await res.json()
|
||||
if (!json.OK)
|
||||
throw new Error(json.Error, { cause: { type: 'application', application: json, }})
|
||||
throw new Error(json.Error, { cause: { type: 'application', application: json, } })
|
||||
|
||||
return json
|
||||
|
||||
} catch (err) {
|
||||
// Catch any other errors from fetch.
|
||||
throw new Error(err.message, { cause: { type: 'http', error: err, }})
|
||||
throw new Error(err.message, { cause: { type: 'http', error: err, } })
|
||||
}
|
||||
}
|
||||
|
||||
// Sends a block of binary data to server.
|
||||
static async upload(uuid, data, sentBytes, file) {
|
||||
try {
|
||||
const path = `/sync/file/upload/${uuid}`
|
||||
const headers = {
|
||||
'X-File-Sent-Bytes': sentBytes,
|
||||
'X-File-Total-Bytes': file.size,
|
||||
}
|
||||
|
||||
// Metadata is needed for the database.
|
||||
// No need to send it every block.
|
||||
if (sentBytes === 0) {
|
||||
headers['X-File-Name'] = file.name
|
||||
headers['X-File-Size'] = file.size
|
||||
headers['X-File-Type'] = file.type
|
||||
headers['X-File-Modified'] = file.lastModified
|
||||
}
|
||||
|
||||
// Authentication is done with a bearer token.
|
||||
// Here provided to the backend if set.
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) {
|
||||
headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
|
||||
const res = await fetch(path, { method: 'POST', headers, body: data })
|
||||
// An HTTP communication level error occured.
|
||||
if (!res.ok || res.status != 200)
|
||||
throw new Error('HTTP error', { cause: { type: 'http', error: res, } })
|
||||
|
||||
// Application level response are handled here.
|
||||
const json = await res.json()
|
||||
if (!json.OK)
|
||||
throw new Error(json.Error, { cause: { type: 'application', application: json, } })
|
||||
|
||||
return json
|
||||
|
||||
} catch (err) {
|
||||
// Catch any other errors from fetch.
|
||||
console.error(err)
|
||||
throw new Error(err, { cause: { type: 'http', error: err } })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue