This commit is contained in:
1 parent
9a6ec940f7
commit
fd32419371
114 files changed
+21997
-96
No files matched your search
@@ -0,0 +1,195 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"mediacollection/internal/config"
|
||||
)
|
||||
|
||||
type kioskMessage struct {
|
||||
Type string `json:"type"`
|
||||
QueueMode string `json:"queueMode,omitempty"`
|
||||
DisplayDuration int64 `json:"displayDuration,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) kioskWebsocket(w http.ResponseWriter, r *http.Request) {
|
||||
options := s.websocketAcceptOptions(r)
|
||||
conn, err := websocket.Accept(w, r, options)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close(websocket.StatusNormalClosure, "")
|
||||
|
||||
updates := make(chan []byte, 16)
|
||||
s.kioskMu.Lock()
|
||||
s.kioskClients[updates] = struct{}{}
|
||||
latest := s.kioskLatest
|
||||
s.kioskMu.Unlock()
|
||||
defer func() {
|
||||
s.kioskMu.Lock()
|
||||
delete(s.kioskClients, updates)
|
||||
s.kioskMu.Unlock()
|
||||
}()
|
||||
|
||||
if latest != "" {
|
||||
s.kioskSend(updates, map[string]any{"type": "new-image", "path": latest})
|
||||
}
|
||||
s.kioskSend(updates, map[string]any{
|
||||
"type": "settings-updated",
|
||||
"queueMode": s.settingValue(r.Context(), "kiosk_queue_mode", "client"),
|
||||
"serverDisplayDuration": kioskDuration(s.settingValue(r.Context(), "server_display_duration", "5000")),
|
||||
})
|
||||
|
||||
ctx := r.Context()
|
||||
writeCtx, cancelWrite := context.WithCancel(ctx)
|
||||
defer cancelWrite()
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-writeCtx.Done():
|
||||
return
|
||||
case data := <-updates:
|
||||
if err := conn.Write(writeCtx, websocket.MessageText, data); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
s.startKioskPoll()
|
||||
for {
|
||||
messageType, payload, readErr := conn.Read(ctx)
|
||||
if readErr != nil {
|
||||
return
|
||||
}
|
||||
if messageType != websocket.MessageText {
|
||||
continue
|
||||
}
|
||||
var message kioskMessage
|
||||
if json.Unmarshal(payload, &message) != nil || message.Type != "update-settings" {
|
||||
continue
|
||||
}
|
||||
if message.QueueMode != "" && (message.QueueMode == "client" || message.QueueMode == "server") {
|
||||
_ = s.saveSetting(ctx, "kiosk_queue_mode", message.QueueMode)
|
||||
}
|
||||
if message.DisplayDuration >= 0 && message.DisplayDuration <= 24*60*60*1000 {
|
||||
_ = s.saveSetting(ctx, "server_display_duration", formatInt(message.DisplayDuration))
|
||||
}
|
||||
s.kioskBroadcast(map[string]any{
|
||||
"type": "settings-updated",
|
||||
"queueMode": s.settingValue(ctx, "kiosk_queue_mode", "client"),
|
||||
"serverDisplayDuration": kioskDuration(s.settingValue(ctx, "server_display_duration", "5000")),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startKioskPoll() {
|
||||
s.kioskMu.Lock()
|
||||
if s.kioskPolling {
|
||||
s.kioskMu.Unlock()
|
||||
return
|
||||
}
|
||||
s.kioskPolling = true
|
||||
s.kioskMu.Unlock()
|
||||
go s.kioskPoll(context.Background())
|
||||
}
|
||||
|
||||
func (s *Server) kioskPoll(ctx context.Context) {
|
||||
if s.cfg.Service != config.Kiosk {
|
||||
return
|
||||
}
|
||||
ticker := time.NewTicker(3 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
s.kioskPollOnce(ctx)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) kioskPollOnce(ctx context.Context) {
|
||||
files, err := s.servantListFiles(ctx, "", false, 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
images := make([]fileEntry, 0, len(files))
|
||||
for _, file := range files {
|
||||
if file.Type == "image" {
|
||||
images = append(images, file)
|
||||
}
|
||||
}
|
||||
if len(images) == 0 {
|
||||
return
|
||||
}
|
||||
sort.Slice(images, func(i, j int) bool { return images[i].Mtime > images[j].Mtime })
|
||||
latest := images[0].Rel
|
||||
s.kioskMu.Lock()
|
||||
changed := latest != s.kioskLatest
|
||||
s.kioskLatest = latest
|
||||
s.kioskMu.Unlock()
|
||||
if changed {
|
||||
s.kioskBroadcast(map[string]any{"type": "new-image", "path": latest})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) kioskBroadcast(message any) {
|
||||
data, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.kioskMu.Lock()
|
||||
defer s.kioskMu.Unlock()
|
||||
for client := range s.kioskClients {
|
||||
select {
|
||||
case client <- data:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) kioskSend(client chan []byte, message any) {
|
||||
data, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case client <- data:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) kioskImage(w http.ResponseWriter, r *http.Request) {
|
||||
relative := chi.URLParam(r, "*")
|
||||
if relative == "" {
|
||||
relative = strings.TrimPrefix(r.URL.Path, "/api/images/")
|
||||
}
|
||||
if !safeRelative(relative) || !isImagePath(relative) {
|
||||
writeError(w, http.StatusNotFound, "image not found")
|
||||
return
|
||||
}
|
||||
s.proxyServant(w, r, "/media/"+urlPath(relative))
|
||||
}
|
||||
|
||||
func kioskDuration(value string) int64 {
|
||||
var duration int64
|
||||
if _, err := fmt.Sscanf(value, "%d", &duration); err != nil || duration < 0 {
|
||||
return 5000
|
||||
}
|
||||
return duration
|
||||
}
|
||||
|
||||
func formatInt(value int64) string {
|
||||
return strconv.FormatInt(value, 10)
|
||||
}
|
||||
Reference in new issue
Block a user