102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
|
package internal
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"git.velvettear.de/velvettear/loggo"
|
||
|
"git.velvettear.de/velvettear/slideshow/internal/config"
|
||
|
)
|
||
|
|
||
|
// get the endpoint url of the api
|
||
|
func getEndpointUrl(endpoint string) string {
|
||
|
url := config.ApiAddress
|
||
|
if !strings.HasSuffix(url, "/") && endpoint != "/" {
|
||
|
url += "/"
|
||
|
}
|
||
|
if len(endpoint) > 0 {
|
||
|
url += endpoint
|
||
|
}
|
||
|
if !strings.HasSuffix(url, "/") {
|
||
|
url += "/"
|
||
|
}
|
||
|
return url
|
||
|
}
|
||
|
|
||
|
// request the name of an image from the api
|
||
|
func requestImageName() (string, error) {
|
||
|
timestamp := time.Now().UnixMilli()
|
||
|
var name string
|
||
|
url := getEndpointUrl("/")
|
||
|
request, error := http.NewRequest("GET", url, nil)
|
||
|
if error != nil {
|
||
|
return name, error
|
||
|
}
|
||
|
client := http.Client{}
|
||
|
response, error := client.Do(request)
|
||
|
if error != nil {
|
||
|
return name, error
|
||
|
}
|
||
|
bytes, error := io.ReadAll(response.Body)
|
||
|
if error != nil {
|
||
|
return name, error
|
||
|
}
|
||
|
tmp := make(map[string]interface{})
|
||
|
error = json.Unmarshal(bytes, &tmp)
|
||
|
if error != nil {
|
||
|
return name, error
|
||
|
}
|
||
|
name = tmp["Content"].(string)
|
||
|
loggo.DebugTimed("successfully requested image '"+name+"'", timestamp, "url: "+url)
|
||
|
return name, nil
|
||
|
}
|
||
|
|
||
|
// request the data stream of an image by name from the api
|
||
|
func requestImageData(name string) (io.ReadCloser, error) {
|
||
|
timestamp := time.Now().UnixMilli()
|
||
|
var stream io.ReadCloser
|
||
|
url := getEndpointUrl("/image/") + name
|
||
|
request, error := http.NewRequest("GET", url, nil)
|
||
|
if error != nil {
|
||
|
return stream, error
|
||
|
}
|
||
|
queryParameters := request.URL.Query()
|
||
|
if config.IsResolutionSet() {
|
||
|
queryParameters.Set("resolution", config.Resolution)
|
||
|
}
|
||
|
request.URL.RawQuery = queryParameters.Encode()
|
||
|
client := http.Client{}
|
||
|
response, error := client.Do(request)
|
||
|
if error != nil {
|
||
|
return stream, error
|
||
|
}
|
||
|
loggo.DebugTimed("successfully requested data stream for image '"+name+"'", timestamp)
|
||
|
return response.Body, nil
|
||
|
}
|
||
|
|
||
|
// request the color palette of an image by name from the api as stream
|
||
|
func requestColorPalette(name string) (io.ReadCloser, error) {
|
||
|
timestamp := time.Now().UnixMilli()
|
||
|
var stream io.ReadCloser
|
||
|
url := getEndpointUrl("/palette/") + name
|
||
|
request, error := http.NewRequest("GET", url, nil)
|
||
|
if error != nil {
|
||
|
return stream, error
|
||
|
}
|
||
|
queryParameters := request.URL.Query()
|
||
|
queryParameters.Set("colors", strconv.Itoa(config.PaletteColors))
|
||
|
queryParameters.Set("algorithm", config.PaletteAlgorithm)
|
||
|
request.URL.RawQuery = queryParameters.Encode()
|
||
|
client := http.Client{}
|
||
|
response, error := client.Do(request)
|
||
|
if error != nil {
|
||
|
return stream, error
|
||
|
}
|
||
|
loggo.DebugTimed("successfully requested color palette stream for image '"+name+"'", timestamp)
|
||
|
return response.Body, nil
|
||
|
}
|