Add jukebox_maker web app v1.0
Go web application for filling USB drives with media files. Runs in Docker on Unraid with /media, /mnt/usb, /config volumes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -25,3 +25,9 @@ go.work.sum
|
|||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
# Build output
|
||||||
|
/jukebox
|
||||||
|
|
||||||
|
# Temp copy files
|
||||||
|
*.juketmp
|
||||||
|
|
||||||
|
|||||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "bible"]
|
||||||
|
path = bible
|
||||||
|
url = https://git.mchus.pro/mchus/bible.git
|
||||||
22
Dockerfile
Normal file
22
Dockerfile
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
FROM golang:1.22-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /src
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" \
|
||||||
|
-o /out/jukebox ./cmd/jukebox
|
||||||
|
|
||||||
|
FROM alpine:3.19
|
||||||
|
|
||||||
|
RUN apk add --no-cache tzdata ca-certificates
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder /out/jukebox .
|
||||||
|
|
||||||
|
VOLUME ["/media", "/mnt/usb", "/config"]
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
ENTRYPOINT ["/app/jukebox"]
|
||||||
|
CMD ["--config", "/config/config.json", "--addr", ":8080", "--media", "/media", "--mount", "/mnt/usb"]
|
||||||
1
bible
Submodule
1
bible
Submodule
Submodule bible added at d2600f1279
149
cmd/jukebox/main.go
Normal file
149
cmd/jukebox/main.go
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/api"
|
||||||
|
"jukebox_maker/internal/config"
|
||||||
|
"jukebox_maker/internal/copier"
|
||||||
|
"jukebox_maker/internal/db"
|
||||||
|
"jukebox_maker/internal/disk"
|
||||||
|
"jukebox_maker/internal/task"
|
||||||
|
"jukebox_maker/internal/watcher"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
configPath := flag.String("config", "/config/config.json", "path to config file")
|
||||||
|
addr := flag.String("addr", ":8080", "HTTP listen address")
|
||||||
|
mediaPath := flag.String("media", "/media", "path to media source directory")
|
||||||
|
mountPath := flag.String("mount", "/mnt/usb", "path to USB mount point")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
cfg, err := config.Load(*configPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("load config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
taskStore := task.NewStore()
|
||||||
|
cp := copier.New(taskStore)
|
||||||
|
|
||||||
|
var activeDB *db.DB
|
||||||
|
var activeDiskID string
|
||||||
|
|
||||||
|
openDiskDB := func(info disk.DiskInfo) {
|
||||||
|
if activeDiskID == info.DiskID {
|
||||||
|
return // already open for this disk
|
||||||
|
}
|
||||||
|
if activeDB != nil {
|
||||||
|
activeDB.Close()
|
||||||
|
activeDB = nil
|
||||||
|
activeDiskID = ""
|
||||||
|
}
|
||||||
|
d, err := db.Open(disk.DBPath(info.MountPath))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("open disk DB: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
activeDB = d
|
||||||
|
activeDiskID = info.DiskID
|
||||||
|
cp.SetDB(d)
|
||||||
|
log.Printf("disk DB opened for %s", info.DiskID)
|
||||||
|
}
|
||||||
|
|
||||||
|
closeDiskDB := func() {
|
||||||
|
if activeDB != nil {
|
||||||
|
activeDB.Close()
|
||||||
|
activeDB = nil
|
||||||
|
activeDiskID = ""
|
||||||
|
cp.SetDB(nil)
|
||||||
|
log.Println("disk DB closed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w := watcher.New(*mountPath, 5*time.Second, func(ev watcher.DiskEvent) {
|
||||||
|
log.Printf("disk: %s -> %s", ev.Prev, ev.Info.State)
|
||||||
|
switch ev.Info.State {
|
||||||
|
case disk.DiskKnown:
|
||||||
|
openDiskDB(ev.Info)
|
||||||
|
if ev.Prev != disk.DiskKnown && cfg.AutoCopy {
|
||||||
|
triggerAutoCopy(cp, cfg, ev.Info, *mediaPath)
|
||||||
|
}
|
||||||
|
case disk.DiskAbsent:
|
||||||
|
closeDiskDB()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Open DB immediately if disk already connected at startup
|
||||||
|
{
|
||||||
|
info, _ := disk.Probe(*mountPath)
|
||||||
|
if info.State == disk.DiskKnown {
|
||||||
|
openDiskDB(info)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
srv, err := api.New(api.Deps{
|
||||||
|
Config: cfg,
|
||||||
|
ConfigPath: *configPath,
|
||||||
|
Watcher: w,
|
||||||
|
Copier: cp,
|
||||||
|
Tasks: taskStore,
|
||||||
|
MediaPath: *mediaPath,
|
||||||
|
MountPath: *mountPath,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("init server: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
go w.Run(ctx)
|
||||||
|
|
||||||
|
httpSrv := &http.Server{Addr: *addr, Handler: srv}
|
||||||
|
go func() {
|
||||||
|
log.Printf("jukebox_maker listening on %s", *addr)
|
||||||
|
if err := httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
log.Fatalf("http: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
log.Println("shutting down…")
|
||||||
|
shutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
httpSrv.Shutdown(shutCtx)
|
||||||
|
closeDiskDB()
|
||||||
|
}
|
||||||
|
|
||||||
|
func triggerAutoCopy(cp *copier.Copier, cfg *config.Config, info disk.DiskInfo, mediaPath string) {
|
||||||
|
var sources []string
|
||||||
|
for _, s := range cfg.Sources {
|
||||||
|
if s.Enabled {
|
||||||
|
sources = append(sources, s.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(sources) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
_, err := cp.Start(context.Background(), copier.Options{
|
||||||
|
DiskID: info.DiskID,
|
||||||
|
MountPath: info.MountPath,
|
||||||
|
MediaPath: mediaPath,
|
||||||
|
EnabledSources: sources,
|
||||||
|
ReserveFreeGB: cfg.ReserveFreeGB,
|
||||||
|
OverwriteMode: cfg.OverwriteMode,
|
||||||
|
FileSelectMode: cfg.FileSelectMode,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("auto-copy: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
19
go.mod
Normal file
19
go.mod
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
module jukebox_maker
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
modernc.org/sqlite v1.49.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
golang.org/x/sys v0.42.0 // indirect
|
||||||
|
modernc.org/libc v1.72.0 // indirect
|
||||||
|
modernc.org/mathutil v1.7.1 // indirect
|
||||||
|
modernc.org/memory v1.11.0 // indirect
|
||||||
|
)
|
||||||
51
go.sum
Normal file
51
go.sum
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||||
|
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
|
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||||
|
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
|
||||||
|
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
|
||||||
|
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||||
|
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
|
||||||
|
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
|
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
|
||||||
|
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
|
||||||
|
modernc.org/cc/v4 v4.27.3 h1:uNCgn37E5U09mTv1XgskEVUJ8ADKpmFMPxzGJ0TSo+U=
|
||||||
|
modernc.org/cc/v4 v4.27.3/go.mod h1:3YjcbCqhoTTHPycJDRl2WZKKFj0nwcOIPBfEZK0Hdk8=
|
||||||
|
modernc.org/ccgo/v4 v4.32.4 h1:L5OB8rpEX4ZsXEQwGozRfJyJSFHbbNVOoQ59DU9/KuU=
|
||||||
|
modernc.org/ccgo/v4 v4.32.4/go.mod h1:lY7f+fiTDHfcv6YlRgSkxYfhs+UvOEEzj49jAn2TOx0=
|
||||||
|
modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM=
|
||||||
|
modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU=
|
||||||
|
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||||
|
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||||
|
modernc.org/gc/v3 v3.1.2 h1:ZtDCnhonXSZexk/AYsegNRV1lJGgaNZJuKjJSWKyEqo=
|
||||||
|
modernc.org/gc/v3 v3.1.2/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
|
||||||
|
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||||
|
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||||
|
modernc.org/libc v1.72.0 h1:IEu559v9a0XWjw0DPoVKtXpO2qt5NVLAnFaBbjq+n8c=
|
||||||
|
modernc.org/libc v1.72.0/go.mod h1:tTU8DL8A+XLVkEY3x5E/tO7s2Q/q42EtnNWda/L5QhQ=
|
||||||
|
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||||
|
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||||
|
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||||
|
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||||
|
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||||
|
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||||
|
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||||
|
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||||
|
modernc.org/sqlite v1.49.1 h1:dYGHTKcX1sJ+EQDnUzvz4TJ5GbuvhNJa8Fg6ElGx73U=
|
||||||
|
modernc.org/sqlite v1.49.1/go.mod h1:m0w8xhwYUVY3H6pSDwc3gkJ/irZT/0YEXwBlhaxQEew=
|
||||||
|
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||||
|
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||||
|
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||||
|
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||||
41
internal/api/handlers_config.go
Normal file
41
internal/api/handlers_config.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cfgMu sync.Mutex
|
||||||
|
|
||||||
|
func (s *Server) handleGetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cfgMu.Lock()
|
||||||
|
cfg := *s.deps.Config
|
||||||
|
cfgMu.Unlock()
|
||||||
|
jsonOK(w, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handlePutConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var incoming config.Config
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&incoming); err != nil {
|
||||||
|
jsonErr(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := incoming.Validate(); err != nil {
|
||||||
|
jsonErr(w, http.StatusUnprocessableEntity, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cfgMu.Lock()
|
||||||
|
*s.deps.Config = incoming
|
||||||
|
cfgMu.Unlock()
|
||||||
|
|
||||||
|
if err := config.Save(s.deps.ConfigPath, s.deps.Config); err != nil {
|
||||||
|
jsonErr(w, http.StatusInternalServerError, "failed to save config: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonOK(w, incoming)
|
||||||
|
}
|
||||||
68
internal/api/handlers_copy.go
Normal file
68
internal/api/handlers_copy.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/copier"
|
||||||
|
"jukebox_maker/internal/disk"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) handleCopyStart(w http.ResponseWriter, r *http.Request) {
|
||||||
|
diskInfo := s.deps.Watcher.CurrentDisk()
|
||||||
|
if diskInfo.State != disk.DiskKnown {
|
||||||
|
jsonErr(w, http.StatusUnprocessableEntity, "no known disk connected")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := s.deps.Config
|
||||||
|
var enabledSources []string
|
||||||
|
for _, src := range cfg.Sources {
|
||||||
|
if src.Enabled {
|
||||||
|
enabledSources = append(enabledSources, src.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(enabledSources) == 0 {
|
||||||
|
jsonErr(w, http.StatusUnprocessableEntity, "no sources enabled")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := copier.Options{
|
||||||
|
DiskID: diskInfo.DiskID,
|
||||||
|
MountPath: diskInfo.MountPath,
|
||||||
|
MediaPath: s.deps.MediaPath,
|
||||||
|
EnabledSources: enabledSources,
|
||||||
|
ReserveFreeGB: cfg.ReserveFreeGB,
|
||||||
|
OverwriteMode: cfg.OverwriteMode,
|
||||||
|
FileSelectMode: cfg.FileSelectMode,
|
||||||
|
}
|
||||||
|
|
||||||
|
taskID, err := s.deps.Copier.Start(context.Background(), opts)
|
||||||
|
if err != nil {
|
||||||
|
switch err.Error() {
|
||||||
|
case "copy already running":
|
||||||
|
jsonErr(w, http.StatusConflict, err.Error())
|
||||||
|
default:
|
||||||
|
jsonErr(w, http.StatusUnprocessableEntity, err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
jsonOK(w, map[string]string{"task_id": taskID})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleCopyCancel(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.deps.Copier.Cancel()
|
||||||
|
jsonOK(w, map[string]bool{"ok": true})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleTaskGet(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := r.PathValue("id")
|
||||||
|
t, ok := s.deps.Tasks.Get(id)
|
||||||
|
if !ok {
|
||||||
|
jsonErr(w, http.StatusNotFound, "task not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonOK(w, t)
|
||||||
|
}
|
||||||
34
internal/api/handlers_disk.go
Normal file
34
internal/api/handlers_disk.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/disk"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
|
info := s.deps.Watcher.CurrentDisk()
|
||||||
|
|
||||||
|
type response struct {
|
||||||
|
State disk.DiskState `json:"state"`
|
||||||
|
DiskID string `json:"disk_id"`
|
||||||
|
TotalBytes int64 `json:"total_bytes"`
|
||||||
|
FreeBytes int64 `json:"free_bytes"`
|
||||||
|
MountPath string `json:"mount_path"`
|
||||||
|
ActiveTaskID string `json:"active_task_id,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := response{
|
||||||
|
State: info.State,
|
||||||
|
DiskID: info.DiskID,
|
||||||
|
TotalBytes: info.TotalBytes,
|
||||||
|
FreeBytes: info.FreeBytes,
|
||||||
|
MountPath: info.MountPath,
|
||||||
|
}
|
||||||
|
|
||||||
|
if t, ok := s.deps.Tasks.ActiveTask(); ok {
|
||||||
|
resp.ActiveTaskID = t.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonOK(w, resp)
|
||||||
|
}
|
||||||
26
internal/api/handlers_sources.go
Normal file
26
internal/api/handlers_sources.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) handleSources(w http.ResponseWriter, r *http.Request) {
|
||||||
|
entries, err := os.ReadDir(s.deps.MediaPath)
|
||||||
|
if err != nil {
|
||||||
|
jsonOK(w, map[string][]string{"items": {}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var items []string
|
||||||
|
for _, e := range entries {
|
||||||
|
if e.IsDir() && e.Name()[0] != '.' {
|
||||||
|
items = append(items, e.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if items == nil {
|
||||||
|
items = []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonOK(w, map[string][]string{"items": items})
|
||||||
|
}
|
||||||
100
internal/api/server.go
Normal file
100
internal/api/server.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
webui "jukebox_maker/web"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/config"
|
||||||
|
"jukebox_maker/internal/copier"
|
||||||
|
"jukebox_maker/internal/task"
|
||||||
|
"jukebox_maker/internal/watcher"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Deps struct {
|
||||||
|
Config *config.Config
|
||||||
|
ConfigPath string
|
||||||
|
Watcher *watcher.Watcher
|
||||||
|
Copier *copier.Copier
|
||||||
|
Tasks *task.Store
|
||||||
|
MediaPath string
|
||||||
|
MountPath string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
deps Deps
|
||||||
|
dashboard *template.Template
|
||||||
|
settings *template.Template
|
||||||
|
mux *http.ServeMux
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(deps Deps) (*Server, error) {
|
||||||
|
dash, err := template.ParseFS(webui.FS, "templates/layout.html", "templates/dashboard.html")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
sett, err := template.ParseFS(webui.FS, "templates/layout.html", "templates/settings.html")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s := &Server{deps: deps, dashboard: dash, settings: sett, mux: http.NewServeMux()}
|
||||||
|
s.routes()
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.mux.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) routes() {
|
||||||
|
s.mux.Handle("GET /static/", http.FileServerFS(webui.FS))
|
||||||
|
|
||||||
|
s.mux.HandleFunc("GET /", s.handleDashboard)
|
||||||
|
s.mux.HandleFunc("GET /settings", s.handleSettings)
|
||||||
|
|
||||||
|
s.mux.HandleFunc("GET /health", s.handleHealth)
|
||||||
|
s.mux.HandleFunc("GET /api/disk", s.handleDiskStatus)
|
||||||
|
s.mux.HandleFunc("GET /api/sources", s.handleSources)
|
||||||
|
s.mux.HandleFunc("GET /api/config", s.handleGetConfig)
|
||||||
|
s.mux.HandleFunc("PUT /api/config", s.handlePutConfig)
|
||||||
|
s.mux.HandleFunc("POST /api/copy/start", s.handleCopyStart)
|
||||||
|
s.mux.HandleFunc("POST /api/copy/cancel", s.handleCopyCancel)
|
||||||
|
s.mux.HandleFunc("GET /api/tasks/{id}", s.handleTaskGet)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/" {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.render(w, s.dashboard, map[string]any{"Title": "Dashboard", "Page": "dashboard"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleSettings(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.render(w, s.settings, map[string]any{"Title": "Настройки", "Page": "settings"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||||
|
jsonOK(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) render(w http.ResponseWriter, tmpl *template.Template, data any) {
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
if err := tmpl.ExecuteTemplate(w, "layout", data); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsonOK(w http.ResponseWriter, v any) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsonErr(w http.ResponseWriter, code int, msg string) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(code)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"error": msg})
|
||||||
|
}
|
||||||
89
internal/config/config.go
Normal file
89
internal/config/config.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OverwriteMode string
|
||||||
|
type FileSelectMode string
|
||||||
|
|
||||||
|
const (
|
||||||
|
OverwriteSkip OverwriteMode = "skip"
|
||||||
|
OverwriteDelete OverwriteMode = "delete"
|
||||||
|
|
||||||
|
SelectNew FileSelectMode = "new"
|
||||||
|
SelectAll FileSelectMode = "all"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SourceFolder struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
ReserveFreeGB float64 `json:"reserve_free_gb"`
|
||||||
|
Sources []SourceFolder `json:"sources"`
|
||||||
|
OverwriteMode OverwriteMode `json:"overwrite_mode"`
|
||||||
|
FileSelectMode FileSelectMode `json:"file_select_mode"`
|
||||||
|
AutoCopy bool `json:"auto_copy"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaults() Config {
|
||||||
|
return Config{
|
||||||
|
ReserveFreeGB: 2.0,
|
||||||
|
OverwriteMode: OverwriteSkip,
|
||||||
|
FileSelectMode: SelectNew,
|
||||||
|
AutoCopy: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load(path string) (*Config, error) {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
cfg := defaults()
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cfg := defaults()
|
||||||
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Save(path string, cfg *Config) error {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := json.MarshalIndent(cfg, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tmp := path + ".tmp"
|
||||||
|
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.Rename(tmp, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) Validate() error {
|
||||||
|
if c.ReserveFreeGB < 0 {
|
||||||
|
return errors.New("reserve_free_gb must be >= 0")
|
||||||
|
}
|
||||||
|
switch c.OverwriteMode {
|
||||||
|
case OverwriteSkip, OverwriteDelete:
|
||||||
|
default:
|
||||||
|
return errors.New("overwrite_mode must be 'skip' or 'delete'")
|
||||||
|
}
|
||||||
|
switch c.FileSelectMode {
|
||||||
|
case SelectNew, SelectAll:
|
||||||
|
default:
|
||||||
|
return errors.New("file_select_mode must be 'new' or 'all'")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
280
internal/copier/copier.go
Normal file
280
internal/copier/copier.go
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
package copier
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/config"
|
||||||
|
"jukebox_maker/internal/db"
|
||||||
|
"jukebox_maker/internal/disk"
|
||||||
|
"jukebox_maker/internal/task"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
DiskID string
|
||||||
|
MountPath string
|
||||||
|
MediaPath string
|
||||||
|
EnabledSources []string
|
||||||
|
ReserveFreeGB float64
|
||||||
|
OverwriteMode config.OverwriteMode
|
||||||
|
FileSelectMode config.FileSelectMode
|
||||||
|
}
|
||||||
|
|
||||||
|
type Copier struct {
|
||||||
|
tasks *task.Store
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
cancel context.CancelFunc
|
||||||
|
|
||||||
|
dbMu sync.RWMutex
|
||||||
|
db *db.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(tasks *task.Store) *Copier {
|
||||||
|
return &Copier{tasks: tasks}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDB replaces the active disk database (called when a disk connects or disconnects).
|
||||||
|
func (c *Copier) SetDB(d *db.DB) {
|
||||||
|
c.dbMu.Lock()
|
||||||
|
c.db = d
|
||||||
|
c.dbMu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Copier) getDB() *db.DB {
|
||||||
|
c.dbMu.RLock()
|
||||||
|
defer c.dbMu.RUnlock()
|
||||||
|
return c.db
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Copier) Start(ctx context.Context, opts Options) (string, error) {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
|
if _, active := c.tasks.ActiveTask(); active {
|
||||||
|
return "", errors.New("copy already running")
|
||||||
|
}
|
||||||
|
|
||||||
|
database := c.getDB()
|
||||||
|
if database == nil {
|
||||||
|
return "", errors.New("no disk database available")
|
||||||
|
}
|
||||||
|
|
||||||
|
t := c.tasks.Create("copy")
|
||||||
|
copyCtx, cancel := context.WithCancel(ctx)
|
||||||
|
c.cancel = cancel
|
||||||
|
|
||||||
|
go c.run(copyCtx, t.ID, opts, database)
|
||||||
|
return t.ID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Copier) Cancel() {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
if c.cancel != nil {
|
||||||
|
c.cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Copier) run(ctx context.Context, taskID string, opts Options, database *db.DB) {
|
||||||
|
setStatus := func(s task.Status, msg string, prog int) {
|
||||||
|
c.tasks.Update(taskID, func(t *task.Task) {
|
||||||
|
t.Status = s
|
||||||
|
t.Message = msg
|
||||||
|
t.Progress = prog
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fail := func(err error) {
|
||||||
|
c.tasks.Update(taskID, func(t *task.Task) {
|
||||||
|
t.Status = task.StatusFailed
|
||||||
|
t.Error = err.Error()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
setStatus(task.StatusRunning, "Подготовка…", 0)
|
||||||
|
|
||||||
|
if opts.OverwriteMode == config.OverwriteDelete {
|
||||||
|
setStatus(task.StatusRunning, "Удаление данных с диска…", 0)
|
||||||
|
if err := deleteOurData(opts.MountPath); err != nil {
|
||||||
|
fail(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var copiedPaths map[string]struct{}
|
||||||
|
if opts.FileSelectMode == config.SelectNew {
|
||||||
|
setStatus(task.StatusRunning, "Загрузка истории…", 0)
|
||||||
|
var err error
|
||||||
|
copiedPaths, err = database.CopiedPaths(opts.DiskID)
|
||||||
|
if err != nil {
|
||||||
|
fail(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setStatus(task.StatusRunning, "Сканирование источников…", 0)
|
||||||
|
files, err := buildFileList(opts.MediaPath, opts.EnabledSources, copiedPaths)
|
||||||
|
if err != nil {
|
||||||
|
fail(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(files) == 0 {
|
||||||
|
setStatus(task.StatusSuccess, "Нет новых файлов для копирования.", 100)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, free, err := disk.DiskUsage(opts.MountPath)
|
||||||
|
if err != nil {
|
||||||
|
fail(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reserveBytes := int64(opts.ReserveFreeGB * 1e9)
|
||||||
|
available := free - reserveBytes
|
||||||
|
if available <= 0 {
|
||||||
|
setStatus(task.StatusSuccess, "Недостаточно свободного места на диске.", 100)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
total := len(files)
|
||||||
|
copied := 0
|
||||||
|
for i, f := range files {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
c.tasks.Update(taskID, func(t *task.Task) {
|
||||||
|
t.Status = task.StatusCanceled
|
||||||
|
t.Message = "Отменено"
|
||||||
|
})
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.size > available {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := fmt.Sprintf("Копирование %s (%d/%d)", filepath.Base(f.srcAbs), i+1, total)
|
||||||
|
prog := int(float64(i+1) / float64(total) * 100)
|
||||||
|
setStatus(task.StatusRunning, msg, prog)
|
||||||
|
|
||||||
|
dstAbs := filepath.Join(opts.MountPath, f.relPath)
|
||||||
|
if err := copyFile(ctx, f.srcAbs, dstAbs); err != nil {
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
c.tasks.Update(taskID, func(t *task.Task) {
|
||||||
|
t.Status = task.StatusCanceled
|
||||||
|
t.Message = "Отменено"
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
available -= f.size
|
||||||
|
copied++
|
||||||
|
_ = database.RecordCopy(db.CopyRecord{
|
||||||
|
DiskID: opts.DiskID,
|
||||||
|
SourcePath: f.relPath,
|
||||||
|
FileSize: f.size,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
setStatus(task.StatusSuccess, fmt.Sprintf("Готово. Скопировано файлов: %d.", copied), 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
type fileEntry struct {
|
||||||
|
srcAbs string
|
||||||
|
relPath string
|
||||||
|
size int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildFileList(mediaPath string, sources []string, skip map[string]struct{}) ([]fileEntry, error) {
|
||||||
|
var result []fileEntry
|
||||||
|
for _, src := range sources {
|
||||||
|
dir := filepath.Join(mediaPath, src)
|
||||||
|
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
|
||||||
|
if err != nil || d.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
rel, _ := filepath.Rel(mediaPath, path)
|
||||||
|
if _, skipped := skip[rel]; skipped {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
info, err := d.Info()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
result = append(result, fileEntry{srcAbs: path, relPath: rel, size: info.Size()})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteOurData(mountPath string) error {
|
||||||
|
entries, err := os.ReadDir(mountPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, e := range entries {
|
||||||
|
if e.Name() == ".jukebox" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := os.RemoveAll(filepath.Join(mountPath, e.Name())); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyFile(ctx context.Context, src, dst string) error {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
in, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer in.Close()
|
||||||
|
|
||||||
|
tmp := dst + ".juketmp"
|
||||||
|
out, err := os.Create(tmp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 512*1024)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
out.Close()
|
||||||
|
os.Remove(tmp)
|
||||||
|
return ctx.Err()
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
n, readErr := in.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
if _, werr := out.Write(buf[:n]); werr != nil {
|
||||||
|
out.Close()
|
||||||
|
os.Remove(tmp)
|
||||||
|
return werr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if errors.Is(readErr, io.EOF) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if readErr != nil {
|
||||||
|
out.Close()
|
||||||
|
os.Remove(tmp)
|
||||||
|
return readErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.Close()
|
||||||
|
return os.Rename(tmp, dst)
|
||||||
|
}
|
||||||
92
internal/db/db.go
Normal file
92
internal/db/db.go
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "modernc.org/sqlite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DB struct {
|
||||||
|
sql *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
type CopyRecord struct {
|
||||||
|
DiskID string
|
||||||
|
SourcePath string
|
||||||
|
FileSize int64
|
||||||
|
CopiedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func Open(path string) (*DB, error) {
|
||||||
|
conn, err := sql.Open("sqlite", path+"?_journal=WAL&_timeout=5000")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
conn.SetMaxOpenConns(1)
|
||||||
|
d := &DB{sql: conn}
|
||||||
|
if err := d.migrate(); err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DB) Close() error {
|
||||||
|
return d.sql.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DB) migrate() error {
|
||||||
|
_, err := d.sql.Exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS copy_history (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
disk_id TEXT NOT NULL,
|
||||||
|
source_path TEXT NOT NULL,
|
||||||
|
file_size INTEGER NOT NULL DEFAULT 0,
|
||||||
|
copied_at DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ','now'))
|
||||||
|
);
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_copy_history_disk_path
|
||||||
|
ON copy_history (disk_id, source_path);
|
||||||
|
`)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DB) WasCopied(diskID, sourcePath string) (bool, error) {
|
||||||
|
var n int
|
||||||
|
err := d.sql.QueryRow(
|
||||||
|
`SELECT COUNT(*) FROM copy_history WHERE disk_id=? AND source_path=?`,
|
||||||
|
diskID, sourcePath,
|
||||||
|
).Scan(&n)
|
||||||
|
return n > 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DB) RecordCopy(rec CopyRecord) error {
|
||||||
|
t := rec.CopiedAt
|
||||||
|
if t.IsZero() {
|
||||||
|
t = time.Now().UTC()
|
||||||
|
}
|
||||||
|
_, err := d.sql.Exec(
|
||||||
|
`INSERT OR IGNORE INTO copy_history (disk_id, source_path, file_size, copied_at) VALUES (?,?,?,?)`,
|
||||||
|
rec.DiskID, rec.SourcePath, rec.FileSize, t.Format(time.RFC3339),
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DB) CopiedPaths(diskID string) (map[string]struct{}, error) {
|
||||||
|
rows, err := d.sql.Query(
|
||||||
|
`SELECT source_path FROM copy_history WHERE disk_id=?`, diskID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
m := make(map[string]struct{})
|
||||||
|
for rows.Next() {
|
||||||
|
var p string
|
||||||
|
if err := rows.Scan(&p); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m[p] = struct{}{}
|
||||||
|
}
|
||||||
|
return m, rows.Err()
|
||||||
|
}
|
||||||
88
internal/disk/disk.go
Normal file
88
internal/disk/disk.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package disk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DiskState string
|
||||||
|
|
||||||
|
const (
|
||||||
|
DiskAbsent DiskState = "absent"
|
||||||
|
DiskForeign DiskState = "foreign"
|
||||||
|
DiskKnown DiskState = "known"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DiskInfo struct {
|
||||||
|
State DiskState `json:"state"`
|
||||||
|
DiskID string `json:"disk_id"`
|
||||||
|
TotalBytes int64 `json:"total_bytes"`
|
||||||
|
FreeBytes int64 `json:"free_bytes"`
|
||||||
|
MountPath string `json:"mount_path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const markerDir = ".jukebox"
|
||||||
|
const idFile = "disk.id"
|
||||||
|
|
||||||
|
func Probe(mountPath string) (DiskInfo, error) {
|
||||||
|
info := DiskInfo{MountPath: mountPath, State: DiskAbsent}
|
||||||
|
|
||||||
|
entries, err := os.ReadDir(mountPath)
|
||||||
|
if err != nil || len(entries) == 0 {
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
total, free, err := DiskUsage(mountPath)
|
||||||
|
if err != nil {
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
info.TotalBytes = total
|
||||||
|
info.FreeBytes = free
|
||||||
|
|
||||||
|
idPath := filepath.Join(mountPath, markerDir, idFile)
|
||||||
|
data, err := os.ReadFile(idPath)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
info.State = DiskForeign
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
info.State = DiskForeign
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
info.DiskID = strings.TrimSpace(string(data))
|
||||||
|
info.State = DiskKnown
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitDisk(mountPath string) (string, error) {
|
||||||
|
dir := filepath.Join(mountPath, markerDir)
|
||||||
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
id := uuid.New().String()
|
||||||
|
idPath := filepath.Join(dir, idFile)
|
||||||
|
if err := os.WriteFile(idPath, []byte(id), 0o644); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DBPath(mountPath string) string {
|
||||||
|
return filepath.Join(mountPath, markerDir, "history.db")
|
||||||
|
}
|
||||||
|
|
||||||
|
func DiskUsage(mountPath string) (total, free int64, err error) {
|
||||||
|
var stat syscall.Statfs_t
|
||||||
|
if err = syscall.Statfs(mountPath, &stat); err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
total = int64(stat.Blocks) * int64(stat.Bsize)
|
||||||
|
free = int64(stat.Bavail) * int64(stat.Bsize)
|
||||||
|
return total, free, nil
|
||||||
|
}
|
||||||
88
internal/task/task.go
Normal file
88
internal/task/task.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package task
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Status string
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusQueued Status = "queued"
|
||||||
|
StatusRunning Status = "running"
|
||||||
|
StatusSuccess Status = "success"
|
||||||
|
StatusFailed Status = "failed"
|
||||||
|
StatusCanceled Status = "canceled"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Task struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Status Status `json:"status"`
|
||||||
|
Progress int `json:"progress"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Task) IsTerminal() bool {
|
||||||
|
return t.Status == StatusSuccess || t.Status == StatusFailed || t.Status == StatusCanceled
|
||||||
|
}
|
||||||
|
|
||||||
|
type Store struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
tasks map[string]*Task
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStore() *Store {
|
||||||
|
return &Store{tasks: make(map[string]*Task)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) Create(taskType string) *Task {
|
||||||
|
t := &Task{
|
||||||
|
ID: uuid.New().String(),
|
||||||
|
Type: taskType,
|
||||||
|
Status: StatusQueued,
|
||||||
|
CreatedAt: time.Now().UTC(),
|
||||||
|
UpdatedAt: time.Now().UTC(),
|
||||||
|
}
|
||||||
|
s.mu.Lock()
|
||||||
|
s.tasks[t.ID] = t
|
||||||
|
s.mu.Unlock()
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) Get(id string) (*Task, bool) {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
t, ok := s.tasks[id]
|
||||||
|
if !ok {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
copy := *t
|
||||||
|
return ©, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) Update(id string, fn func(*Task)) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if t, ok := s.tasks[id]; ok {
|
||||||
|
fn(t)
|
||||||
|
t.UpdatedAt = time.Now().UTC()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) ActiveTask() (*Task, bool) {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
for _, t := range s.tasks {
|
||||||
|
if t.Status == StatusQueued || t.Status == StatusRunning {
|
||||||
|
copy := *t
|
||||||
|
return ©, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
70
internal/watcher/watcher.go
Normal file
70
internal/watcher/watcher.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package watcher
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/disk"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DiskEvent struct {
|
||||||
|
Info disk.DiskInfo
|
||||||
|
Prev disk.DiskState
|
||||||
|
}
|
||||||
|
|
||||||
|
type Handler func(event DiskEvent)
|
||||||
|
|
||||||
|
type Watcher struct {
|
||||||
|
mountPath string
|
||||||
|
interval time.Duration
|
||||||
|
handler Handler
|
||||||
|
|
||||||
|
mu sync.RWMutex
|
||||||
|
current disk.DiskInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(mountPath string, interval time.Duration, handler Handler) *Watcher {
|
||||||
|
return &Watcher{
|
||||||
|
mountPath: mountPath,
|
||||||
|
interval: interval,
|
||||||
|
handler: handler,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Watcher) CurrentDisk() disk.DiskInfo {
|
||||||
|
w.mu.RLock()
|
||||||
|
defer w.mu.RUnlock()
|
||||||
|
return w.current
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Watcher) Run(ctx context.Context) {
|
||||||
|
ticker := time.NewTicker(w.interval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
// probe immediately on start
|
||||||
|
w.probe()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
w.probe()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Watcher) probe() {
|
||||||
|
info, _ := disk.Probe(w.mountPath)
|
||||||
|
|
||||||
|
w.mu.Lock()
|
||||||
|
prev := w.current.State
|
||||||
|
changed := prev != info.State
|
||||||
|
w.current = info
|
||||||
|
w.mu.Unlock()
|
||||||
|
|
||||||
|
if changed && w.handler != nil {
|
||||||
|
w.handler(DiskEvent{Info: info, Prev: prev})
|
||||||
|
}
|
||||||
|
}
|
||||||
20
ops/build-image.sh
Executable file
20
ops/build-image.sh
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
|
||||||
|
|
||||||
|
if ! command -v docker >/dev/null 2>&1; then
|
||||||
|
echo "docker not found in PATH" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
IMAGE_NAME=${IMAGE_NAME:-jukebox-maker}
|
||||||
|
DEFAULT_TAG=$(git -C "${ROOT_DIR}" rev-parse --short HEAD 2>/dev/null || echo dev)
|
||||||
|
IMAGE_TAG=${1:-${IMAGE_TAG:-${DEFAULT_TAG}}}
|
||||||
|
|
||||||
|
echo "building ${IMAGE_NAME}:${IMAGE_TAG}"
|
||||||
|
docker build \
|
||||||
|
-f "${ROOT_DIR}/Dockerfile" \
|
||||||
|
-t "${IMAGE_NAME}:${IMAGE_TAG}" \
|
||||||
|
-t "${IMAGE_NAME}:latest" \
|
||||||
|
"${ROOT_DIR}"
|
||||||
6
web/embed.go
Normal file
6
web/embed.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed static templates
|
||||||
|
var FS embed.FS
|
||||||
329
web/static/style.css
Normal file
329
web/static/style.css
Normal file
@@ -0,0 +1,329 @@
|
|||||||
|
:root {
|
||||||
|
--bg: #ffffff;
|
||||||
|
--surface: #ffffff;
|
||||||
|
--surface-2: #f9fafb;
|
||||||
|
--border: rgba(34, 36, 38, 0.15);
|
||||||
|
--border-lite: rgba(34, 36, 38, 0.1);
|
||||||
|
--ink: rgba(0, 0, 0, 0.87);
|
||||||
|
--muted: rgba(0, 0, 0, 0.6);
|
||||||
|
--accent: #2185d0;
|
||||||
|
--accent-dark: #1678c2;
|
||||||
|
--accent-bg: #dff0ff;
|
||||||
|
--ok: #16ab39;
|
||||||
|
--warn: #f2711c;
|
||||||
|
--crit: #db2828;
|
||||||
|
--header-bg: #1b1c1d;
|
||||||
|
--radius: 4px;
|
||||||
|
--shadow: 0 1px 2px 0 rgba(34, 36, 38, 0.15);
|
||||||
|
--content-width: 960px;
|
||||||
|
}
|
||||||
|
|
||||||
|
* { box-sizing: border-box; }
|
||||||
|
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
background: var(--bg);
|
||||||
|
color: var(--ink);
|
||||||
|
font: 14px/1.5 Lato, "Helvetica Neue", Arial, Helvetica, sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
a { color: var(--accent); text-decoration: none; }
|
||||||
|
a:hover { text-decoration: underline; }
|
||||||
|
|
||||||
|
/* Header */
|
||||||
|
.page-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 24px;
|
||||||
|
padding: 14px 24px;
|
||||||
|
background: var(--header-bg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header h1 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 17px;
|
||||||
|
font-weight: 700;
|
||||||
|
color: rgba(255, 255, 255, 0.9);
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-nav { display: flex; gap: 4px; }
|
||||||
|
|
||||||
|
.header-action {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 6px 14px;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
background: rgba(255, 255, 255, 0.08);
|
||||||
|
color: rgba(255, 255, 255, 0.75);
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.15s;
|
||||||
|
}
|
||||||
|
.header-action:hover { background: rgba(255, 255, 255, 0.15); text-decoration: none; color: #fff; }
|
||||||
|
.header-action.active { background: rgba(255, 255, 255, 0.18); color: #fff; }
|
||||||
|
|
||||||
|
/* Main */
|
||||||
|
.page-main {
|
||||||
|
width: min(var(--content-width), calc(100vw - 48px));
|
||||||
|
margin: 28px auto 56px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Panel */
|
||||||
|
.panel {
|
||||||
|
margin-bottom: 24px;
|
||||||
|
overflow: hidden;
|
||||||
|
background: var(--surface);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
box-shadow: var(--shadow);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel > h2 {
|
||||||
|
margin: 0;
|
||||||
|
padding: 11px 16px;
|
||||||
|
background: var(--surface-2);
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 700;
|
||||||
|
color: var(--ink);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* KV table */
|
||||||
|
.kv-table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
|
||||||
|
.kv-table th,
|
||||||
|
.kv-table td {
|
||||||
|
padding: 10px 16px;
|
||||||
|
text-align: left;
|
||||||
|
vertical-align: top;
|
||||||
|
border-top: 1px solid var(--border-lite);
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.kv-table th {
|
||||||
|
width: 200px;
|
||||||
|
background: var(--surface-2);
|
||||||
|
border-top: 0;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--muted);
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.kv-table tr:first-child td { border-top: 0; }
|
||||||
|
|
||||||
|
/* Progress */
|
||||||
|
.progress-bar-bg {
|
||||||
|
background: rgba(34, 36, 38, 0.12);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
height: 10px;
|
||||||
|
overflow: hidden;
|
||||||
|
margin: 4px 0;
|
||||||
|
}
|
||||||
|
.progress-bar-fill {
|
||||||
|
background: var(--accent);
|
||||||
|
height: 100%;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
transition: width 0.4s ease;
|
||||||
|
}
|
||||||
|
.progress-label { font-size: 13px; color: var(--muted); margin-top: 6px; }
|
||||||
|
|
||||||
|
/* Buttons */
|
||||||
|
.button-primary {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 8px 18px;
|
||||||
|
border: none;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
background: var(--accent);
|
||||||
|
color: #fff;
|
||||||
|
font: inherit;
|
||||||
|
font-weight: 700;
|
||||||
|
font-size: 13px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.15s;
|
||||||
|
}
|
||||||
|
.button-primary:hover:not(:disabled) { background: var(--accent-dark); }
|
||||||
|
.button-primary:disabled { opacity: 0.45; cursor: not-allowed; }
|
||||||
|
|
||||||
|
.button-danger {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 8px 18px;
|
||||||
|
border: none;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
background: var(--crit);
|
||||||
|
color: #fff;
|
||||||
|
font: inherit;
|
||||||
|
font-weight: 700;
|
||||||
|
font-size: 13px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.15s;
|
||||||
|
}
|
||||||
|
.button-danger:hover { background: #c82020; }
|
||||||
|
|
||||||
|
.button-secondary {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 8px 18px;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
background: var(--surface);
|
||||||
|
color: var(--ink);
|
||||||
|
font: inherit;
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.15s;
|
||||||
|
}
|
||||||
|
.button-secondary:hover { background: var(--surface-2); }
|
||||||
|
|
||||||
|
.button-sm { padding: 5px 12px; font-size: 12px; }
|
||||||
|
|
||||||
|
.btn-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 14px 16px;
|
||||||
|
border-top: 1px solid var(--border-lite);
|
||||||
|
background: var(--surface-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Forms */
|
||||||
|
.form-body { padding: 16px; display: flex; flex-direction: column; gap: 18px; }
|
||||||
|
|
||||||
|
.form-group { display: flex; flex-direction: column; gap: 5px; }
|
||||||
|
|
||||||
|
.form-label {
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 700;
|
||||||
|
color: var(--ink);
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-hint { font-size: 12px; color: var(--muted); }
|
||||||
|
|
||||||
|
.form-input {
|
||||||
|
background: var(--surface);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
color: var(--ink);
|
||||||
|
padding: 7px 10px;
|
||||||
|
font: inherit;
|
||||||
|
font-size: 13px;
|
||||||
|
outline: none;
|
||||||
|
transition: border-color 0.15s;
|
||||||
|
}
|
||||||
|
.form-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-bg); }
|
||||||
|
.form-input[type="number"] { width: 120px; }
|
||||||
|
|
||||||
|
.form-select {
|
||||||
|
background: var(--surface);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
color: var(--ink);
|
||||||
|
padding: 7px 28px 7px 10px;
|
||||||
|
font: inherit;
|
||||||
|
font-size: 13px;
|
||||||
|
outline: none;
|
||||||
|
appearance: none;
|
||||||
|
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23666' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
background-position: right 8px center;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: border-color 0.15s;
|
||||||
|
}
|
||||||
|
.form-select:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-bg); }
|
||||||
|
|
||||||
|
/* Checkbox list */
|
||||||
|
.source-list { display: flex; flex-direction: column; gap: 0; }
|
||||||
|
.source-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
padding: 9px 16px;
|
||||||
|
border-bottom: 1px solid var(--border-lite);
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.1s;
|
||||||
|
}
|
||||||
|
.source-item:last-child { border-bottom: 0; }
|
||||||
|
.source-item:hover { background: rgba(33, 133, 208, 0.04); }
|
||||||
|
.source-item input[type="checkbox"] { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
|
||||||
|
.source-item-name { font-size: 13px; font-weight: 600; flex: 1; }
|
||||||
|
.source-item-path { font-size: 12px; color: var(--muted); font-family: monospace; }
|
||||||
|
|
||||||
|
/* Status badges */
|
||||||
|
.status-ok { color: var(--ok); font-weight: 600; }
|
||||||
|
.status-warning { color: var(--warn); font-weight: 600; }
|
||||||
|
.status-critical { color: var(--crit); font-weight: 600; }
|
||||||
|
.status-unknown { color: var(--muted); }
|
||||||
|
|
||||||
|
.badge {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 5px;
|
||||||
|
padding: 2px 8px;
|
||||||
|
border-radius: 2px;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 700;
|
||||||
|
border: 1px solid;
|
||||||
|
}
|
||||||
|
.badge-ok { color: var(--ok); background: rgba(22,171,57,0.08); border-color: rgba(22,171,57,0.3); }
|
||||||
|
.badge-warn { color: var(--warn); background: rgba(242,113,28,0.08); border-color: rgba(242,113,28,0.3); }
|
||||||
|
.badge-critical { color: var(--crit); background: rgba(219,40,40,0.08); border-color: rgba(219,40,40,0.3); }
|
||||||
|
.badge-unknown { color: var(--muted); background: var(--surface-2); border-color: var(--border); }
|
||||||
|
|
||||||
|
.badge::before { content: '●'; font-size: 8px; }
|
||||||
|
|
||||||
|
/* Alerts */
|
||||||
|
.alert {
|
||||||
|
padding: 10px 14px;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
font-size: 13px;
|
||||||
|
border-left: 3px solid;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
.alert-info { background: var(--accent-bg); border-color: var(--accent); color: var(--ink); }
|
||||||
|
.alert-warn { background: rgba(242,113,28,0.08); border-color: var(--warn); color: var(--ink); }
|
||||||
|
.alert-error { background: rgba(219,40,40,0.08); border-color: var(--crit); color: var(--crit); }
|
||||||
|
.alert-ok { background: rgba(22,171,57,0.08); border-color: var(--ok); color: var(--ink); }
|
||||||
|
|
||||||
|
/* Utils */
|
||||||
|
.hidden { display: none !important; }
|
||||||
|
.text-muted { color: var(--muted); font-size: 13px; }
|
||||||
|
.mono { font-family: monospace; font-size: 13px; }
|
||||||
|
|
||||||
|
/* Toast */
|
||||||
|
.toast-container {
|
||||||
|
position: fixed;
|
||||||
|
bottom: 20px; right: 20px;
|
||||||
|
display: flex; flex-direction: column; gap: 8px;
|
||||||
|
z-index: 1000;
|
||||||
|
}
|
||||||
|
.toast {
|
||||||
|
background: var(--header-bg);
|
||||||
|
color: rgba(255,255,255,0.9);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
padding: 10px 16px;
|
||||||
|
font-size: 13px;
|
||||||
|
max-width: 320px;
|
||||||
|
animation: slideIn 0.15s ease;
|
||||||
|
box-shadow: 0 4px 12px rgba(0,0,0,0.25);
|
||||||
|
}
|
||||||
|
.toast-ok { border-left: 3px solid var(--ok); }
|
||||||
|
.toast-error { border-left: 3px solid var(--crit); }
|
||||||
|
|
||||||
|
@keyframes slideIn {
|
||||||
|
from { transform: translateX(16px); opacity: 0; }
|
||||||
|
to { transform: translateX(0); opacity: 1; }
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 720px) {
|
||||||
|
.page-header { flex-wrap: wrap; padding: 12px 16px; }
|
||||||
|
.page-main { width: calc(100vw - 24px); margin-top: 20px; }
|
||||||
|
.kv-table th { width: 130px; }
|
||||||
|
}
|
||||||
137
web/templates/dashboard.html
Normal file
137
web/templates/dashboard.html
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
{{define "content"}}
|
||||||
|
|
||||||
|
<section class="panel">
|
||||||
|
<h2>Накопитель</h2>
|
||||||
|
<table class="kv-table">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<th>Статус</th>
|
||||||
|
<td id="diskState"><span class="badge badge-unknown">Не подключён</span></td>
|
||||||
|
</tr>
|
||||||
|
<tr id="rowDiskID" class="hidden">
|
||||||
|
<th>ID диска</th>
|
||||||
|
<td><span class="mono" id="valDiskID"></span></td>
|
||||||
|
</tr>
|
||||||
|
<tr id="rowTotal" class="hidden">
|
||||||
|
<th>Всего на диске</th>
|
||||||
|
<td id="valTotal"></td>
|
||||||
|
</tr>
|
||||||
|
<tr id="rowFree" class="hidden">
|
||||||
|
<th>Свободно</th>
|
||||||
|
<td id="valFree"></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="panel hidden" id="progressPanel">
|
||||||
|
<h2>Копирование</h2>
|
||||||
|
<div style="padding:14px 16px">
|
||||||
|
<div class="progress-bar-bg">
|
||||||
|
<div class="progress-bar-fill" id="progressFill" style="width:0%"></div>
|
||||||
|
</div>
|
||||||
|
<div class="progress-label" id="progressMsg">Подготовка…</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<div class="btn-row" style="background:transparent;border:none;padding:0;margin-bottom:24px">
|
||||||
|
<button class="button-primary" id="btnStart" onclick="startCopy()" disabled>▶ Запустить копирование</button>
|
||||||
|
<button class="button-danger hidden" id="btnCancel" onclick="cancelCopy()">✕ Отменить</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
let pollInterval = null;
|
||||||
|
let activeTaskId = null;
|
||||||
|
|
||||||
|
async function refreshDisk() {
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/disk');
|
||||||
|
if (!r.ok) return;
|
||||||
|
const d = await r.json();
|
||||||
|
|
||||||
|
const labels = { absent: 'Не подключён', foreign: 'Незнакомый диск', known: 'Диск подключён' };
|
||||||
|
const cls = { absent: 'badge-unknown', foreign: 'badge-warn', known: 'badge-ok' };
|
||||||
|
document.getElementById('diskState').innerHTML =
|
||||||
|
`<span class="badge ${cls[d.state]||'badge-unknown'}">${labels[d.state]||'—'}</span>`;
|
||||||
|
|
||||||
|
const known = d.state === 'known';
|
||||||
|
['rowDiskID','rowTotal','rowFree'].forEach(id =>
|
||||||
|
document.getElementById(id).classList.toggle('hidden', !known));
|
||||||
|
if (known) {
|
||||||
|
document.getElementById('valDiskID').textContent = d.disk_id;
|
||||||
|
document.getElementById('valTotal').textContent = fmtBytes(d.total_bytes);
|
||||||
|
document.getElementById('valFree').textContent = fmtBytes(d.free_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasTask = !!d.active_task_id;
|
||||||
|
document.getElementById('btnStart').disabled = !known || hasTask;
|
||||||
|
document.getElementById('btnStart').classList.toggle('hidden', hasTask);
|
||||||
|
document.getElementById('btnCancel').classList.toggle('hidden', !hasTask);
|
||||||
|
document.getElementById('progressPanel').classList.toggle('hidden', !hasTask);
|
||||||
|
|
||||||
|
if (d.active_task_id && d.active_task_id !== activeTaskId) {
|
||||||
|
activeTaskId = d.active_task_id;
|
||||||
|
startTaskPoll(activeTaskId);
|
||||||
|
}
|
||||||
|
if (!d.active_task_id && activeTaskId) {
|
||||||
|
activeTaskId = null; stopTaskPoll();
|
||||||
|
document.getElementById('progressPanel').classList.add('hidden');
|
||||||
|
}
|
||||||
|
} catch(e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
function startTaskPoll(id) { stopTaskPoll(); pollInterval = setInterval(() => pollTask(id), 1500); }
|
||||||
|
function stopTaskPoll() { if (pollInterval) { clearInterval(pollInterval); pollInterval = null; } }
|
||||||
|
|
||||||
|
async function pollTask(id) {
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/tasks/' + id);
|
||||||
|
if (!r.ok) return;
|
||||||
|
const t = await r.json();
|
||||||
|
document.getElementById('progressFill').style.width = t.progress + '%';
|
||||||
|
document.getElementById('progressMsg').textContent = t.message || '…';
|
||||||
|
if (['success','failed','canceled'].includes(t.status)) {
|
||||||
|
stopTaskPoll(); activeTaskId = null;
|
||||||
|
document.getElementById('btnStart').disabled = false;
|
||||||
|
document.getElementById('btnStart').classList.remove('hidden');
|
||||||
|
document.getElementById('btnCancel').classList.add('hidden');
|
||||||
|
document.getElementById('progressPanel').classList.add('hidden');
|
||||||
|
if (t.status === 'success') toast(t.message || 'Готово', 'ok');
|
||||||
|
if (t.status === 'failed') toast('Ошибка: ' + t.error, 'error');
|
||||||
|
if (t.status === 'canceled') toast('Копирование отменено', 'error');
|
||||||
|
refreshDisk();
|
||||||
|
}
|
||||||
|
} catch(e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startCopy() {
|
||||||
|
document.getElementById('btnStart').disabled = true;
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/copy/start', { method: 'POST' });
|
||||||
|
const d = await r.json();
|
||||||
|
if (!r.ok) {
|
||||||
|
toast(d.error || 'Ошибка запуска', 'error');
|
||||||
|
document.getElementById('btnStart').disabled = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
activeTaskId = d.task_id;
|
||||||
|
document.getElementById('btnStart').classList.add('hidden');
|
||||||
|
document.getElementById('btnCancel').classList.remove('hidden');
|
||||||
|
document.getElementById('progressPanel').classList.remove('hidden');
|
||||||
|
document.getElementById('progressFill').style.width = '0%';
|
||||||
|
document.getElementById('progressMsg').textContent = 'Подготовка…';
|
||||||
|
startTaskPoll(activeTaskId);
|
||||||
|
} catch(e) {
|
||||||
|
toast('Ошибка связи', 'error');
|
||||||
|
document.getElementById('btnStart').disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function cancelCopy() {
|
||||||
|
try { await fetch('/api/copy/cancel', { method: 'POST' }); toast('Отмена…', 'ok'); } catch(e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
refreshDisk();
|
||||||
|
setInterval(refreshDisk, 5000);
|
||||||
|
</script>
|
||||||
|
{{end}}
|
||||||
44
web/templates/layout.html
Normal file
44
web/templates/layout.html
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
{{define "layout"}}<!DOCTYPE html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{{.Title}} — Jukebox Maker</title>
|
||||||
|
<link rel="stylesheet" href="/static/style.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<header class="page-header">
|
||||||
|
<h1>🎵 Jukebox Maker</h1>
|
||||||
|
<nav class="header-nav">
|
||||||
|
<a href="/" class="header-action {{if eq .Page "dashboard"}}active{{end}}">Dashboard</a>
|
||||||
|
<a href="/settings" class="header-action {{if eq .Page "settings"}}active{{end}}">Настройки</a>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main class="page-main">
|
||||||
|
{{template "content" .}}
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<div class="toast-container" id="toastContainer"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function toast(msg, type) {
|
||||||
|
const c = document.getElementById('toastContainer');
|
||||||
|
const t = document.createElement('div');
|
||||||
|
t.className = 'toast toast-' + (type === 'error' ? 'error' : 'ok');
|
||||||
|
t.textContent = msg;
|
||||||
|
c.appendChild(t);
|
||||||
|
setTimeout(() => t.remove(), 4000);
|
||||||
|
}
|
||||||
|
function fmtBytes(b) {
|
||||||
|
if (!b) return '—';
|
||||||
|
if (b >= 1e12) return (b/1e12).toFixed(1) + ' ТБ';
|
||||||
|
if (b >= 1e9) return (b/1e9).toFixed(1) + ' ГБ';
|
||||||
|
if (b >= 1e6) return (b/1e6).toFixed(1) + ' МБ';
|
||||||
|
return (b/1e3).toFixed(0) + ' КБ';
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
{{end}}
|
||||||
138
web/templates/settings.html
Normal file
138
web/templates/settings.html
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
{{define "content"}}
|
||||||
|
<form id="settingsForm" onsubmit="saveSettings(event)">
|
||||||
|
|
||||||
|
<section class="panel">
|
||||||
|
<h2>Источники копирования</h2>
|
||||||
|
<div class="source-list" id="sourceList">
|
||||||
|
<div class="text-muted" style="padding:12px 16px">Загрузка…</div>
|
||||||
|
</div>
|
||||||
|
<div class="btn-row">
|
||||||
|
<button type="button" class="button-secondary button-sm" onclick="loadSources()">↻ Обновить список</button>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="panel">
|
||||||
|
<h2>Параметры копирования</h2>
|
||||||
|
<div class="form-body">
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label" for="reserveGB">Оставить свободным на диске (ГБ)</label>
|
||||||
|
<input class="form-input" type="number" id="reserveGB" min="0" max="1000" step="0.5" value="2">
|
||||||
|
<span class="form-hint">Копирование остановится, когда свободного места останется меньше этого значения.</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label" for="fileSelectMode">Какие файлы копировать</label>
|
||||||
|
<select class="form-select" id="fileSelectMode" style="width:auto;max-width:420px">
|
||||||
|
<option value="new">Только новые (не копировавшиеся на этот диск)</option>
|
||||||
|
<option value="all">Все подряд</option>
|
||||||
|
</select>
|
||||||
|
<span class="form-hint">«Только новые» — пропускает файлы, уже скопированные на данный диск, даже если они были удалены с него (считаются просмотренными).</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label" for="overwriteMode">Режим записи</label>
|
||||||
|
<select class="form-select" id="overwriteMode" style="width:auto;max-width:420px">
|
||||||
|
<option value="skip">Пропустить существующие файлы</option>
|
||||||
|
<option value="delete">Удалить наши данные с диска и перезаписать заново</option>
|
||||||
|
</select>
|
||||||
|
<span class="form-hint">«Удалить и перезаписать» — удаляет с диска всё кроме папки .jukebox, затем копирует заново.</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="panel">
|
||||||
|
<h2>Автоматизация</h2>
|
||||||
|
<div class="form-body">
|
||||||
|
<div class="form-group">
|
||||||
|
<label style="display:flex;align-items:center;gap:8px;cursor:pointer">
|
||||||
|
<input type="checkbox" id="autoCopy" style="width:15px;height:15px;accent-color:var(--accent)">
|
||||||
|
<span class="form-label" style="margin:0">Автоматическое копирование</span>
|
||||||
|
</label>
|
||||||
|
<span class="form-hint">При обнаружении знакомого накопителя копирование запустится автоматически.</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<div style="display:flex;gap:8px;margin-bottom:24px">
|
||||||
|
<button type="submit" class="button-primary">Сохранить настройки</button>
|
||||||
|
<button type="button" class="button-secondary" onclick="loadSettings()">Сбросить</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
let allSources = [];
|
||||||
|
let enabledSources = {};
|
||||||
|
|
||||||
|
async function loadSources() {
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/sources');
|
||||||
|
if (!r.ok) return;
|
||||||
|
const d = await r.json();
|
||||||
|
allSources = d.items || [];
|
||||||
|
renderSources();
|
||||||
|
} catch(e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderSources() {
|
||||||
|
const el = document.getElementById('sourceList');
|
||||||
|
if (!allSources.length) {
|
||||||
|
el.innerHTML = '<div class="text-muted" style="padding:12px 16px">Папки в /media не найдены.</div>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
el.innerHTML = allSources.map(path => {
|
||||||
|
const checked = enabledSources[path] !== false;
|
||||||
|
return `<label class="source-item">
|
||||||
|
<input type="checkbox" data-source="${path}" ${checked ? 'checked' : ''}>
|
||||||
|
<span class="source-item-name">${path}</span>
|
||||||
|
<span class="source-item-path">/media/${path}</span>
|
||||||
|
</label>`;
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadSettings() {
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/config');
|
||||||
|
if (!r.ok) return;
|
||||||
|
const cfg = await r.json();
|
||||||
|
document.getElementById('reserveGB').value = cfg.reserve_free_gb ?? 2;
|
||||||
|
document.getElementById('fileSelectMode').value = cfg.file_select_mode || 'new';
|
||||||
|
document.getElementById('overwriteMode').value = cfg.overwrite_mode || 'skip';
|
||||||
|
document.getElementById('autoCopy').checked = !!cfg.auto_copy;
|
||||||
|
enabledSources = {};
|
||||||
|
(cfg.sources || []).forEach(s => { enabledSources[s.path] = s.enabled; });
|
||||||
|
renderSources();
|
||||||
|
} catch(e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveSettings(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const checkboxes = document.querySelectorAll('[data-source]');
|
||||||
|
const sources = Array.from(checkboxes).map(cb => ({ path: cb.dataset.source, enabled: cb.checked }));
|
||||||
|
Object.keys(enabledSources).forEach(path => {
|
||||||
|
if (!sources.find(s => s.path === path)) sources.push({ path, enabled: false });
|
||||||
|
});
|
||||||
|
const body = {
|
||||||
|
reserve_free_gb: parseFloat(document.getElementById('reserveGB').value) || 2,
|
||||||
|
file_select_mode: document.getElementById('fileSelectMode').value,
|
||||||
|
overwrite_mode: document.getElementById('overwriteMode').value,
|
||||||
|
auto_copy: document.getElementById('autoCopy').checked,
|
||||||
|
sources,
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/config', {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
if (r.ok) { toast('Настройки сохранены', 'ok'); await loadSettings(); }
|
||||||
|
else { const d = await r.json(); toast(d.error || 'Ошибка сохранения', 'error'); }
|
||||||
|
} catch(e) { toast('Ошибка связи', 'error'); }
|
||||||
|
}
|
||||||
|
|
||||||
|
loadSettings();
|
||||||
|
loadSources();
|
||||||
|
</script>
|
||||||
|
{{end}}
|
||||||
Reference in New Issue
Block a user