Make PhotoPanel bound

This commit is contained in:
John Lyon-Smith
2018-04-26 14:11:12 -07:00
parent 109e9f4d3d
commit 71cec6088a
10 changed files with 327 additions and 190 deletions

View File

@@ -137,7 +137,6 @@ class API extends EventEmitter {
this._apiURL = url
this._baseURL = parts[0] + "//" + parts[1]
this._secure = parts[0] === "https:"
if (parts.length === 3) {
this._apiPath = "/" + parts[2]
@@ -155,8 +154,8 @@ class API extends EventEmitter {
return this._apiPath
}
get secure() {
return this._secure
get apiURL() {
return this._apiURL
}
get backend() {
@@ -178,21 +177,13 @@ class API extends EventEmitter {
}
}
makeImageUrl(id, size) {
if (id) {
return this.apiPath + "/assets/" + id + "?access_token=" + this.token
} else if (size && size.width && size.height) {
return `${this.apiPath}/placeholders/${size.width}x${
size.height
}?access_token=${this.token}`
} else {
return null
}
makeImageUrl(id) {
return this._apiURL + "/assets/" + id + ".jpg?access_token=" + this.token
}
makeAssetUrl(id) {
return id
? this.apiPath + "/assets/" + id + "?access_token=" + this.token
? this._apiURL + "/assets/" + id + "?access_token=" + this.token
: null
}
@@ -218,10 +209,17 @@ class API extends EventEmitter {
headers.set("Authorization", "Bearer " + this.token)
}
if (method === "POST" || method === "PUT") {
if (requestOptions.binary) {
headers.set("Content-Type", "application/octet-stream")
headers.set("Content-Length", requestOptions.binary.length)
headers.set("Range", "byte " + requestOptions.binary.offset)
if (requestOptions.raw) {
const isBase64 = requestOptions.raw.base64
headers.set(
"Content-Type",
isBase64 ? "application/base64" : "application/octet-stream"
)
headers.set("Content-Length", requestOptions.raw.length)
headers.set(
"Content-Range",
(isBase64 ? "base64" : "byte") + " " + requestOptions.raw.offset
)
fetchOptions.body = requestBody
} else {
headers.set("Content-Type", "application/json")
@@ -238,7 +236,7 @@ class API extends EventEmitter {
.then((res) => {
return Promise.all([
Promise.resolve(res),
requestOptions.binary && method === "GET" ? res.blob() : res.json(),
requestOptions.raw && method === "GET" ? res.blob() : res.json(),
])
})
.then((arr) => {
@@ -427,33 +425,29 @@ class API extends EventEmitter {
return promise
}
upload(file, progressCallback) {
upload(data, progressCallback) {
return new Promise((resolve, reject) => {
const chunkSize = 32 * 1024
let reader = new FileReader()
const fileSize = file.size
const numberOfChunks = Math.ceil(fileSize / chunkSize)
const uploadSize = data.length
const numberOfChunks = Math.ceil(uploadSize / chunkSize)
let chunk = 0
let uploadId = null
reader.onload = (e) => {
const buffer = e.target.result
const bytesRead = buffer.byteLength
const uploadNextChunk = () => {
const start = chunk * chunkSize
const end = Math.min(uploadSize, start + chunkSize)
this.post("/assets/upload/" + uploadId, buffer, {
binary: { offset: chunk * chunkSize, length: bytesRead },
this.post("/assets/upload/" + uploadId, data.slice(start, end), {
raw: { base64: true, length: chunkSize, offset: start },
})
.then((uploadData) => {
chunk++
if (!progressCallback(uploadData)) {
return Promise.reject(new Error("Upload was canceled"))
}
if (chunk < numberOfChunks) {
let start = chunk * chunkSize
let end = Math.min(fileSize, start + chunkSize)
reader.readAsArrayBuffer(file.slice(start, end))
} else {
if (progressCallback && !progressCallback(uploadData)) {
reject(new Error("Upload was canceled"))
} else if (chunk >= numberOfChunks) {
resolve(uploadData)
} else {
uploadNextChunk()
}
})
.catch((err) => {
@@ -462,14 +456,14 @@ class API extends EventEmitter {
}
this.post("/assets/upload", {
fileName: file.name,
fileSize,
contentType: file.type,
uploadSize,
contentType: "image/jpeg",
chunkContentType: "application/base64",
numberOfChunks,
})
.then((uploadData) => {
uploadId = uploadData.uploadId
reader.readAsArrayBuffer(file.slice(0, chunkSize))
uploadNextChunk()
})
.catch((err) => {
reject(err)