Compare commits
7 Commits
75c6b928ae
...
v1.7
| Author | SHA1 | Date | |
|---|---|---|---|
| 2bad23da3a | |||
| 0a17d11bd1 | |||
| e885e49647 | |||
| 70d301f78f | |||
| 9fd02fb5bf | |||
| 6953c151fe | |||
| 50246ada85 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -27,7 +27,9 @@ go.work.sum
|
|||||||
|
|
||||||
# Build output
|
# Build output
|
||||||
/jukebox
|
/jukebox
|
||||||
|
/release/
|
||||||
|
/.tmp/
|
||||||
|
/.gocache/
|
||||||
|
|
||||||
# Temp copy files
|
# Temp copy files
|
||||||
*.juketmp
|
*.juketmp
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \
|
|||||||
|
|
||||||
FROM alpine:3.19
|
FROM alpine:3.19
|
||||||
|
|
||||||
RUN apk add --no-cache tzdata ca-certificates rsync
|
RUN apk add --no-cache tzdata ca-certificates rsync ffmpeg
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=builder /out/jukebox .
|
COPY --from=builder /out/jukebox .
|
||||||
|
|||||||
51
Makefile
Normal file
51
Makefile
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
APP := jukebox
|
||||||
|
APP_PKG := ./cmd/jukebox
|
||||||
|
APP_URL := http://127.0.0.1:8080
|
||||||
|
|
||||||
|
ROOT_DIR := $(CURDIR)
|
||||||
|
DEV_DIR := $(ROOT_DIR)/.tmp
|
||||||
|
CONFIG_DIR := $(DEV_DIR)/config
|
||||||
|
MEDIA_DIR := $(DEV_DIR)/media
|
||||||
|
MOUNT_DIR := $(DEV_DIR)/mount
|
||||||
|
CONFIG_PATH := $(CONFIG_DIR)/config.json
|
||||||
|
GOCACHE_DIR := $(DEV_DIR)/gocache
|
||||||
|
|
||||||
|
.PHONY: run test build release dev-dirs
|
||||||
|
|
||||||
|
dev-dirs:
|
||||||
|
mkdir -p "$(CONFIG_DIR)" "$(MEDIA_DIR)" "$(MOUNT_DIR)" "$(GOCACHE_DIR)"
|
||||||
|
|
||||||
|
run: dev-dirs
|
||||||
|
@sh -c '\
|
||||||
|
url="$(APP_URL)"; \
|
||||||
|
open_url() { \
|
||||||
|
if command -v open >/dev/null 2>&1; then \
|
||||||
|
open "$$1"; \
|
||||||
|
elif command -v powershell >/dev/null 2>&1; then \
|
||||||
|
powershell -NoProfile -Command "Start-Process '\''$$1'\''" >/dev/null; \
|
||||||
|
elif command -v xdg-open >/dev/null 2>&1; then \
|
||||||
|
xdg-open "$$1" >/dev/null 2>&1; \
|
||||||
|
fi; \
|
||||||
|
}; \
|
||||||
|
for _ in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do \
|
||||||
|
if curl -fsS "$$url/health" >/dev/null 2>&1; then \
|
||||||
|
open_url "$$url"; \
|
||||||
|
exit 0; \
|
||||||
|
fi; \
|
||||||
|
sleep 1; \
|
||||||
|
done \
|
||||||
|
' &
|
||||||
|
GOCACHE="$(GOCACHE_DIR)" go run $(APP_PKG) \
|
||||||
|
-config "$(CONFIG_PATH)" \
|
||||||
|
-media "$(MEDIA_DIR)" \
|
||||||
|
-mount "$(MOUNT_DIR)" \
|
||||||
|
-addr ":8080"
|
||||||
|
|
||||||
|
test: dev-dirs
|
||||||
|
GOCACHE="$(GOCACHE_DIR)" go test ./...
|
||||||
|
|
||||||
|
build: dev-dirs
|
||||||
|
GOCACHE="$(GOCACHE_DIR)" go build -o "$(ROOT_DIR)/$(APP)" $(APP_PKG)
|
||||||
|
|
||||||
|
release:
|
||||||
|
./ops/build-release.sh
|
||||||
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -33,6 +34,9 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("load config: %v", err)
|
log.Fatalf("load config: %v", err)
|
||||||
}
|
}
|
||||||
|
if cfg.MediaPath == "" {
|
||||||
|
cfg.MediaPath = config.NormalizeMediaPath(*mediaPath)
|
||||||
|
}
|
||||||
|
|
||||||
taskStore := task.NewStore()
|
taskStore := task.NewStore()
|
||||||
cp := copier.New(taskStore)
|
cp := copier.New(taskStore)
|
||||||
@@ -122,8 +126,8 @@ func main() {
|
|||||||
switch ev.Info.State {
|
switch ev.Info.State {
|
||||||
case disk.DiskKnown:
|
case disk.DiskKnown:
|
||||||
openDiskDB(ev.Info)
|
openDiskDB(ev.Info)
|
||||||
if watcherReady && ev.Prev.State != disk.DiskKnown && cfg.AutoCopy {
|
if watcherReady && ev.Prev.State != disk.DiskKnown {
|
||||||
triggerAutoCopy(cp, cfg, ev.Info, *mediaPath)
|
triggerAutoCopy(cp, cfg, ev.Info)
|
||||||
}
|
}
|
||||||
case disk.DiskForeign:
|
case disk.DiskForeign:
|
||||||
closeDiskDB(ev.Prev)
|
closeDiskDB(ev.Prev)
|
||||||
@@ -134,6 +138,21 @@ func main() {
|
|||||||
w.ProbeNow()
|
w.ProbeNow()
|
||||||
watcherReady = true
|
watcherReady = true
|
||||||
|
|
||||||
|
probeDisk := func(mountPath string) (disk.DiskInfo, error) {
|
||||||
|
mountPath = config.NormalizeMediaPath(mountPath)
|
||||||
|
if mountPath == "" {
|
||||||
|
return disk.DiskInfo{}, errors.New("mount_path is required")
|
||||||
|
}
|
||||||
|
info, err := disk.Probe(mountPath)
|
||||||
|
if err != nil {
|
||||||
|
return info, err
|
||||||
|
}
|
||||||
|
if info.State == disk.DiskKnown {
|
||||||
|
openDiskDB(info)
|
||||||
|
}
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
srv, err := api.New(api.Deps{
|
srv, err := api.New(api.Deps{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
ConfigPath: *configPath,
|
ConfigPath: *configPath,
|
||||||
@@ -141,8 +160,7 @@ func main() {
|
|||||||
Watcher: w,
|
Watcher: w,
|
||||||
Copier: cp,
|
Copier: cp,
|
||||||
Tasks: taskStore,
|
Tasks: taskStore,
|
||||||
MediaPath: *mediaPath,
|
ProbeDisk: probeDisk,
|
||||||
MountPath: *mountPath,
|
|
||||||
OnDiskInit: func(mountPath, diskID string) {
|
OnDiskInit: func(mountPath, diskID string) {
|
||||||
openDiskDB(disk.DiskInfo{
|
openDiskDB(disk.DiskInfo{
|
||||||
State: disk.DiskKnown,
|
State: disk.DiskKnown,
|
||||||
@@ -178,7 +196,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func triggerAutoCopy(cp *copier.Copier, cfg *config.Config, info disk.DiskInfo, mediaPath string) {
|
func triggerAutoCopy(cp *copier.Copier, cfg *config.Config, info disk.DiskInfo) {
|
||||||
|
// Используем AutoCopy из профиля диска, если он есть; иначе — из глобального config
|
||||||
|
autoCopy := cfg.AutoCopy
|
||||||
|
if info.Profile != nil {
|
||||||
|
autoCopy = info.Profile.AutoCopy
|
||||||
|
}
|
||||||
|
if !autoCopy {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
hasEnabledSources := false
|
hasEnabledSources := false
|
||||||
for _, s := range cfg.Sources {
|
for _, s := range cfg.Sources {
|
||||||
if s.Enabled {
|
if s.Enabled {
|
||||||
@@ -189,17 +216,32 @@ func triggerAutoCopy(cp *copier.Copier, cfg *config.Config, info disk.DiskInfo,
|
|||||||
if !hasEnabledSources {
|
if !hasEnabledSources {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opts := copier.Options{
|
||||||
|
DiskID: info.DiskID,
|
||||||
|
MountPath: info.MountPath,
|
||||||
|
MediaPath: cfg.MediaPath,
|
||||||
|
SourceRules: cfg.Sources,
|
||||||
|
AllowedExtensions: cfg.EffectiveAllowedExtensions(),
|
||||||
|
OverwriteMode: cfg.OverwriteMode,
|
||||||
|
}
|
||||||
|
if p := info.Profile; p != nil {
|
||||||
|
opts.DestFolder = p.DestFolder
|
||||||
|
opts.ReserveFreeGB = p.ReserveFreeGB
|
||||||
|
opts.FileSelectMode = config.FileSelectMode(p.FileSelectMode)
|
||||||
|
opts.Transcode = p.Transcode
|
||||||
|
opts.ShuffleDepth = p.ShuffleDepth
|
||||||
|
if p.OverwriteMode != "" {
|
||||||
|
opts.OverwriteMode = config.OverwriteMode(p.OverwriteMode)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
opts.DestFolder = cfg.DestFolder
|
||||||
|
opts.ReserveFreeGB = cfg.ReserveFreeGB
|
||||||
|
opts.FileSelectMode = cfg.FileSelectMode
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
_, err := cp.Start(context.Background(), copier.Options{
|
_, err := cp.Start(context.Background(), opts)
|
||||||
DiskID: info.DiskID,
|
|
||||||
MountPath: info.MountPath,
|
|
||||||
MediaPath: mediaPath,
|
|
||||||
DestFolder: cfg.DestFolder,
|
|
||||||
SourceRules: cfg.Sources,
|
|
||||||
ReserveFreeGB: cfg.ReserveFreeGB,
|
|
||||||
OverwriteMode: cfg.OverwriteMode,
|
|
||||||
FileSelectMode: cfg.FileSelectMode,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("auto-copy: %v", err)
|
log.Printf("auto-copy: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,57 @@ import (
|
|||||||
"jukebox_maker/internal/disk"
|
"jukebox_maker/internal/disk"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s *Server) copyOptions(cfg *config.Config, diskInfo disk.DiskInfo, overwriteMode config.OverwriteMode) copier.Options {
|
||||||
|
opts := copier.Options{
|
||||||
|
DiskID: diskInfo.DiskID,
|
||||||
|
MountPath: diskInfo.MountPath,
|
||||||
|
MediaPath: cfg.MediaPath,
|
||||||
|
SourceRules: cfg.Sources,
|
||||||
|
AllowedExtensions: cfg.EffectiveAllowedExtensions(),
|
||||||
|
OverwriteMode: overwriteMode,
|
||||||
|
}
|
||||||
|
if p := diskInfo.Profile; p != nil {
|
||||||
|
opts.DestFolder = p.DestFolder
|
||||||
|
opts.ReserveFreeGB = p.ReserveFreeGB
|
||||||
|
opts.FileSelectMode = config.FileSelectMode(p.FileSelectMode)
|
||||||
|
opts.Transcode = p.Transcode
|
||||||
|
opts.ShuffleDepth = p.ShuffleDepth
|
||||||
|
} else {
|
||||||
|
opts.DestFolder = cfg.DestFolder
|
||||||
|
opts.ReserveFreeGB = cfg.ReserveFreeGB
|
||||||
|
opts.FileSelectMode = cfg.FileSelectMode
|
||||||
|
}
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasEnabledSources(cfg *config.Config) bool {
|
||||||
|
for _, src := range cfg.Sources {
|
||||||
|
if src.Enabled {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeCopyMode(r *http.Request, fallback config.OverwriteMode) (config.OverwriteMode, error) {
|
||||||
|
var req struct {
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
}
|
||||||
|
if r.Body != nil {
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch req.Mode {
|
||||||
|
case "", "add":
|
||||||
|
return config.OverwriteSkip, nil
|
||||||
|
case "replace":
|
||||||
|
return config.OverwriteDelete, nil
|
||||||
|
default:
|
||||||
|
return fallback, errors.New("invalid copy mode")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) handleCopyStart(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) handleCopyStart(w http.ResponseWriter, r *http.Request) {
|
||||||
diskID := r.PathValue("diskID")
|
diskID := r.PathValue("diskID")
|
||||||
diskInfo, ok := s.deps.Watcher.DiskByID(diskID)
|
diskInfo, ok := s.deps.Watcher.DiskByID(diskID)
|
||||||
@@ -21,26 +72,70 @@ func (s *Server) handleCopyStart(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg := s.deps.Config
|
cfg := s.deps.Config
|
||||||
hasEnabledSources := false
|
if !hasEnabledSources(cfg) {
|
||||||
for _, src := range cfg.Sources {
|
jsonErr(w, http.StatusUnprocessableEntity, "no source folders selected")
|
||||||
if src.Enabled {
|
return
|
||||||
hasEnabledSources = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if !hasEnabledSources {
|
|
||||||
|
overwriteMode, err := decodeCopyMode(r, cfg.OverwriteMode)
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == "invalid copy mode" {
|
||||||
|
jsonErr(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonErr(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
reserveGB := cfg.ReserveFreeGB
|
||||||
|
if diskInfo.Profile != nil {
|
||||||
|
reserveGB = diskInfo.Profile.ReserveFreeGB
|
||||||
|
}
|
||||||
|
if diskInfo.FreeBytes <= int64(reserveGB*1e9) {
|
||||||
|
jsonErr(w, http.StatusUnprocessableEntity, "free space is below reserve threshold")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := s.copyOptions(cfg, diskInfo, overwriteMode)
|
||||||
|
|
||||||
|
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) handleCopyStartSelected(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cfg := s.deps.Config
|
||||||
|
if !hasEnabledSources(cfg) {
|
||||||
jsonErr(w, http.StatusUnprocessableEntity, "no source folders selected")
|
jsonErr(w, http.StatusUnprocessableEntity, "no source folders selected")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req struct {
|
var req struct {
|
||||||
Mode string `json:"mode"`
|
MountPath string `json:"mount_path"`
|
||||||
|
Mode string `json:"mode"`
|
||||||
}
|
}
|
||||||
if r.Body != nil {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
|
jsonErr(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
||||||
jsonErr(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
return
|
||||||
return
|
}
|
||||||
}
|
diskInfo, err := s.deps.ProbeDisk(req.MountPath)
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if diskInfo.State != disk.DiskKnown {
|
||||||
|
jsonErr(w, http.StatusUnprocessableEntity, "selected directory is not an initialized jukebox disk")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
overwriteMode := cfg.OverwriteMode
|
overwriteMode := cfg.OverwriteMode
|
||||||
@@ -54,24 +149,19 @@ func (s *Server) handleCopyStart(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
reserveBytes := int64(cfg.ReserveFreeGB * 1e9)
|
reserveGB := cfg.ReserveFreeGB
|
||||||
if diskInfo.FreeBytes <= reserveBytes {
|
if diskInfo.Profile != nil {
|
||||||
|
reserveGB = diskInfo.Profile.ReserveFreeGB
|
||||||
|
}
|
||||||
|
if diskInfo.FreeBytes <= int64(reserveGB*1e9) {
|
||||||
jsonErr(w, http.StatusUnprocessableEntity, "free space is below reserve threshold")
|
jsonErr(w, http.StatusUnprocessableEntity, "free space is below reserve threshold")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
opts := copier.Options{
|
if s.deps.OnDiskInit != nil {
|
||||||
DiskID: diskInfo.DiskID,
|
s.deps.OnDiskInit(diskInfo.MountPath, diskInfo.DiskID)
|
||||||
MountPath: diskInfo.MountPath,
|
|
||||||
MediaPath: s.deps.MediaPath,
|
|
||||||
DestFolder: cfg.DestFolder,
|
|
||||||
SourceRules: cfg.Sources,
|
|
||||||
ReserveFreeGB: cfg.ReserveFreeGB,
|
|
||||||
OverwriteMode: overwriteMode,
|
|
||||||
FileSelectMode: cfg.FileSelectMode,
|
|
||||||
}
|
}
|
||||||
|
taskID, err := s.deps.Copier.Start(context.Background(), s.copyOptions(cfg, diskInfo, overwriteMode))
|
||||||
taskID, err := s.deps.Copier.Start(context.Background(), opts)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err.Error() {
|
switch err.Error() {
|
||||||
case "copy already running":
|
case "copy already running":
|
||||||
|
|||||||
@@ -5,9 +5,33 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/config"
|
||||||
"jukebox_maker/internal/disk"
|
"jukebox_maker/internal/disk"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s *Server) diskResponse(info disk.DiskInfo) map[string]any {
|
||||||
|
item := map[string]any{
|
||||||
|
"state": info.State,
|
||||||
|
"disk_id": info.DiskID,
|
||||||
|
"total_bytes": info.TotalBytes,
|
||||||
|
"free_bytes": info.FreeBytes,
|
||||||
|
"mount_path": info.MountPath,
|
||||||
|
"profile": info.Profile,
|
||||||
|
}
|
||||||
|
if info.DiskID != "" {
|
||||||
|
if s.deps.OnDiskInit != nil {
|
||||||
|
s.deps.OnDiskInit(info.MountPath, info.DiskID)
|
||||||
|
}
|
||||||
|
if lastCopiedAt, ok, err := s.deps.Copier.LastCopiedAt(info.DiskID); err == nil && ok {
|
||||||
|
item["last_copied_at"] = lastCopiedAt.Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
if t, ok := s.deps.Tasks.ActiveTaskByDisk(info.DiskID); ok {
|
||||||
|
item["active_task_id"] = t.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
type response struct {
|
type response struct {
|
||||||
State disk.DiskState `json:"state"`
|
State disk.DiskState `json:"state"`
|
||||||
@@ -29,12 +53,12 @@ func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
FreeBytes: info.FreeBytes,
|
FreeBytes: info.FreeBytes,
|
||||||
MountPath: info.MountPath,
|
MountPath: info.MountPath,
|
||||||
}
|
}
|
||||||
if info.DiskID != "" {
|
if payload := s.diskResponse(info); payload != nil {
|
||||||
if lastCopiedAt, ok, err := s.deps.Copier.LastCopiedAt(info.DiskID); err == nil && ok {
|
if v, ok := payload["last_copied_at"].(string); ok {
|
||||||
item.LastCopiedAt = lastCopiedAt.Format(time.RFC3339)
|
item.LastCopiedAt = v
|
||||||
}
|
}
|
||||||
if t, ok := s.deps.Tasks.ActiveTaskByDisk(info.DiskID); ok {
|
if v, ok := payload["active_task_id"].(string); ok {
|
||||||
item.ActiveTaskID = t.ID
|
item.ActiveTaskID = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resp = append(resp, item)
|
resp = append(resp, item)
|
||||||
@@ -43,6 +67,16 @@ func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonOK(w, map[string]any{"items": resp})
|
jsonOK(w, map[string]any{"items": resp})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleDiskProbe(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mountPath := r.URL.Query().Get("mount_path")
|
||||||
|
info, err := s.deps.ProbeDisk(mountPath)
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonOK(w, s.diskResponse(info))
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) handleDiskInit(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) handleDiskInit(w http.ResponseWriter, r *http.Request) {
|
||||||
var req struct {
|
var req struct {
|
||||||
MountPath string `json:"mount_path"`
|
MountPath string `json:"mount_path"`
|
||||||
@@ -52,9 +86,9 @@ func (s *Server) handleDiskInit(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
info, ok := s.deps.Watcher.DiskByMountPath(req.MountPath)
|
info, err := s.deps.ProbeDisk(req.MountPath)
|
||||||
if !ok {
|
if err != nil {
|
||||||
jsonErr(w, http.StatusNotFound, "disk not found")
|
jsonErr(w, http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if info.State == disk.DiskAbsent {
|
if info.State == disk.DiskAbsent {
|
||||||
@@ -76,7 +110,44 @@ func (s *Server) handleDiskInit(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.deps.OnDiskInit(info.MountPath, diskID)
|
if s.deps.OnDiskInit != nil {
|
||||||
s.deps.Watcher.ProbeNow()
|
s.deps.OnDiskInit(info.MountPath, diskID)
|
||||||
|
}
|
||||||
jsonOK(w, map[string]string{"disk_id": diskID})
|
jsonOK(w, map[string]string{"disk_id": diskID})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleGetProfile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mountPath := config.NormalizeMediaPath(r.URL.Query().Get("mount_path"))
|
||||||
|
if mountPath == "" {
|
||||||
|
jsonErr(w, http.StatusBadRequest, "mount_path is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p, err := disk.LoadProfile(mountPath)
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, http.StatusNotFound, "profile not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonOK(w, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handlePutProfile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mountPath := config.NormalizeMediaPath(r.URL.Query().Get("mount_path"))
|
||||||
|
if mountPath == "" {
|
||||||
|
jsonErr(w, http.StatusBadRequest, "mount_path is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var p disk.DiskProfile
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
|
||||||
|
jsonErr(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := disk.SaveProfile(mountPath, &p); err != nil {
|
||||||
|
jsonErr(w, http.StatusInternalServerError, "save profile: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Обновляем информацию о диске в watcher если он там есть
|
||||||
|
if s.deps.Watcher != nil {
|
||||||
|
s.deps.Watcher.ProbeNow()
|
||||||
|
}
|
||||||
|
jsonOK(w, &p)
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func (s *Server) handleSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
absPath := s.deps.MediaPath
|
absPath := s.deps.Config.MediaPath
|
||||||
if relPath != "" {
|
if relPath != "" {
|
||||||
absPath = filepath.Join(absPath, relPath)
|
absPath = filepath.Join(absPath, relPath)
|
||||||
}
|
}
|
||||||
|
|||||||
16
internal/api/handlers_system.go
Normal file
16
internal/api/handlers_system.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/dialog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) handlePickFolder(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path, err := dialog.PickFolder()
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, http.StatusUnprocessableEntity, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonOK(w, map[string]string{"path": path})
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"jukebox_maker/internal/config"
|
"jukebox_maker/internal/config"
|
||||||
"jukebox_maker/internal/copier"
|
"jukebox_maker/internal/copier"
|
||||||
|
"jukebox_maker/internal/disk"
|
||||||
"jukebox_maker/internal/task"
|
"jukebox_maker/internal/task"
|
||||||
"jukebox_maker/internal/watcher"
|
"jukebox_maker/internal/watcher"
|
||||||
)
|
)
|
||||||
@@ -20,8 +21,7 @@ type Deps struct {
|
|||||||
Watcher *watcher.Watcher
|
Watcher *watcher.Watcher
|
||||||
Copier *copier.Copier
|
Copier *copier.Copier
|
||||||
Tasks *task.Store
|
Tasks *task.Store
|
||||||
MediaPath string
|
ProbeDisk func(mountPath string) (disk.DiskInfo, error)
|
||||||
MountPath string
|
|
||||||
// OnDiskInit вызывается при ручной инициализации диска через UI.
|
// OnDiskInit вызывается при ручной инициализации диска через UI.
|
||||||
OnDiskInit func(mountPath, diskID string)
|
OnDiskInit func(mountPath, diskID string)
|
||||||
}
|
}
|
||||||
@@ -60,13 +60,18 @@ func (s *Server) routes() {
|
|||||||
|
|
||||||
s.mux.HandleFunc("GET /health", s.handleHealth)
|
s.mux.HandleFunc("GET /health", s.handleHealth)
|
||||||
s.mux.HandleFunc("GET /api/disks", s.handleDiskStatus)
|
s.mux.HandleFunc("GET /api/disks", s.handleDiskStatus)
|
||||||
|
s.mux.HandleFunc("GET /api/disks/probe", s.handleDiskProbe)
|
||||||
s.mux.HandleFunc("POST /api/disks/init", s.handleDiskInit)
|
s.mux.HandleFunc("POST /api/disks/init", s.handleDiskInit)
|
||||||
|
s.mux.HandleFunc("POST /api/disks/copy/start", s.handleCopyStartSelected)
|
||||||
s.mux.HandleFunc("GET /api/sources", s.handleSources)
|
s.mux.HandleFunc("GET /api/sources", s.handleSources)
|
||||||
s.mux.HandleFunc("GET /api/config", s.handleGetConfig)
|
s.mux.HandleFunc("GET /api/config", s.handleGetConfig)
|
||||||
s.mux.HandleFunc("PUT /api/config", s.handlePutConfig)
|
s.mux.HandleFunc("PUT /api/config", s.handlePutConfig)
|
||||||
|
s.mux.HandleFunc("POST /api/system/pick-folder", s.handlePickFolder)
|
||||||
s.mux.HandleFunc("POST /api/disks/{diskID}/copy/start", s.handleCopyStart)
|
s.mux.HandleFunc("POST /api/disks/{diskID}/copy/start", s.handleCopyStart)
|
||||||
s.mux.HandleFunc("POST /api/disks/{diskID}/copy/cancel", s.handleCopyCancel)
|
s.mux.HandleFunc("POST /api/disks/{diskID}/copy/cancel", s.handleCopyCancel)
|
||||||
s.mux.HandleFunc("GET /api/tasks/{id}", s.handleTaskGet)
|
s.mux.HandleFunc("GET /api/tasks/{id}", s.handleTaskGet)
|
||||||
|
s.mux.HandleFunc("GET /api/disks/profile", s.handleGetProfile)
|
||||||
|
s.mux.HandleFunc("PUT /api/disks/profile", s.handlePutProfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
type OverwriteMode string
|
type OverwriteMode string
|
||||||
type FileSelectMode string
|
type FileSelectMode string
|
||||||
|
type AllowedFilesMode string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultDestFolder = "media"
|
DefaultDestFolder = "media"
|
||||||
@@ -21,31 +22,46 @@ const (
|
|||||||
|
|
||||||
SelectNew FileSelectMode = "new"
|
SelectNew FileSelectMode = "new"
|
||||||
SelectAll FileSelectMode = "all"
|
SelectAll FileSelectMode = "all"
|
||||||
|
|
||||||
|
AllowedFilesByMediaType AllowedFilesMode = "media_types"
|
||||||
|
AllowedFilesByExtensions AllowedFilesMode = "extensions"
|
||||||
|
|
||||||
|
MediaTypeAudio = "audio"
|
||||||
|
MediaTypeVideo = "video"
|
||||||
|
MediaTypePhoto = "photo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SourceFolder struct {
|
type SourceFolder struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
|
Root bool `json:"root,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ReserveFreeGB float64 `json:"reserve_free_gb"`
|
MediaPath string `json:"media_path"`
|
||||||
DestFolder string `json:"dest_folder"`
|
ReserveFreeGB float64 `json:"reserve_free_gb"`
|
||||||
Sources []SourceFolder `json:"sources"`
|
DestFolder string `json:"dest_folder"`
|
||||||
OverwriteMode OverwriteMode `json:"overwrite_mode"`
|
Sources []SourceFolder `json:"sources"`
|
||||||
FileSelectMode FileSelectMode `json:"file_select_mode"`
|
OverwriteMode OverwriteMode `json:"overwrite_mode"`
|
||||||
AutoCopy bool `json:"auto_copy"`
|
FileSelectMode FileSelectMode `json:"file_select_mode"`
|
||||||
|
AllowedFilesMode AllowedFilesMode `json:"allowed_files_mode"`
|
||||||
|
EnabledMediaTypes []string `json:"enabled_media_types,omitempty"`
|
||||||
|
AllowedExtensions []string `json:"allowed_extensions,omitempty"`
|
||||||
|
AutoCopy bool `json:"auto_copy"`
|
||||||
FileReplicaCounts map[string]int `json:"file_replica_counts,omitempty"`
|
FileReplicaCounts map[string]int `json:"file_replica_counts,omitempty"`
|
||||||
DiskReplicaFiles map[string][]string `json:"disk_replica_files,omitempty"`
|
DiskReplicaFiles map[string][]string `json:"disk_replica_files,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaults() Config {
|
func defaults() Config {
|
||||||
return Config{
|
return Config{
|
||||||
ReserveFreeGB: 2.0,
|
ReserveFreeGB: 2.0,
|
||||||
DestFolder: DefaultDestFolder,
|
DestFolder: DefaultDestFolder,
|
||||||
OverwriteMode: OverwriteSkip,
|
OverwriteMode: OverwriteSkip,
|
||||||
FileSelectMode: SelectNew,
|
FileSelectMode: SelectNew,
|
||||||
AutoCopy: false,
|
AllowedFilesMode: AllowedFilesByMediaType,
|
||||||
|
EnabledMediaTypes: DefaultEnabledMediaTypes(),
|
||||||
|
AllowedExtensions: DefaultAllowedExtensions(),
|
||||||
|
AutoCopy: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +83,19 @@ func Load(path string) (*Config, error) {
|
|||||||
} else {
|
} else {
|
||||||
cfg.DestFolder = defaults().DestFolder
|
cfg.DestFolder = defaults().DestFolder
|
||||||
}
|
}
|
||||||
|
if cfg.AllowedFilesMode != AllowedFilesByMediaType && cfg.AllowedFilesMode != AllowedFilesByExtensions {
|
||||||
|
cfg.AllowedFilesMode = defaults().AllowedFilesMode
|
||||||
|
}
|
||||||
|
cfg.EnabledMediaTypes = NormalizeMediaTypes(cfg.EnabledMediaTypes)
|
||||||
|
if len(cfg.EnabledMediaTypes) == 0 {
|
||||||
|
cfg.EnabledMediaTypes = defaults().EnabledMediaTypes
|
||||||
|
}
|
||||||
|
cfg.AllowedExtensions = NormalizeExtensions(cfg.AllowedExtensions)
|
||||||
|
if len(cfg.AllowedExtensions) == 0 {
|
||||||
|
cfg.AllowedExtensions = defaults().AllowedExtensions
|
||||||
|
}
|
||||||
|
cfg.MediaPath = NormalizeMediaPath(cfg.MediaPath)
|
||||||
|
cfg.Sources = NormalizeSources(cfg.Sources, cfg.MediaPath)
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,6 +115,8 @@ func Save(path string, cfg *Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) Validate() error {
|
func (c *Config) Validate() error {
|
||||||
|
c.MediaPath = NormalizeMediaPath(c.MediaPath)
|
||||||
|
c.Sources = NormalizeSources(c.Sources, c.MediaPath)
|
||||||
if c.ReserveFreeGB < 0 {
|
if c.ReserveFreeGB < 0 {
|
||||||
return errors.New("reserve_free_gb must be >= 0")
|
return errors.New("reserve_free_gb must be >= 0")
|
||||||
}
|
}
|
||||||
@@ -102,9 +133,185 @@ func (c *Config) Validate() error {
|
|||||||
default:
|
default:
|
||||||
return errors.New("file_select_mode must be 'new' or 'all'")
|
return errors.New("file_select_mode must be 'new' or 'all'")
|
||||||
}
|
}
|
||||||
|
switch c.AllowedFilesMode {
|
||||||
|
case "", AllowedFilesByMediaType:
|
||||||
|
c.AllowedFilesMode = AllowedFilesByMediaType
|
||||||
|
c.EnabledMediaTypes = NormalizeMediaTypes(c.EnabledMediaTypes)
|
||||||
|
if len(c.EnabledMediaTypes) == 0 {
|
||||||
|
return errors.New("enabled_media_types must contain at least one of: audio, video, photo")
|
||||||
|
}
|
||||||
|
case AllowedFilesByExtensions:
|
||||||
|
c.AllowedExtensions = NormalizeExtensions(c.AllowedExtensions)
|
||||||
|
if len(c.AllowedExtensions) == 0 {
|
||||||
|
return errors.New("allowed_extensions must contain at least one file extension")
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return errors.New("allowed_files_mode must be 'media_types' or 'extensions'")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DefaultEnabledMediaTypes() []string {
|
||||||
|
return []string{MediaTypeAudio, MediaTypeVideo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultAllowedExtensions() []string {
|
||||||
|
return extensionsForMediaTypes(DefaultEnabledMediaTypes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func BuiltInMediaTypeExtensions() map[string][]string {
|
||||||
|
return map[string][]string{
|
||||||
|
MediaTypeAudio: {
|
||||||
|
".aac", ".aif", ".aiff", ".alac", ".ape", ".flac", ".m4a", ".mp2", ".mp3", ".ogg", ".opus", ".wav", ".wma",
|
||||||
|
},
|
||||||
|
MediaTypeVideo: {
|
||||||
|
".3gp", ".avi", ".m2ts", ".m4v", ".mkv", ".mov", ".mp4", ".mpeg", ".mpg", ".mts", ".ts", ".webm", ".wmv",
|
||||||
|
},
|
||||||
|
MediaTypePhoto: {
|
||||||
|
".bmp", ".gif", ".heic", ".heif", ".jpeg", ".jpg", ".png", ".tif", ".tiff", ".webp",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NormalizeMediaTypes(items []string) []string {
|
||||||
|
order := []string{MediaTypeAudio, MediaTypeVideo, MediaTypePhoto}
|
||||||
|
allowed := make(map[string]struct{}, len(order))
|
||||||
|
for _, item := range order {
|
||||||
|
allowed[item] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
seen := make(map[string]struct{}, len(items))
|
||||||
|
result := make([]string, 0, len(order))
|
||||||
|
for _, item := range items {
|
||||||
|
value := strings.ToLower(strings.TrimSpace(item))
|
||||||
|
if _, ok := allowed[value]; !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := seen[value]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[value] = struct{}{}
|
||||||
|
}
|
||||||
|
for _, item := range order {
|
||||||
|
if _, ok := seen[item]; ok {
|
||||||
|
result = append(result, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func NormalizeExtensions(items []string) []string {
|
||||||
|
seen := make(map[string]struct{}, len(items))
|
||||||
|
result := make([]string, 0, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
value := normalizeExtension(item)
|
||||||
|
if value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := seen[value]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[value] = struct{}{}
|
||||||
|
result = append(result, value)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeExtension(value string) string {
|
||||||
|
value = strings.ToLower(strings.TrimSpace(value))
|
||||||
|
value = strings.TrimPrefix(value, "*")
|
||||||
|
if value == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(value, ".") {
|
||||||
|
value = "." + value
|
||||||
|
}
|
||||||
|
if len(value) < 2 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for _, ch := range value[1:] {
|
||||||
|
switch {
|
||||||
|
case ch >= 'a' && ch <= 'z':
|
||||||
|
case ch >= '0' && ch <= '9':
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) EffectiveAllowedExtensions() []string {
|
||||||
|
switch c.AllowedFilesMode {
|
||||||
|
case AllowedFilesByExtensions:
|
||||||
|
if items := NormalizeExtensions(c.AllowedExtensions); len(items) > 0 {
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
types := NormalizeMediaTypes(c.EnabledMediaTypes)
|
||||||
|
if len(types) == 0 {
|
||||||
|
types = DefaultEnabledMediaTypes()
|
||||||
|
}
|
||||||
|
return extensionsForMediaTypes(types)
|
||||||
|
}
|
||||||
|
return DefaultAllowedExtensions()
|
||||||
|
}
|
||||||
|
|
||||||
|
func extensionsForMediaTypes(items []string) []string {
|
||||||
|
sets := BuiltInMediaTypeExtensions()
|
||||||
|
result := make([]string, 0)
|
||||||
|
seen := make(map[string]struct{})
|
||||||
|
for _, mediaType := range NormalizeMediaTypes(items) {
|
||||||
|
for _, ext := range sets[mediaType] {
|
||||||
|
if _, ok := seen[ext]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[ext] = struct{}{}
|
||||||
|
result = append(result, ext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func NormalizeMediaPath(value string) string {
|
||||||
|
value = strings.TrimSpace(value)
|
||||||
|
if value == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return filepath.Clean(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NormalizeSources(items []SourceFolder, mediaPath string) []SourceFolder {
|
||||||
|
seen := make(map[string]struct{}, len(items))
|
||||||
|
result := make([]SourceFolder, 0, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
path := normalizeSourcePath(item.Path, mediaPath)
|
||||||
|
if path == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := seen[path]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[path] = struct{}{}
|
||||||
|
result = append(result, SourceFolder{
|
||||||
|
Path: path,
|
||||||
|
Enabled: item.Enabled,
|
||||||
|
Root: item.Root,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeSourcePath(value string, mediaPath string) string {
|
||||||
|
value = strings.TrimSpace(value)
|
||||||
|
if value == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if !filepath.IsAbs(value) && mediaPath != "" {
|
||||||
|
value = filepath.Join(mediaPath, value)
|
||||||
|
}
|
||||||
|
return filepath.Clean(value)
|
||||||
|
}
|
||||||
|
|
||||||
func NormalizeDestFolder(value string) (string, error) {
|
func NormalizeDestFolder(value string) (string, error) {
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
if value == "" {
|
if value == "" {
|
||||||
|
|||||||
68
internal/config/config_test.go
Normal file
68
internal/config/config_test.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestDefaultsAllowedFiles(t *testing.T) {
|
||||||
|
cfg := defaults()
|
||||||
|
|
||||||
|
if cfg.AllowedFilesMode != AllowedFilesByMediaType {
|
||||||
|
t.Fatalf("allowed files mode = %q, want %q", cfg.AllowedFilesMode, AllowedFilesByMediaType)
|
||||||
|
}
|
||||||
|
|
||||||
|
wantTypes := []string{MediaTypeAudio, MediaTypeVideo}
|
||||||
|
if len(cfg.EnabledMediaTypes) != len(wantTypes) {
|
||||||
|
t.Fatalf("enabled media types len = %d, want %d", len(cfg.EnabledMediaTypes), len(wantTypes))
|
||||||
|
}
|
||||||
|
for i, want := range wantTypes {
|
||||||
|
if cfg.EnabledMediaTypes[i] != want {
|
||||||
|
t.Fatalf("enabled media types[%d] = %q, want %q", i, cfg.EnabledMediaTypes[i], want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exts := cfg.EffectiveAllowedExtensions()
|
||||||
|
if containsString(exts, ".jpg") {
|
||||||
|
t.Fatalf("default extensions unexpectedly include photo files: %v", exts)
|
||||||
|
}
|
||||||
|
if !containsString(exts, ".mp3") || !containsString(exts, ".mp4") {
|
||||||
|
t.Fatalf("default extensions = %v, want audio/video entries", exts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateAllowedFilesModeExtensions(t *testing.T) {
|
||||||
|
cfg := defaults()
|
||||||
|
cfg.AllowedFilesMode = AllowedFilesByExtensions
|
||||||
|
cfg.AllowedExtensions = []string{" mp3 ", ".MP4", "*.jpg", ".mp3"}
|
||||||
|
|
||||||
|
if err := cfg.Validate(); err != nil {
|
||||||
|
t.Fatalf("Validate() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := []string{".mp3", ".mp4", ".jpg"}
|
||||||
|
if len(cfg.AllowedExtensions) != len(want) {
|
||||||
|
t.Fatalf("allowed extensions len = %d, want %d", len(cfg.AllowedExtensions), len(want))
|
||||||
|
}
|
||||||
|
for i, item := range want {
|
||||||
|
if cfg.AllowedExtensions[i] != item {
|
||||||
|
t.Fatalf("allowed extensions[%d] = %q, want %q", i, cfg.AllowedExtensions[i], item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateRejectsEmptyAllowedFiles(t *testing.T) {
|
||||||
|
cfg := defaults()
|
||||||
|
cfg.AllowedFilesMode = AllowedFilesByExtensions
|
||||||
|
cfg.AllowedExtensions = nil
|
||||||
|
|
||||||
|
if err := cfg.Validate(); err == nil {
|
||||||
|
t.Fatal("Validate() error = nil, want non-nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsString(items []string, want string) bool {
|
||||||
|
for _, item := range items {
|
||||||
|
if item == want {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -5,9 +5,10 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"hash/fnv"
|
||||||
|
"io"
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -18,17 +19,22 @@ import (
|
|||||||
"jukebox_maker/internal/db"
|
"jukebox_maker/internal/db"
|
||||||
"jukebox_maker/internal/disk"
|
"jukebox_maker/internal/disk"
|
||||||
"jukebox_maker/internal/task"
|
"jukebox_maker/internal/task"
|
||||||
|
"jukebox_maker/internal/transcoder"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
DiskID string
|
DiskID string
|
||||||
MountPath string
|
MountPath string
|
||||||
MediaPath string
|
MediaPath string
|
||||||
DestFolder string // subfolder on disk, default "media"
|
DestFolder string // subfolder on disk, default "media"
|
||||||
SourceRules []config.SourceFolder
|
SourceRules []config.SourceFolder
|
||||||
ReserveFreeGB float64
|
AllowedExtensions []string
|
||||||
OverwriteMode config.OverwriteMode
|
ReserveFreeGB float64
|
||||||
FileSelectMode config.FileSelectMode
|
OverwriteMode config.OverwriteMode
|
||||||
|
FileSelectMode config.FileSelectMode
|
||||||
|
Transcode *disk.TranscodeProfile // nil = не транскодировать
|
||||||
|
// ShuffleDepth: -1=выкл, 0=файлы вразнобой, 1+=группировка по папке на глубине N
|
||||||
|
ShuffleDepth int
|
||||||
}
|
}
|
||||||
|
|
||||||
type Copier struct {
|
type Copier struct {
|
||||||
@@ -240,7 +246,7 @@ func (c *Copier) run(ctx context.Context, taskID string, opts Options, database
|
|||||||
if t, ok := c.tasks.Get(taskID); ok {
|
if t, ok := c.tasks.Get(taskID); ok {
|
||||||
_ = database.UpdateTask(*t)
|
_ = database.UpdateTask(*t)
|
||||||
}
|
}
|
||||||
files, err := buildFileList(opts.MediaPath, opts.SourceRules, copiedPaths)
|
files, err := buildFileList(opts.MediaPath, opts.SourceRules, copiedPaths, opts.AllowedExtensions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err)
|
fail(err)
|
||||||
return
|
return
|
||||||
@@ -250,8 +256,7 @@ func (c *Copier) run(ctx context.Context, taskID string, opts Options, database
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// случайный порядок — выбираем что копировать до начала копирования
|
files = applyShuffleDepth(files, opts.ShuffleDepth)
|
||||||
rand.Shuffle(len(files), func(i, j int) { files[i], files[j] = files[j], files[i] })
|
|
||||||
|
|
||||||
_, free, err := disk.DiskUsage(opts.MountPath)
|
_, free, err := disk.DiskUsage(opts.MountPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -322,8 +327,14 @@ func (c *Copier) run(ctx context.Context, taskID string, opts Options, database
|
|||||||
}
|
}
|
||||||
|
|
||||||
dstAbs := filepath.Join(destRoot, f.relPath)
|
dstAbs := filepath.Join(destRoot, f.relPath)
|
||||||
if err := rsyncFile(ctx, f.srcAbs, dstAbs); err != nil {
|
var fileErr error
|
||||||
if errors.Is(err, context.Canceled) {
|
if opts.Transcode != nil && isVideoFile(f.srcAbs) {
|
||||||
|
fileErr = c.processVideo(ctx, taskID, database, opts.Transcode, f.srcAbs, dstAbs)
|
||||||
|
} else {
|
||||||
|
fileErr = copyFile(ctx, f.srcAbs, dstAbs)
|
||||||
|
}
|
||||||
|
if fileErr != nil {
|
||||||
|
if errors.Is(fileErr, context.Canceled) {
|
||||||
c.tasks.Update(taskID, func(t *task.Task) {
|
c.tasks.Update(taskID, func(t *task.Task) {
|
||||||
t.Status = task.StatusCanceled
|
t.Status = task.StatusCanceled
|
||||||
t.Message = "Canceled"
|
t.Message = "Canceled"
|
||||||
@@ -351,18 +362,91 @@ func (c *Copier) run(ctx context.Context, taskID string, opts Options, database
|
|||||||
setStatus(task.StatusSuccess, fmt.Sprintf("Done. Copied %d files.", copied), 100)
|
setStatus(task.StatusSuccess, fmt.Sprintf("Done. Copied %d files.", copied), 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// videoExtensions — расширения видеофайлов из встроенного справочника.
|
||||||
|
var videoExtensions = func() map[string]struct{} {
|
||||||
|
exts := config.BuiltInMediaTypeExtensions()[config.MediaTypeVideo]
|
||||||
|
set := make(map[string]struct{}, len(exts))
|
||||||
|
for _, e := range exts {
|
||||||
|
set[e] = struct{}{}
|
||||||
|
}
|
||||||
|
return set
|
||||||
|
}()
|
||||||
|
|
||||||
|
func isVideoFile(path string) bool {
|
||||||
|
_, ok := videoExtensions[strings.ToLower(filepath.Ext(path))]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// processVideo определяет: транскодировать или скопировать файл.
|
||||||
|
func (c *Copier) processVideo(ctx context.Context, taskID string, database *db.DB, profile *disk.TranscodeProfile, src, dst string) error {
|
||||||
|
info, err := transcoder.ProbeVideo(src)
|
||||||
|
if err != nil {
|
||||||
|
// Не смогли зондировать — просто копируем
|
||||||
|
return copyFile(ctx, src, dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !transcoder.NeedsTranscode(info, profile) {
|
||||||
|
return copyFile(ctx, src, dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Меняем расширение выходного файла под формат контейнера
|
||||||
|
ext := transcoder.OutputExt(profile.OutputFormat)
|
||||||
|
dstTranscoded := strings.TrimSuffix(dst, filepath.Ext(dst)) + ext
|
||||||
|
|
||||||
|
srcFPS := fmt.Sprintf("%.2f", info.FPS)
|
||||||
|
msg := fmt.Sprintf("Transcoding %s (%s/%dch/%sfps → %s/%s/%dfps %s)",
|
||||||
|
filepath.Base(src),
|
||||||
|
info.Codec, info.AudioChannels, srcFPS,
|
||||||
|
profile.VideoCodec, profile.AudioCodec, profile.MaxFPS, profile.OutputFormat,
|
||||||
|
)
|
||||||
|
c.tasks.Update(taskID, func(t *task.Task) {
|
||||||
|
t.Phase = task.PhaseTranscoding
|
||||||
|
t.Message = msg
|
||||||
|
})
|
||||||
|
if t, ok := c.tasks.Get(taskID); ok {
|
||||||
|
_ = database.UpdateTask(*t)
|
||||||
|
}
|
||||||
|
|
||||||
|
progressFn := func(pct float64) {
|
||||||
|
c.tasks.Update(taskID, func(t *task.Task) {
|
||||||
|
t.Progress = int(pct * 100)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return transcoder.Transcode(ctx, transcoder.Options{
|
||||||
|
Input: src,
|
||||||
|
Output: dstTranscoded,
|
||||||
|
Profile: profile,
|
||||||
|
SourceInfo: info,
|
||||||
|
}, progressFn)
|
||||||
|
}
|
||||||
|
|
||||||
type fileEntry struct {
|
type fileEntry struct {
|
||||||
srcAbs string
|
srcAbs string
|
||||||
relPath string // relative to /media
|
relPath string // relative to /media
|
||||||
size int64
|
size int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildFileList(mediaPath string, rules []config.SourceFolder, skip map[string]struct{}) ([]fileEntry, error) {
|
func buildFileList(mediaPath string, rules []config.SourceFolder, skip map[string]struct{}, allowedExtensions []string) ([]fileEntry, error) {
|
||||||
roots, ruleMap := normalizeSourceRules(rules)
|
_ = mediaPath
|
||||||
|
roots, selectedRoots, ruleMap := normalizeSourceRules(rules)
|
||||||
|
aliases := sourceAliases(roots)
|
||||||
|
allowedExts := makeAllowedExtensionSet(allowedExtensions)
|
||||||
|
|
||||||
var result []fileEntry
|
var result []fileEntry
|
||||||
for _, src := range roots {
|
for _, src := range selectedRoots {
|
||||||
dir := filepath.Join(mediaPath, src)
|
root := owningRoot(src, roots)
|
||||||
|
if root == "" {
|
||||||
|
root = src
|
||||||
|
}
|
||||||
|
alias := aliases[root]
|
||||||
|
if alias == "" {
|
||||||
|
alias = filepath.Base(root)
|
||||||
|
if alias == "." || alias == "" || alias == string(filepath.Separator) {
|
||||||
|
alias = "source-" + shortHash(root)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dir := src
|
||||||
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
|
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
|
||||||
if err != nil || d.IsDir() {
|
if err != nil || d.IsDir() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -371,29 +455,36 @@ func buildFileList(mediaPath string, rules []config.SourceFolder, skip map[strin
|
|||||||
if path == dir {
|
if path == dir {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
rel, relErr := filepath.Rel(mediaPath, path)
|
rel, relErr := filepath.Rel(root, path)
|
||||||
if relErr != nil {
|
if relErr != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
rel = filepath.ToSlash(rel)
|
rel = filepath.ToSlash(rel)
|
||||||
if !isPathEnabled(rel, ruleMap) && !hasEnabledDescendant(rel, ruleMap) {
|
if !isPathEnabled(path, ruleMap) && !hasEnabledDescendant(path, ruleMap) {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
rel, _ := filepath.Rel(mediaPath, path)
|
if !isPathEnabled(path, ruleMap) {
|
||||||
rel = filepath.ToSlash(rel)
|
|
||||||
if !isPathEnabled(rel, ruleMap) {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if !isExtensionAllowed(path, allowedExts) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
rel, _ := filepath.Rel(root, path)
|
||||||
|
rel = filepath.ToSlash(rel)
|
||||||
|
destRel := filepath.ToSlash(filepath.Join(alias, rel))
|
||||||
if _, skipped := skip[rel]; skipped {
|
if _, skipped := skip[rel]; skipped {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if _, skipped := skip[destRel]; skipped {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
info, err := d.Info()
|
info, err := d.Info()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
result = append(result, fileEntry{srcAbs: path, relPath: rel, size: info.Size()})
|
result = append(result, fileEntry{srcAbs: path, relPath: destRel, size: info.Size()})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -403,30 +494,56 @@ func buildFileList(mediaPath string, rules []config.SourceFolder, skip map[strin
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeSourceRules(rules []config.SourceFolder) ([]string, map[string]bool) {
|
func makeAllowedExtensionSet(items []string) map[string]struct{} {
|
||||||
|
normalized := config.NormalizeExtensions(items)
|
||||||
|
if len(normalized) == 0 {
|
||||||
|
normalized = config.DefaultAllowedExtensions()
|
||||||
|
}
|
||||||
|
result := make(map[string]struct{}, len(normalized))
|
||||||
|
for _, item := range normalized {
|
||||||
|
result[item] = struct{}{}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func isExtensionAllowed(path string, allowed map[string]struct{}) bool {
|
||||||
|
ext := strings.ToLower(filepath.Ext(path))
|
||||||
|
if ext == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, ok := allowed[ext]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeSourceRules(rules []config.SourceFolder) ([]string, []string, map[string]bool) {
|
||||||
ruleMap := make(map[string]bool, len(rules))
|
ruleMap := make(map[string]bool, len(rules))
|
||||||
|
rootSet := make(map[string]struct{})
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
src := filepath.ToSlash(filepath.Clean(strings.TrimSpace(rule.Path)))
|
src := filepath.Clean(strings.TrimSpace(rule.Path))
|
||||||
src = strings.TrimPrefix(src, "./")
|
|
||||||
src = strings.TrimPrefix(src, "/")
|
|
||||||
if src == "" || src == "." {
|
if src == "" || src == "." {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if src == ".." || strings.HasPrefix(src, "../") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ruleMap[src] = rule.Enabled
|
ruleMap[src] = rule.Enabled
|
||||||
|
if rule.Root {
|
||||||
|
rootSet[src] = struct{}{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var roots []string
|
var roots []string
|
||||||
|
for src := range rootSet {
|
||||||
|
roots = append(roots, src)
|
||||||
|
}
|
||||||
|
sort.Strings(roots)
|
||||||
|
|
||||||
|
var selectedRoots []string
|
||||||
for src, enabled := range ruleMap {
|
for src, enabled := range ruleMap {
|
||||||
if !enabled || hasEnabledAncestor(src, ruleMap) {
|
if !enabled || hasEnabledAncestor(src, ruleMap) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
roots = append(roots, src)
|
selectedRoots = append(selectedRoots, src)
|
||||||
}
|
}
|
||||||
sort.Strings(roots)
|
sort.Strings(selectedRoots)
|
||||||
return roots, ruleMap
|
return roots, selectedRoots, ruleMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func hasEnabledAncestor(path string, ruleMap map[string]bool) bool {
|
func hasEnabledAncestor(path string, ruleMap map[string]bool) bool {
|
||||||
@@ -439,9 +556,8 @@ func hasEnabledAncestor(path string, ruleMap map[string]bool) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func hasEnabledDescendant(path string, ruleMap map[string]bool) bool {
|
func hasEnabledDescendant(path string, ruleMap map[string]bool) bool {
|
||||||
prefix := path + "/"
|
|
||||||
for other, enabled := range ruleMap {
|
for other, enabled := range ruleMap {
|
||||||
if enabled && strings.HasPrefix(other, prefix) {
|
if enabled && isPathInside(path, other) && other != path {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -458,36 +574,202 @@ func isPathEnabled(path string, ruleMap map[string]bool) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parentSourcePath(path string) string {
|
func parentSourcePath(path string) string {
|
||||||
idx := strings.LastIndex(path, "/")
|
parent := filepath.Dir(path)
|
||||||
if idx < 0 {
|
if parent == "." || parent == path {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return path[:idx]
|
return parent
|
||||||
}
|
}
|
||||||
|
|
||||||
// rsyncFile copies src to dst using rsync with resume support.
|
func owningRoot(path string, roots []string) string {
|
||||||
// --partial keeps partial files on interruption.
|
var best string
|
||||||
// --append-verify resumes partial transfers and verifies checksums.
|
for _, root := range roots {
|
||||||
func rsyncFile(ctx context.Context, src, dst string) error {
|
if isPathInside(root, path) {
|
||||||
|
if len(root) > len(best) {
|
||||||
|
best = root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return best
|
||||||
|
}
|
||||||
|
|
||||||
|
func sourceAliases(roots []string) map[string]string {
|
||||||
|
counts := make(map[string]int, len(roots))
|
||||||
|
for _, root := range roots {
|
||||||
|
counts[strings.ToLower(filepath.Base(root))]++
|
||||||
|
}
|
||||||
|
|
||||||
|
aliases := make(map[string]string, len(roots))
|
||||||
|
for _, root := range roots {
|
||||||
|
base := filepath.Base(root)
|
||||||
|
if base == "." || base == string(filepath.Separator) || base == "" {
|
||||||
|
base = "source"
|
||||||
|
}
|
||||||
|
key := strings.ToLower(base)
|
||||||
|
if counts[key] > 1 {
|
||||||
|
base = fmt.Sprintf("%s-%s", base, shortHash(root))
|
||||||
|
}
|
||||||
|
aliases[root] = base
|
||||||
|
}
|
||||||
|
return aliases
|
||||||
|
}
|
||||||
|
|
||||||
|
func shortHash(value string) string {
|
||||||
|
h := fnv.New32a()
|
||||||
|
_, _ = h.Write([]byte(value))
|
||||||
|
return fmt.Sprintf("%08x", h.Sum32())[:6]
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPathInside(base, candidate string) bool {
|
||||||
|
if candidate == base {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
rel, err := filepath.Rel(base, candidate)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return rel != "." && rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator))
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyFile(ctx context.Context, src, dst string) error {
|
||||||
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cmd := exec.CommandContext(ctx, "rsync",
|
|
||||||
"--partial",
|
srcFile, err := os.Open(src)
|
||||||
"--append-verify",
|
|
||||||
"--times",
|
|
||||||
"--no-perms",
|
|
||||||
"--no-owner",
|
|
||||||
"--no-group",
|
|
||||||
"--chmod=ugo=rwx",
|
|
||||||
src, dst,
|
|
||||||
)
|
|
||||||
out, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ctx.Err() != nil {
|
return err
|
||||||
return ctx.Err()
|
}
|
||||||
|
defer srcFile.Close()
|
||||||
|
|
||||||
|
srcInfo, err := srcFile.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := int64(0)
|
||||||
|
if dstInfo, err := os.Stat(dst); err == nil {
|
||||||
|
switch {
|
||||||
|
case dstInfo.Size() < srcInfo.Size():
|
||||||
|
offset = dstInfo.Size()
|
||||||
|
case dstInfo.Size() == srcInfo.Size():
|
||||||
|
return os.Chtimes(dst, srcInfo.ModTime(), srcInfo.ModTime())
|
||||||
|
default:
|
||||||
|
if err := os.Remove(dst); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return fmt.Errorf("rsync: %w: %s", err, out)
|
} else if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if offset > 0 {
|
||||||
|
if _, err := srcFile.Seek(offset, io.SeekStart); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer dstFile.Close()
|
||||||
|
|
||||||
|
if offset > 0 {
|
||||||
|
if _, err := dstFile.Seek(offset, io.SeekStart); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if err := dstFile.Truncate(0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 1024*1024)
|
||||||
|
for {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
nr, readErr := srcFile.Read(buf)
|
||||||
|
if nr > 0 {
|
||||||
|
nw, writeErr := dstFile.Write(buf[:nr])
|
||||||
|
if writeErr != nil {
|
||||||
|
return writeErr
|
||||||
|
}
|
||||||
|
if nw != nr {
|
||||||
|
return io.ErrShortWrite
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if readErr != nil {
|
||||||
|
if errors.Is(readErr, io.EOF) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return readErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dstFile.Sync(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.Chtimes(dst, srcInfo.ModTime(), srcInfo.ModTime()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dstInfo, err := os.Stat(dst)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if dstInfo.Size() != srcInfo.Size() {
|
||||||
|
return fmt.Errorf("copied size mismatch for %s", filepath.Base(src))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// applyShuffleDepth упорядочивает файлы по заданной глубине шафлера.
|
||||||
|
// depth < 0 → оригинальный порядок (без шафла)
|
||||||
|
// depth == 0 → все файлы перемешиваются случайно
|
||||||
|
// depth >= 1 → файлы группируются по папке на уровне depth от корня /media,
|
||||||
|
// группы перемешиваются, внутри каждой группы порядок сохраняется.
|
||||||
|
func applyShuffleDepth(files []fileEntry, depth int) []fileEntry {
|
||||||
|
if depth < 0 || len(files) == 0 {
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
if depth == 0 {
|
||||||
|
rand.Shuffle(len(files), func(i, j int) { files[i], files[j] = files[j], files[i] })
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
type group struct {
|
||||||
|
key string
|
||||||
|
files []fileEntry
|
||||||
|
}
|
||||||
|
groupMap := make(map[string]*group, 64)
|
||||||
|
var order []string
|
||||||
|
for _, f := range files {
|
||||||
|
key := folderKeyAtDepth(f.relPath, depth)
|
||||||
|
if _, ok := groupMap[key]; !ok {
|
||||||
|
groupMap[key] = &group{key: key}
|
||||||
|
order = append(order, key)
|
||||||
|
}
|
||||||
|
groupMap[key].files = append(groupMap[key].files, f)
|
||||||
|
}
|
||||||
|
rand.Shuffle(len(order), func(i, j int) { order[i], order[j] = order[j], order[i] })
|
||||||
|
|
||||||
|
result := make([]fileEntry, 0, len(files))
|
||||||
|
for _, key := range order {
|
||||||
|
result = append(result, groupMap[key].files...)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// folderKeyAtDepth возвращает путь к папке глубины depth из relPath.
|
||||||
|
// relPath вида "anime/Naruto/Season1/ep01.mkv", depth=2 → "anime/Naruto"
|
||||||
|
func folderKeyAtDepth(relPath string, depth int) string {
|
||||||
|
relPath = filepath.ToSlash(relPath)
|
||||||
|
parts := strings.Split(relPath, "/")
|
||||||
|
maxDepth := len(parts) - 1 // последний элемент — имя файла
|
||||||
|
if depth >= maxDepth {
|
||||||
|
depth = maxDepth
|
||||||
|
}
|
||||||
|
return strings.Join(parts[:depth], "/")
|
||||||
|
}
|
||||||
|
|||||||
54
internal/copier/copier_test.go
Normal file
54
internal/copier/copier_test.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package copier
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuildFileListFiltersAllowedExtensions(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
source := filepath.Join(root, "music")
|
||||||
|
if err := os.MkdirAll(filepath.Join(source, "nested"), 0o755); err != nil {
|
||||||
|
t.Fatalf("MkdirAll() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
files := map[string]string{
|
||||||
|
filepath.Join(source, "track.mp3"): "audio",
|
||||||
|
filepath.Join(source, "clip.mp4"): "video",
|
||||||
|
filepath.Join(source, "cover.jpg"): "photo",
|
||||||
|
filepath.Join(source, "nested", "note.txt"): "text",
|
||||||
|
}
|
||||||
|
for path, body := range files {
|
||||||
|
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile(%q) error = %v", path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := buildFileList("", []config.SourceFolder{
|
||||||
|
{Path: source, Enabled: true, Root: true},
|
||||||
|
}, nil, config.DefaultAllowedExtensions())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("buildFileList() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(items) != 2 {
|
||||||
|
t.Fatalf("buildFileList() len = %d, want 2", len(items))
|
||||||
|
}
|
||||||
|
|
||||||
|
got := make(map[string]struct{}, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
got[filepath.Base(item.relPath)] = struct{}{}
|
||||||
|
}
|
||||||
|
if _, ok := got["track.mp3"]; !ok {
|
||||||
|
t.Fatalf("missing mp3 file: %v", got)
|
||||||
|
}
|
||||||
|
if _, ok := got["clip.mp4"]; !ok {
|
||||||
|
t.Fatalf("missing mp4 file: %v", got)
|
||||||
|
}
|
||||||
|
if _, ok := got["cover.jpg"]; ok {
|
||||||
|
t.Fatalf("unexpected jpg file: %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
20
internal/dialog/pickfolder_darwin.go
Normal file
20
internal/dialog/pickfolder_darwin.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package dialog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PickFolder() (string, error) {
|
||||||
|
cmd := exec.Command("osascript", "-e", `POSIX path of (choose folder with prompt "Select a folder")`)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("folder selection failed: %s", strings.TrimSpace(string(out)))
|
||||||
|
}
|
||||||
|
path := strings.TrimSpace(string(out))
|
||||||
|
if path == "" {
|
||||||
|
return "", fmt.Errorf("folder selection canceled")
|
||||||
|
}
|
||||||
|
return strings.TrimSuffix(path, "/"), nil
|
||||||
|
}
|
||||||
9
internal/dialog/pickfolder_other.go
Normal file
9
internal/dialog/pickfolder_other.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
//go:build !darwin && !windows
|
||||||
|
|
||||||
|
package dialog
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func PickFolder() (string, error) {
|
||||||
|
return "", fmt.Errorf("native folder picker is not supported on this platform")
|
||||||
|
}
|
||||||
21
internal/dialog/pickfolder_windows.go
Normal file
21
internal/dialog/pickfolder_windows.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package dialog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PickFolder() (string, error) {
|
||||||
|
script := `Add-Type -AssemblyName System.Windows.Forms; $dialog = New-Object System.Windows.Forms.FolderBrowserDialog; $dialog.ShowNewFolderButton = $false; if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { [Console]::Write($dialog.SelectedPath) }`
|
||||||
|
cmd := exec.Command("powershell", "-NoProfile", "-STA", "-Command", script)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("folder selection failed: %s", strings.TrimSpace(string(out)))
|
||||||
|
}
|
||||||
|
path := strings.TrimSpace(string(out))
|
||||||
|
if path == "" {
|
||||||
|
return "", fmt.Errorf("folder selection canceled")
|
||||||
|
}
|
||||||
|
return path, nil
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
@@ -19,11 +18,12 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type DiskInfo struct {
|
type DiskInfo struct {
|
||||||
State DiskState `json:"state"`
|
State DiskState `json:"state"`
|
||||||
DiskID string `json:"disk_id"`
|
DiskID string `json:"disk_id"`
|
||||||
TotalBytes int64 `json:"total_bytes"`
|
TotalBytes int64 `json:"total_bytes"`
|
||||||
FreeBytes int64 `json:"free_bytes"`
|
FreeBytes int64 `json:"free_bytes"`
|
||||||
MountPath string `json:"mount_path"`
|
MountPath string `json:"mount_path"`
|
||||||
|
Profile *DiskProfile `json:"profile,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const MarkerDir = ".jukebox"
|
const MarkerDir = ".jukebox"
|
||||||
@@ -56,33 +56,12 @@ func Probe(mountPath string) (DiskInfo, error) {
|
|||||||
|
|
||||||
info.DiskID = strings.TrimSpace(string(data))
|
info.DiskID = strings.TrimSpace(string(data))
|
||||||
info.State = DiskKnown
|
info.State = DiskKnown
|
||||||
|
if p, err := LoadProfile(mountPath); err == nil {
|
||||||
|
info.Profile = p
|
||||||
|
}
|
||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsMountPoint(path string) bool {
|
|
||||||
pathInfo, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
parent := filepath.Dir(filepath.Clean(path))
|
|
||||||
parentInfo, err := os.Stat(parent)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
pathStat, ok := pathInfo.Sys().(*syscall.Stat_t)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
parentStat, ok := parentInfo.Sys().(*syscall.Stat_t)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return pathStat.Dev != parentStat.Dev
|
|
||||||
}
|
|
||||||
|
|
||||||
func CheckWritable(path string) error {
|
func CheckWritable(path string) error {
|
||||||
f, err := os.CreateTemp(path, ".jukebox-writecheck-*")
|
f, err := os.CreateTemp(path, ".jukebox-writecheck-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -106,19 +85,10 @@ func InitDisk(mountPath string) (string, error) {
|
|||||||
if err := os.WriteFile(idPath, []byte(id), 0o644); err != nil {
|
if err := os.WriteFile(idPath, []byte(id), 0o644); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
_ = SaveProfile(mountPath, DefaultProfile())
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DBPath(mountPath string) string {
|
func DBPath(mountPath string) string {
|
||||||
return filepath.Join(mountPath, MarkerDir, "history.db")
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
73
internal/disk/profile.go
Normal file
73
internal/disk/profile.go
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package disk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DiskProfile struct {
|
||||||
|
DestFolder string `json:"dest_folder"`
|
||||||
|
OverwriteMode string `json:"overwrite_mode"`
|
||||||
|
FileSelectMode string `json:"file_select_mode"`
|
||||||
|
ReserveFreeGB float64 `json:"reserve_free_gb"`
|
||||||
|
AutoCopy bool `json:"auto_copy"`
|
||||||
|
// ShuffleDepth: -1=выкл, 0=файлы вразнобой, 1+=папки на глубине N от корня /media
|
||||||
|
ShuffleDepth int `json:"shuffle_depth"`
|
||||||
|
|
||||||
|
// nil = не транскодировать видео
|
||||||
|
Transcode *TranscodeProfile `json:"transcode,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TranscodeProfile struct {
|
||||||
|
VideoCodec string `json:"video_codec"` // "h264" | "h265" | "mpeg4"
|
||||||
|
MaxResolution string `json:"max_resolution"` // "480p" | "720p" | "1080p"
|
||||||
|
MaxVideoBitrate string `json:"max_video_bitrate"` // "1000k" | "2000k" | "" (без лимита)
|
||||||
|
MaxFPS int `json:"max_fps"` // 0=без лимита | 24 | 25 | 30
|
||||||
|
AudioCodec string `json:"audio_codec"` // "aac" | "mp3"
|
||||||
|
MaxAudioBitrate string `json:"max_audio_bitrate"` // "128k" | "192k" | ""
|
||||||
|
MaxAudioChannels int `json:"max_audio_channels"` // 2=стерео | 6=5.1
|
||||||
|
OutputFormat string `json:"output_format"` // "mp4" | "mkv" | "avi"
|
||||||
|
}
|
||||||
|
|
||||||
|
const profileFile = "profile.json"
|
||||||
|
|
||||||
|
func profilePath(mountPath string) string {
|
||||||
|
return filepath.Join(mountPath, MarkerDir, profileFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadProfile(mountPath string) (*DiskProfile, error) {
|
||||||
|
data, err := os.ReadFile(profilePath(mountPath))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var p DiskProfile
|
||||||
|
if err := json.Unmarshal(data, &p); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveProfile(mountPath string, p *DiskProfile) error {
|
||||||
|
data, err := json.MarshalIndent(p, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
path := profilePath(mountPath)
|
||||||
|
tmp := path + ".tmp"
|
||||||
|
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.Rename(tmp, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultProfile() *DiskProfile {
|
||||||
|
return &DiskProfile{
|
||||||
|
DestFolder: "media",
|
||||||
|
OverwriteMode: "skip",
|
||||||
|
FileSelectMode: "new",
|
||||||
|
ReserveFreeGB: 2.0,
|
||||||
|
AutoCopy: false,
|
||||||
|
ShuffleDepth: -1,
|
||||||
|
}
|
||||||
|
}
|
||||||
43
internal/disk/storage_unix.go
Normal file
43
internal/disk/storage_unix.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
//go:build !windows
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsMountPoint(path string) bool {
|
||||||
|
pathInfo, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
parent := filepath.Dir(filepath.Clean(path))
|
||||||
|
parentInfo, err := os.Stat(parent)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
pathStat, ok := pathInfo.Sys().(*syscall.Stat_t)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
parentStat, ok := parentInfo.Sys().(*syscall.Stat_t)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return pathStat.Dev != parentStat.Dev
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
40
internal/disk/storage_windows.go
Normal file
40
internal/disk/storage_windows.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
//go:build windows
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsMountPoint(path string) bool {
|
||||||
|
clean := filepath.Clean(path)
|
||||||
|
volume := filepath.VolumeName(clean)
|
||||||
|
if volume == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
root := volume + `\`
|
||||||
|
return strings.EqualFold(clean, root)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DiskUsage(mountPath string) (total, free int64, err error) {
|
||||||
|
root := filepath.Clean(mountPath)
|
||||||
|
if volume := filepath.VolumeName(root); volume != "" {
|
||||||
|
root = volume + `\`
|
||||||
|
}
|
||||||
|
|
||||||
|
pathPtr, err := windows.UTF16PtrFromString(root)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var freeBytesAvailable uint64
|
||||||
|
var totalNumberOfBytes uint64
|
||||||
|
var totalNumberOfFreeBytes uint64
|
||||||
|
if err := windows.GetDiskFreeSpaceEx(pathPtr, &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes); err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
return int64(totalNumberOfBytes), int64(freeBytesAvailable), nil
|
||||||
|
}
|
||||||
@@ -23,6 +23,7 @@ const (
|
|||||||
PhaseReplacing = "replacing"
|
PhaseReplacing = "replacing"
|
||||||
PhaseLoadingHistory = "loading_history"
|
PhaseLoadingHistory = "loading_history"
|
||||||
PhaseScanning = "scanning"
|
PhaseScanning = "scanning"
|
||||||
|
PhaseTranscoding = "transcoding"
|
||||||
PhaseCopying = "copying"
|
PhaseCopying = "copying"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
172
internal/transcoder/detect.go
Normal file
172
internal/transcoder/detect.go
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
package transcoder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/disk"
|
||||||
|
)
|
||||||
|
|
||||||
|
type VideoInfo struct {
|
||||||
|
Codec string
|
||||||
|
Width int
|
||||||
|
Height int
|
||||||
|
FPS float64
|
||||||
|
DurationSec float64
|
||||||
|
VideoBitrate int64
|
||||||
|
AudioCodec string
|
||||||
|
AudioChannels int
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProbeVideo(path string) (VideoInfo, error) {
|
||||||
|
out, err := exec.Command("ffprobe",
|
||||||
|
"-v", "quiet",
|
||||||
|
"-print_format", "json",
|
||||||
|
"-show_streams",
|
||||||
|
"-show_format",
|
||||||
|
path,
|
||||||
|
).Output()
|
||||||
|
if err != nil {
|
||||||
|
return VideoInfo{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var raw struct {
|
||||||
|
Streams []struct {
|
||||||
|
CodecType string `json:"codec_type"`
|
||||||
|
CodecName string `json:"codec_name"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Height int `json:"height"`
|
||||||
|
RFrameRate string `json:"r_frame_rate"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
Channels int `json:"channels"`
|
||||||
|
} `json:"streams"`
|
||||||
|
Format struct {
|
||||||
|
Duration string `json:"duration"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
} `json:"format"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(out, &raw); err != nil {
|
||||||
|
return VideoInfo{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var info VideoInfo
|
||||||
|
for _, s := range raw.Streams {
|
||||||
|
switch s.CodecType {
|
||||||
|
case "video":
|
||||||
|
info.Codec = s.CodecName
|
||||||
|
info.Width = s.Width
|
||||||
|
info.Height = s.Height
|
||||||
|
info.FPS = parseFraction(s.RFrameRate)
|
||||||
|
if br, err := strconv.ParseInt(s.BitRate, 10, 64); err == nil {
|
||||||
|
info.VideoBitrate = br
|
||||||
|
}
|
||||||
|
case "audio":
|
||||||
|
if info.AudioCodec == "" {
|
||||||
|
info.AudioCodec = s.CodecName
|
||||||
|
info.AudioChannels = s.Channels
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if d, err := strconv.ParseFloat(raw.Format.Duration, 64); err == nil {
|
||||||
|
info.DurationSec = d
|
||||||
|
}
|
||||||
|
// Если стрим-битрейт не известен — используем битрейт контейнера
|
||||||
|
if info.VideoBitrate == 0 {
|
||||||
|
if br, err := strconv.ParseInt(raw.Format.BitRate, 10, 64); err == nil {
|
||||||
|
info.VideoBitrate = br
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NeedsTranscode(info VideoInfo, p *disk.TranscodeProfile) bool {
|
||||||
|
// Несовместимый видеокодек
|
||||||
|
if normalizeCodec(info.Codec) != p.VideoCodec {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Разрешение превышает лимит
|
||||||
|
if maxH := maxHeight(p.MaxResolution); maxH > 0 && info.Height > maxH {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Битрейт видео превышает лимит
|
||||||
|
if p.MaxVideoBitrate != "" {
|
||||||
|
if maxBR := parseBitrate(p.MaxVideoBitrate); maxBR > 0 && info.VideoBitrate > maxBR {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FPS превышает лимит
|
||||||
|
if p.MaxFPS > 0 && info.FPS > float64(p.MaxFPS)+0.01 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Несовместимый аудиокодек
|
||||||
|
if p.AudioCodec != "" && normalizeCodec(info.AudioCodec) != p.AudioCodec {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Каналов больше лимита
|
||||||
|
if p.MaxAudioChannels > 0 && info.AudioChannels > p.MaxAudioChannels {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeCodec(codec string) string {
|
||||||
|
switch strings.ToLower(codec) {
|
||||||
|
case "hevc", "h265":
|
||||||
|
return "h265"
|
||||||
|
case "h264", "avc", "avc1":
|
||||||
|
return "h264"
|
||||||
|
case "mpeg4", "mp4v":
|
||||||
|
return "mpeg4"
|
||||||
|
case "aac":
|
||||||
|
return "aac"
|
||||||
|
case "mp3":
|
||||||
|
return "mp3"
|
||||||
|
default:
|
||||||
|
return strings.ToLower(codec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxHeight(res string) int {
|
||||||
|
switch res {
|
||||||
|
case "480p":
|
||||||
|
return 480
|
||||||
|
case "720p":
|
||||||
|
return 720
|
||||||
|
case "1080p":
|
||||||
|
return 1080
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseBitrate(s string) int64 {
|
||||||
|
s = strings.ToLower(strings.TrimSpace(s))
|
||||||
|
if strings.HasSuffix(s, "k") {
|
||||||
|
v, _ := strconv.ParseInt(s[:len(s)-1], 10, 64)
|
||||||
|
return v * 1000
|
||||||
|
}
|
||||||
|
v, _ := strconv.ParseInt(s, 10, 64)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseFraction(s string) float64 {
|
||||||
|
parts := strings.SplitN(s, "/", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
v, _ := strconv.ParseFloat(s, 64)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
num, _ := strconv.ParseFloat(parts[0], 64)
|
||||||
|
den, _ := strconv.ParseFloat(parts[1], 64)
|
||||||
|
if den == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return num / den
|
||||||
|
}
|
||||||
173
internal/transcoder/transcoder.go
Normal file
173
internal/transcoder/transcoder.go
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
package transcoder
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"jukebox_maker/internal/disk"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
Input string
|
||||||
|
Output string
|
||||||
|
Profile *disk.TranscodeProfile
|
||||||
|
SourceInfo VideoInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transcode запускает ffmpeg. progress вызывается с 0..1 по мере работы.
|
||||||
|
func Transcode(ctx context.Context, opts Options, progress func(float64)) error {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(opts.Output), 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
args := buildArgs(opts)
|
||||||
|
cmd := exec.CommandContext(ctx, "ffmpeg", args...)
|
||||||
|
|
||||||
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var stderr bytes.Buffer
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return fmt.Errorf("ffmpeg start: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Парсим прогресс из stdout (-progress pipe:1)
|
||||||
|
if opts.SourceInfo.DurationSec > 0 && progress != nil {
|
||||||
|
scanner := bufio.NewScanner(stdout)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if strings.HasPrefix(line, "out_time_us=") {
|
||||||
|
val := strings.TrimPrefix(line, "out_time_us=")
|
||||||
|
if us, err := strconv.ParseInt(val, 10, 64); err == nil && us > 0 {
|
||||||
|
sec := float64(us) / 1e6
|
||||||
|
pct := sec / opts.SourceInfo.DurationSec
|
||||||
|
if pct > 1 {
|
||||||
|
pct = 1
|
||||||
|
}
|
||||||
|
progress(pct)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cmd.Wait(); err != nil {
|
||||||
|
msg := strings.TrimSpace(stderr.String())
|
||||||
|
if msg != "" {
|
||||||
|
return fmt.Errorf("ffmpeg: %w: %s", err, msg)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("ffmpeg: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildArgs(opts Options) []string {
|
||||||
|
p := opts.Profile
|
||||||
|
src := opts.SourceInfo
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-i", opts.Input,
|
||||||
|
"-threads", "0",
|
||||||
|
"-progress", "pipe:1",
|
||||||
|
"-v", "error",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Видеокодек
|
||||||
|
args = append(args, "-c:v", ffmpegVideoCodec(p.VideoCodec))
|
||||||
|
|
||||||
|
// Битрейт — только если источник выше лимита
|
||||||
|
if p.MaxVideoBitrate != "" {
|
||||||
|
if targetBR := parseBitrate(p.MaxVideoBitrate); targetBR > 0 &&
|
||||||
|
(src.VideoBitrate == 0 || src.VideoBitrate > targetBR) {
|
||||||
|
args = append(args, "-b:v", p.MaxVideoBitrate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Масштаб + FPS — строим vf
|
||||||
|
var filters []string
|
||||||
|
if maxH := maxHeight(p.MaxResolution); maxH > 0 {
|
||||||
|
filters = append(filters, fmt.Sprintf("scale=-2:min(%d\\,ih)", maxH))
|
||||||
|
}
|
||||||
|
if p.MaxFPS > 0 {
|
||||||
|
targetFPS := p.MaxFPS
|
||||||
|
if src.FPS > 0 && src.FPS < float64(targetFPS) {
|
||||||
|
// Источник медленнее лимита — сохраняем исходный FPS
|
||||||
|
filters = append(filters, fmt.Sprintf("fps=%.3f", src.FPS))
|
||||||
|
} else {
|
||||||
|
filters = append(filters, fmt.Sprintf("fps=%d", targetFPS))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(filters) > 0 {
|
||||||
|
args = append(args, "-vf", strings.Join(filters, ","))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Аудиокодек
|
||||||
|
if p.AudioCodec != "" {
|
||||||
|
args = append(args, "-c:a", ffmpegAudioCodec(p.AudioCodec))
|
||||||
|
} else {
|
||||||
|
args = append(args, "-c:a", "copy")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Аудио-битрейт
|
||||||
|
if p.MaxAudioBitrate != "" {
|
||||||
|
args = append(args, "-b:a", p.MaxAudioBitrate)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Каналы — только даунмикс
|
||||||
|
if p.MaxAudioChannels > 0 && src.AudioChannels > p.MaxAudioChannels {
|
||||||
|
args = append(args, "-ac", strconv.Itoa(p.MaxAudioChannels))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Для mp4 — оптимизация для стриминга
|
||||||
|
if p.OutputFormat == "mp4" {
|
||||||
|
args = append(args, "-movflags", "+faststart")
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, "-y", opts.Output)
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
|
func ffmpegVideoCodec(codec string) string {
|
||||||
|
switch codec {
|
||||||
|
case "h264":
|
||||||
|
return "libx264"
|
||||||
|
case "h265":
|
||||||
|
return "libx265"
|
||||||
|
case "mpeg4":
|
||||||
|
return "mpeg4"
|
||||||
|
default:
|
||||||
|
return "libx264"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ffmpegAudioCodec(codec string) string {
|
||||||
|
switch codec {
|
||||||
|
case "aac":
|
||||||
|
return "aac"
|
||||||
|
case "mp3":
|
||||||
|
return "libmp3lame"
|
||||||
|
default:
|
||||||
|
return "aac"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OutputExt возвращает расширение файла для заданного формата контейнера.
|
||||||
|
func OutputExt(format string) string {
|
||||||
|
switch format {
|
||||||
|
case "mkv":
|
||||||
|
return ".mkv"
|
||||||
|
case "avi":
|
||||||
|
return ".avi"
|
||||||
|
default:
|
||||||
|
return ".mp4"
|
||||||
|
}
|
||||||
|
}
|
||||||
39
ops/build-release.sh
Executable file
39
ops/build-release.sh
Executable file
@@ -0,0 +1,39 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
|
||||||
|
RELEASE_DIR="${ROOT_DIR}/release"
|
||||||
|
CACHE_DIR="${RELEASE_DIR}/.gocache"
|
||||||
|
VERSION=${VERSION:-$(git -C "${ROOT_DIR}" describe --tags --always 2>/dev/null || echo dev)}
|
||||||
|
|
||||||
|
build_target() {
|
||||||
|
goos="$1"
|
||||||
|
goarch="$2"
|
||||||
|
output_name="$3"
|
||||||
|
|
||||||
|
echo "building ${goos}/${goarch} -> ${output_name}"
|
||||||
|
env \
|
||||||
|
GOCACHE="${CACHE_DIR}" \
|
||||||
|
GOOS="${goos}" \
|
||||||
|
GOARCH="${goarch}" \
|
||||||
|
go build \
|
||||||
|
-ldflags "-X main.Version=${VERSION}" \
|
||||||
|
-o "${RELEASE_DIR}/${output_name}" \
|
||||||
|
./cmd/jukebox
|
||||||
|
}
|
||||||
|
|
||||||
|
command -v go >/dev/null 2>&1 || {
|
||||||
|
echo "error: go not found in PATH" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
mkdir -p "${RELEASE_DIR}" "${CACHE_DIR}"
|
||||||
|
|
||||||
|
build_target darwin arm64 jukebox-darwin-arm64
|
||||||
|
build_target windows 386 jukebox-windows-386.exe
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "artifacts:"
|
||||||
|
echo " ${RELEASE_DIR}/jukebox-darwin-arm64"
|
||||||
|
echo " ${RELEASE_DIR}/jukebox-windows-386.exe"
|
||||||
@@ -225,6 +225,17 @@ a:hover { text-decoration: underline; }
|
|||||||
|
|
||||||
.form-group { display: flex; flex-direction: column; gap: 5px; }
|
.form-group { display: flex; flex-direction: column; gap: 5px; }
|
||||||
|
|
||||||
|
.path-input-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.path-input-row .form-input {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
|
||||||
.form-label {
|
.form-label {
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
font-weight: 700;
|
font-weight: 700;
|
||||||
@@ -268,10 +279,25 @@ a:hover { text-decoration: underline; }
|
|||||||
/* Checkbox list */
|
/* Checkbox list */
|
||||||
.source-list { display: flex; flex-direction: column; gap: 0; }
|
.source-list { display: flex; flex-direction: column; gap: 0; }
|
||||||
.source-tree {
|
.source-tree {
|
||||||
padding: 8px 0;
|
padding: 12px;
|
||||||
|
background: linear-gradient(180deg, rgba(33, 133, 208, 0.03), rgba(34, 36, 38, 0.015));
|
||||||
}
|
}
|
||||||
.source-tree-empty {
|
.source-tree-empty {
|
||||||
padding: 12px 16px;
|
padding: 20px 16px;
|
||||||
|
border: 1px dashed var(--border);
|
||||||
|
border-radius: calc(var(--radius) + 2px);
|
||||||
|
background: rgba(255, 255, 255, 0.75);
|
||||||
|
}
|
||||||
|
.source-root-card {
|
||||||
|
margin-bottom: 12px;
|
||||||
|
overflow: hidden;
|
||||||
|
border: 1px solid rgba(33, 133, 208, 0.18);
|
||||||
|
border-radius: calc(var(--radius) + 2px);
|
||||||
|
background: rgba(255, 255, 255, 0.92);
|
||||||
|
box-shadow: 0 8px 24px rgba(27, 28, 29, 0.04);
|
||||||
|
}
|
||||||
|
.source-root-card:last-child {
|
||||||
|
margin-bottom: 0;
|
||||||
}
|
}
|
||||||
.source-node {
|
.source-node {
|
||||||
border-bottom: 1px solid var(--border-lite);
|
border-bottom: 1px solid var(--border-lite);
|
||||||
@@ -284,10 +310,22 @@ a:hover { text-decoration: underline; }
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 8px;
|
gap: 8px;
|
||||||
padding: 8px 16px;
|
padding: 8px 16px;
|
||||||
|
transition: background 0.12s ease;
|
||||||
}
|
}
|
||||||
.source-row:hover {
|
.source-row:hover {
|
||||||
background: rgba(33, 133, 208, 0.04);
|
background: rgba(33, 133, 208, 0.04);
|
||||||
}
|
}
|
||||||
|
.source-root-row {
|
||||||
|
padding-top: 12px;
|
||||||
|
padding-bottom: 12px;
|
||||||
|
border-bottom: 1px solid rgba(33, 133, 208, 0.12);
|
||||||
|
background:
|
||||||
|
linear-gradient(90deg, rgba(33, 133, 208, 0.08), rgba(33, 133, 208, 0.015) 42%, rgba(255, 255, 255, 0.96) 100%);
|
||||||
|
}
|
||||||
|
.source-root-row:hover {
|
||||||
|
background:
|
||||||
|
linear-gradient(90deg, rgba(33, 133, 208, 0.12), rgba(33, 133, 208, 0.03) 42%, rgba(255, 255, 255, 1) 100%);
|
||||||
|
}
|
||||||
.source-toggle {
|
.source-toggle {
|
||||||
width: 24px;
|
width: 24px;
|
||||||
height: 24px;
|
height: 24px;
|
||||||
@@ -318,12 +356,40 @@ a:hover { text-decoration: underline; }
|
|||||||
min-width: 0;
|
min-width: 0;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
.source-root-title {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
.source-root-badge {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
padding: 2px 7px;
|
||||||
|
border-radius: 999px;
|
||||||
|
background: rgba(33, 133, 208, 0.1);
|
||||||
|
color: var(--accent-dark);
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 700;
|
||||||
|
letter-spacing: 0.02em;
|
||||||
|
text-transform: uppercase;
|
||||||
|
}
|
||||||
.source-item-hint {
|
.source-item-hint {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: var(--muted);
|
color: var(--muted);
|
||||||
}
|
}
|
||||||
.source-children {
|
.source-children {
|
||||||
padding-left: 20px;
|
position: relative;
|
||||||
|
padding: 6px 0 8px 0;
|
||||||
|
}
|
||||||
|
.source-children::before {
|
||||||
|
content: "";
|
||||||
|
position: absolute;
|
||||||
|
left: 26px;
|
||||||
|
top: 0;
|
||||||
|
bottom: 8px;
|
||||||
|
width: 1px;
|
||||||
|
background: linear-gradient(180deg, rgba(33, 133, 208, 0.2), rgba(33, 133, 208, 0.03));
|
||||||
}
|
}
|
||||||
.source-loading {
|
.source-loading {
|
||||||
padding: 6px 16px 10px 48px;
|
padding: 6px 16px 10px 48px;
|
||||||
@@ -418,4 +484,11 @@ a:hover { text-decoration: underline; }
|
|||||||
.disk-grid { grid-template-columns: 1fr; }
|
.disk-grid { grid-template-columns: 1fr; }
|
||||||
.kv-table th { width: 130px; }
|
.kv-table th { width: 130px; }
|
||||||
.btn-row { flex-wrap: wrap; }
|
.btn-row { flex-wrap: wrap; }
|
||||||
|
.path-input-row { flex-wrap: wrap; }
|
||||||
|
.source-root-row {
|
||||||
|
align-items: flex-start;
|
||||||
|
}
|
||||||
|
.source-root-title {
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
{{define "content"}}
|
{{define "content"}}
|
||||||
|
|
||||||
<section class="panel">
|
<section class="panel">
|
||||||
<h2>Disks</h2>
|
<h2>Mounted Disk</h2>
|
||||||
<div class="panel-body">
|
<div class="panel-body">
|
||||||
<div id="diskSummary" class="text-muted">Loading disks...</div>
|
<div class="path-input-row">
|
||||||
|
<input class="form-input" type="text" id="mountPath" placeholder="/Volumes/JUKEBOX or E:\\">
|
||||||
|
<button type="button" class="button-secondary" onclick="refreshSelectedDisk()">Refresh</button>
|
||||||
|
</div>
|
||||||
|
<div class="form-hint">Choose the directory where the removable disk is mounted. The app works with one selected disk at a time in standalone mode.</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<div class="disk-grid" id="diskGrid"></div>
|
<div id="diskState"></div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
let disks = [];
|
const selectedDisk = { info: null };
|
||||||
const taskState = new Map();
|
const taskState = new Map();
|
||||||
const taskPollers = new Map();
|
const taskPollers = new Map();
|
||||||
|
|
||||||
@@ -24,16 +28,12 @@ function escapeHTML(value) {
|
|||||||
}[char]));
|
}[char]));
|
||||||
}
|
}
|
||||||
|
|
||||||
function diskKey(disk) {
|
|
||||||
return disk.disk_id || disk.mount_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
function badgeClass(state) {
|
function badgeClass(state) {
|
||||||
return ({ absent: 'badge-unknown', foreign: 'badge-warn', known: 'badge-ok' })[state] || 'badge-unknown';
|
return ({ absent: 'badge-unknown', foreign: 'badge-warn', known: 'badge-ok' })[state] || 'badge-unknown';
|
||||||
}
|
}
|
||||||
|
|
||||||
function badgeLabel(state) {
|
function badgeLabel(state) {
|
||||||
return ({ absent: 'Not connected', foreign: 'Uninitialized disk', known: 'Ready' })[state] || '—';
|
return ({ absent: 'Directory unavailable', foreign: 'Uninitialized disk', known: 'Ready' })[state] || '—';
|
||||||
}
|
}
|
||||||
|
|
||||||
function fmtSpeed(bps) {
|
function fmtSpeed(bps) {
|
||||||
@@ -69,77 +69,219 @@ function taskMeta(task) {
|
|||||||
return [fmtSpeed(task.speed_bps), task.eta_sec ? 'ETA: ' + fmtETA(task.eta_sec) : ''].filter(Boolean).join(' · ');
|
return [fmtSpeed(task.speed_bps), task.eta_sec ? 'ETA: ' + fmtETA(task.eta_sec) : ''].filter(Boolean).join(' · ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderDisks() {
|
function renderDisk() {
|
||||||
const grid = document.getElementById('diskGrid');
|
const root = document.getElementById('diskState');
|
||||||
const summary = document.getElementById('diskSummary');
|
const disk = selectedDisk.info;
|
||||||
|
if (!disk) {
|
||||||
if (!disks.length) {
|
root.innerHTML = `
|
||||||
summary.textContent = 'No disks found.';
|
<section class="panel">
|
||||||
grid.innerHTML = '';
|
<div class="panel-body text-muted">Choose a mounted disk directory to inspect it.</div>
|
||||||
|
</section>
|
||||||
|
`;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const knownCount = disks.filter((disk) => disk.state === 'known').length;
|
const activeTask = disk.active_task_id ? taskState.get(disk.active_task_id) : null;
|
||||||
summary.textContent = `Disks found: ${disks.length}. Ready to copy: ${knownCount}.`;
|
const progress = activeTask ? activeTask.progress : 0;
|
||||||
|
const message = activeTask ? (activeTask.message || 'Preparing...') : '';
|
||||||
|
const meta = activeTask ? taskMeta(activeTask) : '';
|
||||||
|
const isKnown = disk.state === 'known';
|
||||||
|
const isForeign = disk.state === 'foreign';
|
||||||
|
const hasCapacity = disk.state !== 'absent';
|
||||||
|
|
||||||
grid.innerHTML = disks.map((disk) => {
|
root.innerHTML = `
|
||||||
const activeTask = disk.active_task_id ? taskState.get(disk.active_task_id) : null;
|
<section class="panel disk-card">
|
||||||
const progress = activeTask ? activeTask.progress : 0;
|
<h2>${escapeHTML(disk.mount_path)}</h2>
|
||||||
const message = activeTask ? (activeTask.message || 'Preparing...') : '';
|
<table class="kv-table">
|
||||||
const meta = activeTask ? taskMeta(activeTask) : '';
|
<tbody>
|
||||||
const isKnown = disk.state === 'known';
|
<tr>
|
||||||
const isForeign = disk.state === 'foreign';
|
<th>Status</th>
|
||||||
const hasCapacity = disk.state !== 'absent';
|
<td><span class="badge ${badgeClass(disk.state)}">${badgeLabel(disk.state)}</span></td>
|
||||||
|
</tr>
|
||||||
return `
|
<tr>
|
||||||
<section class="panel disk-card">
|
<th>Disk ID</th>
|
||||||
<h2>${escapeHTML(disk.mount_path)}</h2>
|
<td>${disk.disk_id ? `<span class="mono">${escapeHTML(disk.disk_id)}</span>` : '<span class="text-muted">not initialized yet</span>'}</td>
|
||||||
<table class="kv-table">
|
</tr>
|
||||||
<tbody>
|
<tr>
|
||||||
<tr>
|
<th>Total capacity</th>
|
||||||
<th>Status</th>
|
<td>${hasCapacity ? fmtBytes(disk.total_bytes) : '—'}</td>
|
||||||
<td><span class="badge ${badgeClass(disk.state)}">${badgeLabel(disk.state)}</span></td>
|
</tr>
|
||||||
</tr>
|
<tr>
|
||||||
<tr>
|
<th>Free space</th>
|
||||||
<th>Disk ID</th>
|
<td>${hasCapacity ? fmtBytes(disk.free_bytes) : '—'}</td>
|
||||||
<td>${disk.disk_id ? `<span class="mono">${escapeHTML(disk.disk_id)}</span>` : '<span class="text-muted">not initialized yet</span>'}</td>
|
</tr>
|
||||||
</tr>
|
<tr>
|
||||||
<tr>
|
<th>Last copied</th>
|
||||||
<th>Total capacity</th>
|
<td>${fmtDateTime(disk.last_copied_at)}</td>
|
||||||
<td>${hasCapacity ? fmtBytes(disk.total_bytes) : '—'}</td>
|
</tr>
|
||||||
</tr>
|
</tbody>
|
||||||
<tr>
|
</table>
|
||||||
<th>Free space</th>
|
${activeTask ? `
|
||||||
<td>${hasCapacity ? fmtBytes(disk.free_bytes) : '—'}</td>
|
<div class="panel-body progress-wrap">
|
||||||
</tr>
|
<div class="progress-bar-bg">
|
||||||
<tr>
|
<div class="progress-bar-fill" style="width:${progress}%"></div>
|
||||||
<th>Last copied</th>
|
|
||||||
<td>${fmtDateTime(disk.last_copied_at)}</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
${activeTask ? `
|
|
||||||
<div class="panel-body progress-wrap">
|
|
||||||
<div class="progress-bar-bg">
|
|
||||||
<div class="progress-bar-fill" style="width:${progress}%"></div>
|
|
||||||
</div>
|
|
||||||
<div class="progress-label">${escapeHTML(message)}</div>
|
|
||||||
<div class="progress-label">${escapeHTML(meta)}</div>
|
|
||||||
</div>
|
</div>
|
||||||
` : ''}
|
<div class="progress-label">${escapeHTML(message)}</div>
|
||||||
<div class="btn-row">
|
<div class="progress-label">${escapeHTML(meta)}</div>
|
||||||
${isKnown ? `
|
|
||||||
<button class="button-danger" data-action="start-copy" data-mode="replace" data-disk-id="${escapeHTML(disk.disk_id)}" ${activeTask ? 'disabled' : ''}>Replace media</button>
|
|
||||||
<button class="button-primary" data-action="start-copy" data-mode="add" data-disk-id="${escapeHTML(disk.disk_id)}" ${activeTask ? 'disabled' : ''}>Add media</button>
|
|
||||||
<button class="button-danger ${activeTask ? '' : 'hidden'}" data-action="cancel-copy" data-disk-id="${escapeHTML(disk.disk_id)}">Cancel</button>
|
|
||||||
` : ''}
|
|
||||||
${isForeign ? `
|
|
||||||
<button class="button-secondary" data-action="init-disk" data-mount-path="${escapeHTML(disk.mount_path)}">Initialize disk</button>
|
|
||||||
` : ''}
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
` : ''}
|
||||||
`;
|
<div class="btn-row">
|
||||||
}).join('');
|
${isKnown ? `
|
||||||
|
<button class="button-danger" data-action="start-copy" data-mode="replace" ${activeTask ? 'disabled' : ''}>Replace media</button>
|
||||||
|
<button class="button-primary" data-action="start-copy" data-mode="add" ${activeTask ? 'disabled' : ''}>Add media</button>
|
||||||
|
<button class="button-danger ${activeTask ? '' : 'hidden'}" data-action="cancel-copy">Cancel</button>
|
||||||
|
<button class="button-secondary" data-action="disk-settings" style="margin-left:auto">⚙ Settings</button>
|
||||||
|
` : ''}
|
||||||
|
${isForeign ? `
|
||||||
|
<button class="button-secondary" data-action="init-disk">Initialize disk</button>
|
||||||
|
` : ''}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
${isKnown ? renderProfile(disk) : ''}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderProfile(disk) {
|
||||||
|
const p = disk.profile || {};
|
||||||
|
const t = p.transcode || null;
|
||||||
|
const transcodeEnabled = !!t;
|
||||||
|
|
||||||
|
const sel = (name, value, options) => {
|
||||||
|
const opts = options.map(([v, label]) =>
|
||||||
|
`<option value="${v}" ${v === value ? 'selected' : ''}>${escapeHTML(label)}</option>`
|
||||||
|
).join('');
|
||||||
|
return `<select class="form-input" id="prof_${name}">${opts}</select>`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const transcodeSection = `
|
||||||
|
<div id="transcodeFields" style="${transcodeEnabled ? '' : 'display:none'}">
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Видеокодек</label>
|
||||||
|
${sel('video_codec', t?.video_codec || 'h264', [['h264','H.264 (AVC)'],['h265','H.265 (HEVC)'],['mpeg4','MPEG-4']])}
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Макс. разрешение</label>
|
||||||
|
${sel('max_resolution', t?.max_resolution || '720p', [['480p','480p'],['720p','720p (HD)'],['1080p','1080p (Full HD)']])}
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Макс. битрейт видео</label>
|
||||||
|
${sel('max_video_bitrate', t?.max_video_bitrate || '2000k', [['','Без лимита'],['1000k','1000 кбит/с'],['2000k','2000 кбит/с'],['4000k','4000 кбит/с'],['8000k','8000 кбит/с']])}
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Макс. FPS</label>
|
||||||
|
${sel('max_fps', String(t?.max_fps ?? 0), [['0','Без лимита'],['24','24'],['25','25'],['30','30']])}
|
||||||
|
</div>
|
||||||
|
<hr>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Аудиокодек</label>
|
||||||
|
${sel('audio_codec', t?.audio_codec || 'aac', [['aac','AAC'],['mp3','MP3']])}
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Макс. битрейт аудио</label>
|
||||||
|
${sel('max_audio_bitrate', t?.max_audio_bitrate || '192k', [['','Без лимита'],['128k','128 кбит/с'],['192k','192 кбит/с'],['320k','320 кбит/с']])}
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Каналы</label>
|
||||||
|
${sel('max_audio_channels', String(t?.max_audio_channels ?? 0), [['0','Копировать'],['2','Стерео (2.0)'],['6','5.1']])}
|
||||||
|
</div>
|
||||||
|
<hr>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Формат контейнера</label>
|
||||||
|
${sel('output_format', t?.output_format || 'mp4', [['mp4','MP4'],['mkv','MKV'],['avi','AVI']])}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
return `
|
||||||
|
<section class="panel" id="profilePanel" style="display:none">
|
||||||
|
<h2>Профиль диска</h2>
|
||||||
|
<div class="panel-body">
|
||||||
|
<h3>Параметры копирования</h3>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Папка назначения</label>
|
||||||
|
<input class="form-input" type="text" id="prof_dest_folder" value="${escapeHTML(p.dest_folder || 'media')}">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Режим перезаписи</label>
|
||||||
|
${sel('overwrite_mode', p.overwrite_mode || 'skip', [['skip','Пропускать существующие'],['delete','Заменять всё']])}
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Выбор файлов</label>
|
||||||
|
${sel('file_select_mode', p.file_select_mode || 'new', [['new','Только новые'],['all','Все подходящие']])}
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Порядок копирования</label>
|
||||||
|
${sel('shuffle_depth', String(p.shuffle_depth ?? -1), [['-1','По порядку (без перемешивания)'],['0','Случайный порядок файлов'],['1','Случайная папка 1-го уровня (жанр)'],['2','Случайная папка 2-го уровня (сериал)'],['3','Случайная папка 3-го уровня (сезон)']])}
|
||||||
|
<span class="form-hint">Уровень задаёт глубину вложения: все файлы выбранной папки копируются целиком.</span>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Резерв свободного места (ГБ)</label>
|
||||||
|
<input class="form-input" type="number" id="prof_reserve_free_gb" value="${p.reserve_free_gb ?? 2}" min="0" step="0.5">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label><input type="checkbox" id="prof_auto_copy" ${p.auto_copy ? 'checked' : ''}> Автокопирование при подключении</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3 style="margin-top:1.5em">Транскодирование видео</h3>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" id="prof_transcode_enabled" ${transcodeEnabled ? 'checked' : ''}
|
||||||
|
onchange="document.getElementById('transcodeFields').style.display=this.checked?'':'none'">
|
||||||
|
Ограничить видео под устройство
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
${transcodeSection}
|
||||||
|
|
||||||
|
<div class="btn-row" style="margin-top:1em">
|
||||||
|
<button class="button-primary" onclick="saveProfile('${escapeHTML(disk.mount_path)}')">Сохранить профиль</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveProfile(mountPath) {
|
||||||
|
const g = id => document.getElementById(id);
|
||||||
|
const transcodeEnabled = g('prof_transcode_enabled')?.checked;
|
||||||
|
|
||||||
|
const profile = {
|
||||||
|
dest_folder: g('prof_dest_folder')?.value.trim() || 'media',
|
||||||
|
overwrite_mode: g('prof_overwrite_mode')?.value || 'skip',
|
||||||
|
file_select_mode: g('prof_file_select_mode')?.value || 'new',
|
||||||
|
reserve_free_gb: parseFloat(g('prof_reserve_free_gb')?.value || '2') || 0,
|
||||||
|
auto_copy: g('prof_auto_copy')?.checked || false,
|
||||||
|
shuffle_depth: parseInt(g('prof_shuffle_depth')?.value ?? '-1', 10),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (transcodeEnabled) {
|
||||||
|
profile.transcode = {
|
||||||
|
video_codec: g('prof_video_codec')?.value || 'h264',
|
||||||
|
max_resolution: g('prof_max_resolution')?.value || '720p',
|
||||||
|
max_video_bitrate: g('prof_max_video_bitrate')?.value || '',
|
||||||
|
max_fps: parseInt(g('prof_max_fps')?.value || '0', 10),
|
||||||
|
audio_codec: g('prof_audio_codec')?.value || 'aac',
|
||||||
|
max_audio_bitrate: g('prof_max_audio_bitrate')?.value || '',
|
||||||
|
max_audio_channels: parseInt(g('prof_max_audio_channels')?.value || '0', 10),
|
||||||
|
output_format: g('prof_output_format')?.value || 'mp4',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/disks/profile?mount_path=' + encodeURIComponent(mountPath), {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(profile)
|
||||||
|
});
|
||||||
|
const payload = await response.json();
|
||||||
|
if (!response.ok) {
|
||||||
|
toast(payload.error || 'Ошибка сохранения профиля', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
toast('Профиль сохранён', 'ok');
|
||||||
|
refreshSelectedDisk();
|
||||||
|
} catch (error) {
|
||||||
|
toast('Ошибка сети', 'error');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopTaskPoll(taskID) {
|
function stopTaskPoll(taskID) {
|
||||||
@@ -154,28 +296,42 @@ function startTaskPoll(taskID) {
|
|||||||
pollTask(taskID);
|
pollTask(taskID);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function refreshDisks() {
|
async function refreshSelectedDisk() {
|
||||||
try {
|
const mountPath = document.getElementById('mountPath').value.trim();
|
||||||
const response = await fetch('/api/disks');
|
if (!mountPath) {
|
||||||
if (!response.ok) return;
|
selectedDisk.info = null;
|
||||||
const payload = await response.json();
|
renderDisk();
|
||||||
disks = payload.items || [];
|
return;
|
||||||
renderDisks();
|
}
|
||||||
|
|
||||||
const activeTasks = new Set();
|
localStorage.setItem('jukebox.selectedMountPath', mountPath);
|
||||||
for (const disk of disks) {
|
try {
|
||||||
if (disk.active_task_id) {
|
const response = await fetch('/api/disks/probe?mount_path=' + encodeURIComponent(mountPath));
|
||||||
activeTasks.add(disk.active_task_id);
|
const payload = await response.json();
|
||||||
startTaskPoll(disk.active_task_id);
|
if (!response.ok) {
|
||||||
}
|
toast(payload.error || 'Failed to inspect directory', 'error');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
for (const taskID of Array.from(taskPollers.keys())) {
|
selectedDisk.info = payload;
|
||||||
if (!activeTasks.has(taskID)) {
|
renderDisk();
|
||||||
|
|
||||||
|
if (payload.active_task_id) {
|
||||||
|
for (const taskID of Array.from(taskPollers.keys())) {
|
||||||
|
if (taskID !== payload.active_task_id) {
|
||||||
|
stopTaskPoll(taskID);
|
||||||
|
taskState.delete(taskID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
startTaskPoll(payload.active_task_id);
|
||||||
|
} else {
|
||||||
|
for (const taskID of Array.from(taskPollers.keys())) {
|
||||||
stopTaskPoll(taskID);
|
stopTaskPoll(taskID);
|
||||||
taskState.delete(taskID);
|
taskState.delete(taskID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {}
|
} catch (error) {
|
||||||
|
toast('Network error', 'error');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function pollTask(taskID) {
|
async function pollTask(taskID) {
|
||||||
@@ -184,7 +340,7 @@ async function pollTask(taskID) {
|
|||||||
if (!response.ok) return;
|
if (!response.ok) return;
|
||||||
const task = await response.json();
|
const task = await response.json();
|
||||||
taskState.set(taskID, task);
|
taskState.set(taskID, task);
|
||||||
renderDisks();
|
renderDisk();
|
||||||
|
|
||||||
if (['success', 'failed', 'canceled'].includes(task.status)) {
|
if (['success', 'failed', 'canceled'].includes(task.status)) {
|
||||||
stopTaskPoll(taskID);
|
stopTaskPoll(taskID);
|
||||||
@@ -192,17 +348,19 @@ async function pollTask(taskID) {
|
|||||||
if (task.status === 'success') toast(task.message || 'Done', 'ok');
|
if (task.status === 'success') toast(task.message || 'Done', 'ok');
|
||||||
if (task.status === 'failed') toast('Error: ' + task.error, 'error');
|
if (task.status === 'failed') toast('Error: ' + task.error, 'error');
|
||||||
if (task.status === 'canceled') toast('Copy canceled', 'error');
|
if (task.status === 'canceled') toast('Copy canceled', 'error');
|
||||||
refreshDisks();
|
refreshSelectedDisk();
|
||||||
}
|
}
|
||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startCopy(diskID, mode) {
|
async function startCopy(mode) {
|
||||||
|
const mountPath = document.getElementById('mountPath').value.trim();
|
||||||
|
if (!mountPath) return;
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/disks/' + encodeURIComponent(diskID) + '/copy/start', {
|
const response = await fetch('/api/disks/copy/start', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ mode })
|
body: JSON.stringify({ mount_path: mountPath, mode })
|
||||||
});
|
});
|
||||||
const payload = await response.json();
|
const payload = await response.json();
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -210,22 +368,25 @@ async function startCopy(diskID, mode) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
startTaskPoll(payload.task_id);
|
startTaskPoll(payload.task_id);
|
||||||
refreshDisks();
|
refreshSelectedDisk();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast('Network error', 'error');
|
toast('Network error', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cancelCopy(diskID) {
|
async function cancelCopy() {
|
||||||
|
if (!selectedDisk.info || !selectedDisk.info.disk_id) return;
|
||||||
try {
|
try {
|
||||||
await fetch('/api/disks/' + encodeURIComponent(diskID) + '/copy/cancel', { method: 'POST' });
|
await fetch('/api/disks/' + encodeURIComponent(selectedDisk.info.disk_id) + '/copy/cancel', { method: 'POST' });
|
||||||
toast('Canceling...', 'ok');
|
toast('Canceling...', 'ok');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast('Network error', 'error');
|
toast('Network error', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function initDisk(mountPath) {
|
async function initDisk() {
|
||||||
|
const mountPath = document.getElementById('mountPath').value.trim();
|
||||||
|
if (!mountPath) return;
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/disks/init', {
|
const response = await fetch('/api/disks/init', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -238,23 +399,35 @@ async function initDisk(mountPath) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
toast('Disk initialized', 'ok');
|
toast('Disk initialized', 'ok');
|
||||||
refreshDisks();
|
refreshSelectedDisk();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast('Network error', 'error');
|
toast('Network error', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('diskGrid').addEventListener('click', (event) => {
|
document.getElementById('diskState').addEventListener('click', (event) => {
|
||||||
const button = event.target.closest('button[data-action]');
|
const button = event.target.closest('button[data-action]');
|
||||||
if (!button) return;
|
if (!button) return;
|
||||||
|
|
||||||
const action = button.dataset.action;
|
const action = button.dataset.action;
|
||||||
if (action === 'start-copy') startCopy(button.dataset.diskId, button.dataset.mode || 'add');
|
if (action === 'start-copy') startCopy(button.dataset.mode || 'add');
|
||||||
if (action === 'cancel-copy') cancelCopy(button.dataset.diskId);
|
if (action === 'cancel-copy') cancelCopy();
|
||||||
if (action === 'init-disk') initDisk(button.dataset.mountPath);
|
if (action === 'init-disk') initDisk();
|
||||||
|
if (action === 'disk-settings') {
|
||||||
|
const panel = document.getElementById('profilePanel');
|
||||||
|
if (!panel) return;
|
||||||
|
const open = panel.style.display !== 'none';
|
||||||
|
panel.style.display = open ? 'none' : '';
|
||||||
|
button.textContent = open ? '⚙ Settings' : '⚙ Settings ✕';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
refreshDisks();
|
const savedMountPath = localStorage.getItem('jukebox.selectedMountPath');
|
||||||
setInterval(refreshDisks, 5000);
|
if (savedMountPath) {
|
||||||
|
document.getElementById('mountPath').value = savedMountPath;
|
||||||
|
refreshSelectedDisk();
|
||||||
|
} else {
|
||||||
|
renderDisk();
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|||||||
@@ -35,6 +35,46 @@
|
|||||||
<span class="form-hint">The new-only mode skips files already copied to this disk, even if they were later removed.</span>
|
<span class="form-hint">The new-only mode skips files already copied to this disk, even if they were later removed.</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group" id="mediaTypesGroup">
|
||||||
|
<div style="display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap">
|
||||||
|
<label class="form-label" style="margin:0">Allowed file types</label>
|
||||||
|
<button type="button" class="button-secondary button-sm" id="editAllowedFilesButton" onclick="toggleAllowedFilesEditor()">Edit list</button>
|
||||||
|
</div>
|
||||||
|
<div style="display:grid;gap:8px">
|
||||||
|
<label style="display:flex;align-items:flex-start;gap:8px;cursor:pointer">
|
||||||
|
<input type="checkbox" id="mediaTypeAudio" style="width:15px;height:15px;accent-color:var(--accent)">
|
||||||
|
<span>
|
||||||
|
<strong>Audio</strong>
|
||||||
|
<span class="form-hint" id="mediaTypeAudioHint" style="display:block"></span>
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
<label style="display:flex;align-items:flex-start;gap:8px;cursor:pointer">
|
||||||
|
<input type="checkbox" id="mediaTypeVideo" style="width:15px;height:15px;accent-color:var(--accent)">
|
||||||
|
<span>
|
||||||
|
<strong>Video</strong>
|
||||||
|
<span class="form-hint" id="mediaTypeVideoHint" style="display:block"></span>
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
<label style="display:flex;align-items:flex-start;gap:8px;cursor:pointer">
|
||||||
|
<input type="checkbox" id="mediaTypePhoto" style="width:15px;height:15px;accent-color:var(--accent)">
|
||||||
|
<span>
|
||||||
|
<strong>Photo</strong>
|
||||||
|
<span class="form-hint" id="mediaTypePhotoHint" style="display:block"></span>
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<span class="form-hint">Built into the app by default: audio, video, and photo. New installations start with only audio and video enabled.</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group" id="extensionsGroup" style="display:none">
|
||||||
|
<div style="display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap">
|
||||||
|
<label class="form-label" for="allowedExtensions" style="margin:0">Allowed extensions</label>
|
||||||
|
<button type="button" class="button-secondary button-sm" onclick="toggleAllowedFilesEditor()">Use media types</button>
|
||||||
|
</div>
|
||||||
|
<textarea class="form-input" id="allowedExtensions" rows="5" placeholder=".mp3, .flac, .mp4"></textarea>
|
||||||
|
<span class="form-hint">One extension per line or separated by commas. You can write <code>mp3</code> or <code>.mp3</code>.</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="form-label" for="destFolder">Destination folder on disk</label>
|
<label class="form-label" for="destFolder">Destination folder on disk</label>
|
||||||
<input class="form-input" type="text" id="destFolder" placeholder="media" style="width:200px">
|
<input class="form-input" type="text" id="destFolder" placeholder="media" style="width:200px">
|
||||||
@@ -77,7 +117,13 @@
|
|||||||
const sourceTree = new Map();
|
const sourceTree = new Map();
|
||||||
const expandedNodes = new Set();
|
const expandedNodes = new Set();
|
||||||
const loadingNodes = new Set();
|
const loadingNodes = new Set();
|
||||||
|
const builtInMediaTypes = {
|
||||||
|
audio: ['.aac', '.aif', '.aiff', '.alac', '.ape', '.flac', '.m4a', '.mp2', '.mp3', '.ogg', '.opus', '.wav', '.wma'],
|
||||||
|
video: ['.3gp', '.avi', '.m2ts', '.m4v', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.mts', '.ts', '.webm', '.wmv'],
|
||||||
|
photo: ['.bmp', '.gif', '.heic', '.heif', '.jpeg', '.jpg', '.png', '.tif', '.tiff', '.webp'],
|
||||||
|
};
|
||||||
let sourceConfig = {};
|
let sourceConfig = {};
|
||||||
|
let allowedFilesMode = 'media_types';
|
||||||
|
|
||||||
function escapeHTML(value) {
|
function escapeHTML(value) {
|
||||||
return String(value || '').replace(/[&<>"']/g, (char) => ({
|
return String(value || '').replace(/[&<>"']/g, (char) => ({
|
||||||
@@ -214,6 +260,56 @@ async function reloadSourceTree() {
|
|||||||
await loadSourceChildren('');
|
await loadSourceChildren('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function defaultAllowedExtensions() {
|
||||||
|
return [...builtInMediaTypes.audio, ...builtInMediaTypes.video];
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseExtensionsInput(value) {
|
||||||
|
const items = String(value || '')
|
||||||
|
.split(/[\n,]+/)
|
||||||
|
.map((item) => item.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
const result = [];
|
||||||
|
const seen = new Set();
|
||||||
|
items.forEach((item) => {
|
||||||
|
let value = item.toLowerCase().replace(/^\*/, '');
|
||||||
|
if (!value.startsWith('.')) value = '.' + value;
|
||||||
|
if (!/^\.[a-z0-9]+$/.test(value)) return;
|
||||||
|
if (seen.has(value)) return;
|
||||||
|
seen.add(value);
|
||||||
|
result.push(value);
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatExtensionsInput(items) {
|
||||||
|
return (items || []).join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectedMediaTypes() {
|
||||||
|
return ['audio', 'video', 'photo'].filter((name) => {
|
||||||
|
const id = 'mediaType' + name.charAt(0).toUpperCase() + name.slice(1);
|
||||||
|
return document.getElementById(id).checked;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateAllowedFilesModeUI() {
|
||||||
|
document.getElementById('mediaTypesGroup').style.display = allowedFilesMode === 'media_types' ? '' : 'none';
|
||||||
|
document.getElementById('extensionsGroup').style.display = allowedFilesMode === 'extensions' ? '' : 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleAllowedFilesEditor() {
|
||||||
|
allowedFilesMode = allowedFilesMode === 'extensions' ? 'media_types' : 'extensions';
|
||||||
|
updateAllowedFilesModeUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderMediaTypeHints() {
|
||||||
|
document.getElementById('mediaTypeAudioHint').textContent = builtInMediaTypes.audio.join(', ');
|
||||||
|
document.getElementById('mediaTypeVideoHint').textContent = builtInMediaTypes.video.join(', ');
|
||||||
|
document.getElementById('mediaTypePhotoHint').textContent = builtInMediaTypes.photo.join(', ');
|
||||||
|
}
|
||||||
|
|
||||||
async function loadSettings() {
|
async function loadSettings() {
|
||||||
try {
|
try {
|
||||||
const r = await fetch('/api/config');
|
const r = await fetch('/api/config');
|
||||||
@@ -224,6 +320,12 @@ async function loadSettings() {
|
|||||||
document.getElementById('fileSelectMode').value = cfg.file_select_mode || 'new';
|
document.getElementById('fileSelectMode').value = cfg.file_select_mode || 'new';
|
||||||
document.getElementById('overwriteMode').value = cfg.overwrite_mode || 'skip';
|
document.getElementById('overwriteMode').value = cfg.overwrite_mode || 'skip';
|
||||||
document.getElementById('autoCopy').checked = !!cfg.auto_copy;
|
document.getElementById('autoCopy').checked = !!cfg.auto_copy;
|
||||||
|
allowedFilesMode = cfg.allowed_files_mode || 'media_types';
|
||||||
|
document.getElementById('mediaTypeAudio').checked = (cfg.enabled_media_types || ['audio', 'video']).includes('audio');
|
||||||
|
document.getElementById('mediaTypeVideo').checked = (cfg.enabled_media_types || ['audio', 'video']).includes('video');
|
||||||
|
document.getElementById('mediaTypePhoto').checked = (cfg.enabled_media_types || []).includes('photo');
|
||||||
|
document.getElementById('allowedExtensions').value = formatExtensionsInput((cfg.allowed_extensions || []).length ? cfg.allowed_extensions : defaultAllowedExtensions());
|
||||||
|
updateAllowedFilesModeUI();
|
||||||
|
|
||||||
sourceConfig = {};
|
sourceConfig = {};
|
||||||
(cfg.sources || []).forEach((source) => {
|
(cfg.sources || []).forEach((source) => {
|
||||||
@@ -237,12 +339,15 @@ async function saveSettings(event) {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
const body = {
|
const body = {
|
||||||
reserve_free_gb: parseFloat(document.getElementById('reserveGB').value) || 2,
|
reserve_free_gb: parseFloat(document.getElementById('reserveGB').value) || 2,
|
||||||
dest_folder: document.getElementById('destFolder').value.trim() || 'media',
|
dest_folder: document.getElementById('destFolder').value.trim() || 'media',
|
||||||
file_select_mode: document.getElementById('fileSelectMode').value,
|
file_select_mode: document.getElementById('fileSelectMode').value,
|
||||||
overwrite_mode: document.getElementById('overwriteMode').value,
|
allowed_files_mode: allowedFilesMode,
|
||||||
auto_copy: document.getElementById('autoCopy').checked,
|
enabled_media_types: selectedMediaTypes(),
|
||||||
sources: collectSourcesForSave(),
|
allowed_extensions: parseExtensionsInput(document.getElementById('allowedExtensions').value),
|
||||||
|
overwrite_mode: document.getElementById('overwriteMode').value,
|
||||||
|
auto_copy: document.getElementById('autoCopy').checked,
|
||||||
|
sources: collectSourcesForSave(),
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -282,6 +387,7 @@ document.getElementById('sourceTree').addEventListener('change', (event) => {
|
|||||||
toggleSource(checkbox.dataset.path, checkbox.checked);
|
toggleSource(checkbox.dataset.path, checkbox.checked);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
renderMediaTypeHints();
|
||||||
loadSettings();
|
loadSettings();
|
||||||
loadSourceChildren('');
|
loadSourceChildren('');
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user