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
|
import io from "socket.io-client"
class APIConnector {
constructor(url) {
this.url = url ?? ""
this.socket = io(url)
this.assetIndex = 0
this.movement = 0
this.assets = null
this.currentAsset = null
this.seekCallbacks = []
this.socket.on("seek", e => {
this.assetIndex = e.asset_index
this.movement = e.movement
this.assets = e.assets
this.currentAsset = e.current_asset
for (const cb of this.seekCallbacks)
cb()
})
this.downloadAnchor = document.createElement("a")
this.downloadAnchor.classList = "hidden"
document.body.appendChild(this.downloadAnchor)
}
fetch(endpoint, c) {
return fetch(this.url + "/api" + endpoint, c ?? {})
.then(response => {
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.json()
})
.then(data => {
return data
})
.catch(error => {
console.error("Fetch error:", error)
return null
})
}
async assetDownload(key) {
const filename = await this.assetFileName(key)
fetch(this.assetFullsizeSrc(key))
.then(response => {
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`)
return response.blob()
})
.then(blob => {
const blobUrl = URL.createObjectURL(blob)
this.downloadAnchor.href = blobUrl
this.downloadAnchor.download = filename
this.downloadAnchor.click()
URL.revokeObjectURL(blobUrl)
})
.catch(error => {
console.error("Fetch error:", error)
return null
})
}
post(endpoint, body) {
return this.fetch(endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
})
}
seek(increment) {
this.socket.emit("seek", increment)
}
fetchAlbums() { return this.fetch("/albums") }
fetchConfig() { return this.fetch("/config") }
updateAlbums(albums) { return this.post("/albums/update", albums) }
updateConfig(config) { return this.post("/config/update", config) }
albumSrc(key) { return `${this.url}/api/redirect/albums/${key}` }
assetPreviewSrc(key) { return `${this.url}/api/asset/${key}` }
assetThumbnailSrc(key) { return `${this.url}/api/asset/${key}/thumbnail` }
assetFullsizeSrc(key) { return `${this.url}/api/asset/${key}/fullsize` }
assetFileName(key) { return this.fetch(`/asset/${key}/filename`).then(d => d.filename) }
}
const apiConnector = new APIConnector(window.location.origin)
export default apiConnector
|