From 1ce57c114cf16337748bf5b6e0a0e125b87d7869 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Fri, 20 Jun 2025 22:31:41 -0500 Subject: asset download and album search + set --- src/connector.js | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'src/connector.js') diff --git a/src/connector.js b/src/connector.js index 29f0cff..5d8b62f 100644 --- a/src/connector.js +++ b/src/connector.js @@ -5,18 +5,24 @@ class APIConnector { this.url = url ?? "" this.socket = io(url) - this.asset_index = 0 + 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) { @@ -34,6 +40,28 @@ class APIConnector { }) } + async assetDownload(key) { + const filename = await this.assetFileName(key) + console.log(filename) + 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) + }) + } + post(endpoint, body) { return this.fetch(endpoint, { method: "POST", @@ -48,11 +76,15 @@ class APIConnector { 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}` } - assetSrc(key) { return `${this.url}/api/asset/${key}` } + 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("http://localhost:5000") -- cgit v1.2.3