125 lines
4.5 KiB
Go
125 lines
4.5 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"git.lastassault.de/speatzle/morffix/constants"
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
type LibrariesData struct {
|
|
Libraries []Library
|
|
FfmpegCommands []FfmpegCommand
|
|
}
|
|
|
|
type Library struct {
|
|
ID string `db:"id"`
|
|
HealthCommandID *int `db:"health_command_id"`
|
|
TranscodeCommandID *int `db:"transcode_command_id"`
|
|
Name string `db:"name"`
|
|
Path string `db:"path"`
|
|
Enable bool `db:"enable"`
|
|
TranscodeReplace bool `db:"transcode_replace"`
|
|
TranscodePath string `db:"transcode_path"`
|
|
ScanNewQueueHealth bool `db:"scan_new_queue_health"`
|
|
ScanChangedQueueHealth bool `db:"scan_changed_queue_health"`
|
|
ScanNewQueueTranscode bool `db:"scan_new_queue_transcode"`
|
|
ScanChangedQueueTranscode bool `db:"scan_changed_queue_transcode"`
|
|
HealthOkQueueTranscode bool `db:"health_ok_queue_transcode"`
|
|
}
|
|
|
|
func handleLibraries(w http.ResponseWriter, r *http.Request) {
|
|
data := LibrariesData{}
|
|
|
|
if r.Method == "POST" {
|
|
err := createLibrary(r)
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Create Library", "err", err)
|
|
http.Error(w, "Error Create Library: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
rows, err := db.Query(r.Context(), "SELECT * FROM libraries")
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Query Libraries", "err", err)
|
|
http.Error(w, "Error Query Libraries: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
libraries, err := pgx.CollectRows[Library](rows, pgx.RowToStructByName[Library])
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Collect Rows", "err", err)
|
|
http.Error(w, "Error Query Libraries: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data.Libraries = libraries
|
|
|
|
rows, err = db.Query(r.Context(), "SELECT id, name, data FROM ffmpeg_commands")
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Query Ffmpeg Commands", "err", err)
|
|
http.Error(w, "Error Ffmpeg Commands: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
ffmpegCommands, err := pgx.CollectRows[FfmpegCommand](rows, pgx.RowToStructByName[FfmpegCommand])
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Collect Ffmpeg Commands", "err", err)
|
|
http.Error(w, "Error Collect Ffmpeg Commands: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data.FfmpegCommands = ffmpegCommands
|
|
|
|
buf := bytes.Buffer{}
|
|
err = templates.ExecuteTemplate(&buf, constants.LIBRARIES_TEMPLATE_NAME, data)
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Executing Libraries Template", "err", err)
|
|
http.Error(w, "Error Executing Template: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
_, err = w.Write(buf.Bytes())
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Writing http Response", "err", err)
|
|
}
|
|
}
|
|
|
|
func createLibrary(r *http.Request) error {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return fmt.Errorf("Parseing Form: %w", err)
|
|
}
|
|
name := r.FormValue("name")
|
|
path := r.FormValue("path")
|
|
enable := r.FormValue("enable") == "on"
|
|
transcode_replace := r.FormValue("transcode_replace") == "on"
|
|
transcode_path := r.FormValue("transcode_path")
|
|
scan_new_queue_health := r.FormValue("scan_new_queue_health") == "on"
|
|
scan_changed_queue_health := r.FormValue("scan_changed_queue_health") == "on"
|
|
scan_new_queue_transcode := r.FormValue("scan_new_queue_transcode") == "on"
|
|
scan_changed_queue_transcode := r.FormValue("scan_changed_queue_transcode") == "on"
|
|
health_ok_queue_transcode := r.FormValue("health_ok_queue_transcode") == "on"
|
|
|
|
var health_command_id *int = nil
|
|
var transcode_command_id *int = nil
|
|
|
|
h, err := strconv.Atoi(r.FormValue("health_command_id"))
|
|
if err == nil {
|
|
health_command_id = &h
|
|
}
|
|
t, err := strconv.Atoi(r.FormValue("transcode_command_id"))
|
|
if err == nil {
|
|
transcode_command_id = &t
|
|
}
|
|
|
|
slog.Info("Got Library Create", "name", name, "path", path, "enable", enable)
|
|
|
|
_, err = db.Exec(r.Context(), "INSERT INTO Libraries (name, path, enable, scan_new_queue_health, scan_changed_queue_health, scan_new_queue_transcode, scan_changed_queue_transcode, health_ok_queue_transcode, transcode_replace, transcode_path, health_command_id, transcode_command_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)", name, path, enable, scan_new_queue_health, scan_changed_queue_health, scan_new_queue_transcode, scan_changed_queue_transcode, health_ok_queue_transcode, transcode_replace, transcode_path, health_command_id, transcode_command_id)
|
|
if err != nil {
|
|
return fmt.Errorf("Inserting Library: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|