From 34e024979fe2c7ed63ab948ae0f7bf701329b3f2 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 29 May 2024 18:06:34 -0500 Subject: [PATCH] Load images into UI --- albumCover.go | 44 +++++++++++++++++++++++++++---- frontend/src/routes/Home.tsx | 16 ++++++++--- frontend/wailsjs/go/main/App.d.ts | 2 ++ frontend/wailsjs/go/main/App.js | 4 +++ 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/albumCover.go b/albumCover.go index a54cd1f..63f1d52 100644 --- a/albumCover.go +++ b/albumCover.go @@ -21,6 +21,14 @@ type AlbumCoverInfo struct { var albumCoverCacheInfo = make(map[string]*AlbumCoverInfo) var cacheMutex = sync.RWMutex{} +func generateCacheFilename(albumId string) string { + albumCacheFile, err := xdg.CacheFile(fmt.Sprintf("%s/%s", appname, albumId)) + if err != nil { + panic(fmt.Sprint("error creating cacheFile url: ", err)) + } + return albumCacheFile +} + // Loads a single album cover and caches it in XDG_CACHE_HOME // First it checks if the cover already exists in XDG_CACHE_HOME // If it doesn't, loads it and stores it @@ -34,13 +42,10 @@ func loadAlbumCover(albumId string) { return } - albumCacheFile, err := xdg.CacheFile(fmt.Sprintf("%s/%s", appname, albumId)) - if err != nil { - panic(fmt.Sprint("error creating cacheFile url: ", err)) - } + albumCacheFile := generateCacheFilename(albumId) // Attempt to read file - if _, err = os.Stat(albumCacheFile); err != nil { + if _, err := os.Stat(albumCacheFile); err == nil { // File exists log.Print("album cover: cache hit (disk): ", albumId) cacheMutex.Lock() @@ -90,6 +95,7 @@ func loadAlbumCover(albumId string) { imgBytes := response.Body() // Write the image to cache + log.Printf("write %s: %+v", albumId, imgBytes[:10]) err = os.WriteFile(albumCacheFile, imgBytes, 0644) if err != nil { coverInfo.Error = errors.New("error writing album cover to disk") @@ -99,3 +105,31 @@ func loadAlbumCover(albumId string) { log.Print("Loading albumCover for ", albumId, " successful") } + +// Tries to load the album cover +func (a *App) GetAlbumCover(albumId string) ([]byte, error) { + cacheMutex.Lock() + coverInfo, ok := albumCoverCacheInfo[albumId] + cacheMutex.Unlock() + if !ok { + panic("Illegal state: Tried to load an album, but it wasn't on memory cache") + } + + coverInfo.WaitGroup.Wait() + + if coverInfo.Error != nil { + return nil, coverInfo.Error + } + + // Read the file + filename := generateCacheFilename(albumId) + log.Print("reading: ", filename) + bytes, err := os.ReadFile(filename) + if err != nil { + log.Fatal("error getting album cover: ", err) + return nil, errors.New("error reading cover file") + } + + log.Printf("%s : %+v", albumId, bytes[:10]) + return bytes, nil +} diff --git a/frontend/src/routes/Home.tsx b/frontend/src/routes/Home.tsx index 03216d1..737cbbe 100644 --- a/frontend/src/routes/Home.tsx +++ b/frontend/src/routes/Home.tsx @@ -1,5 +1,5 @@ -import { For, createResource, createSignal, onMount } from "solid-js"; -import { GetRandomAlbums } from "../../wailsjs/go/main/App"; +import { For, createMemo, createResource, createSignal, onMount } from "solid-js"; +import { GetAlbumCover, GetRandomAlbums } from "../../wailsjs/go/main/App"; import { main } from "../../wailsjs/go/models"; export function Home() { @@ -24,11 +24,21 @@ export function Home() { } function Album(props: { album: main.Album }) { + const [coverBytes] = createResource(async() => await GetAlbumCover(props.album.id)); + + const base64Image = createMemo(() => { + if (coverBytes.state !== "ready") return ""; + + // At runtime this is a string, not a number array + const bytes = coverBytes() as unknown as string; + return `data:;base64,${bytes}`; + }); + return (

diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 9b5c20e..d3471c9 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -2,6 +2,8 @@ // This file is automatically generated. DO NOT EDIT import {main} from '../models'; +export function GetAlbumCover(arg1:string):Promise>; + export function GetRandomAlbums():Promise>; export function Greet(arg1:string):Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index 4a96693..e757194 100755 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -2,6 +2,10 @@ // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT +export function GetAlbumCover(arg1) { + return window['go']['main']['App']['GetAlbumCover'](arg1); +} + export function GetRandomAlbums() { return window['go']['main']['App']['GetRandomAlbums'](); }