package server import ( "bytes" "fmt" "log/slog" "net/http" "time" "git.lastassault.de/speatzle/morffix/constants" "github.com/jackc/pgx/v5" ) type LibraryData struct { Library Library Files []FileDisplay } type File struct { ID int `db:"id"` Path string `db:"path"` Size int64 `db:"size"` OldSize *int64 `db:"old_size"` Status constants.FileStatus `db:"status"` Health constants.FileHealth `db:"health"` Transcode constants.FileTranscode `db:"transcode"` MD5 []byte `db:"md5"` OldMD5 *[]byte `db:"old_md5"` UpdatedAt time.Time `db:"updated_at"` } type FileDisplay struct { ID int Path string Size int64 Status string Health string Transcode string MD5 string UpdatedAt string `db:"updated_at"` } func handleLibrary(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") if id == "" { handleLibraries(w, r) return } data := LibraryData{} var name string var path string var enabled bool err := db.QueryRow(r.Context(), "SELECT name, path, enable FROM libraries WHERE id = $1", id).Scan(&name, &path, &enabled) if err != nil { slog.ErrorContext(r.Context(), "Get Library", "err", err) http.Error(w, "Error Get Library: "+err.Error(), http.StatusInternalServerError) return } data.Library = Library{ ID: id, Name: name, Path: path, Enable: enabled, } rows, err := db.Query(r.Context(), "SELECT id, path, size, old_size, status, health, transcode, md5, old_md5, updated_at FROM files where library_id = $1", id) if err != nil { slog.ErrorContext(r.Context(), "Query Files", "err", err) http.Error(w, "Error Query Files: "+err.Error(), http.StatusInternalServerError) return } files, err := pgx.CollectRows[File](rows, pgx.RowToStructByName[File]) if err != nil { slog.ErrorContext(r.Context(), "Collect Rows", "err", err) http.Error(w, "Error Query Files: "+err.Error(), http.StatusInternalServerError) return } for i := range files { data.Files = append(data.Files, FileDisplay{ ID: files[i].ID, Path: files[i].Path, Size: files[i].Size, Status: files[i].Status.String(), Health: files[i].Health.String(), Transcode: files[i].Transcode.String(), MD5: fmt.Sprintf("%x", files[i].MD5), UpdatedAt: files[i].UpdatedAt.Format(time.DateTime), }) } buf := bytes.Buffer{} err = templates.ExecuteTemplate(&buf, constants.LIBRARY_TEMPLATE_NAME, data) if err != nil { slog.ErrorContext(r.Context(), "Executing Library 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) } }