streamd: optional keys, per-stream proxy, auto deps; agent module.yaml sync; RTE AutoPlay; Telegram release announces
- streamd: MPD-only start, proxy field, ffmpeg/NRE/mp4decrypt auto-install - GET/PUT /api/modules for module.yaml sync; agent sync_modules - RTE Live-tab AutoPlay fixes; modules zip named bbc/rte/tg4.zip - Telegram notify scripts wired into Forgejo release workflows
This commit is contained in:
parent
f9c28be3f1
commit
ac8701b5a6
36 changed files with 1460 additions and 143 deletions
157
apps/streamd/internal/api/modules.go
Normal file
157
apps/streamd/internal/api/modules.go
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"drmdecryption/modhost"
|
||||
"drmdecryption/repo"
|
||||
)
|
||||
|
||||
// moduleInfo is one modules/<name>/ entry for agent sync.
|
||||
type moduleInfo struct {
|
||||
Name string `json:"name"`
|
||||
HasYAML bool `json:"has_yaml"`
|
||||
YAMLSHA256 string `json:"yaml_sha256,omitempty"`
|
||||
}
|
||||
|
||||
var moduleNameRe = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$`)
|
||||
|
||||
func (h *Handler) modules(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/modules")
|
||||
path = strings.Trim(path, "/")
|
||||
|
||||
switch {
|
||||
case path == "" && r.Method == http.MethodGet:
|
||||
h.listModules(w, r)
|
||||
case path != "" && !strings.Contains(path, "/") && r.Method == http.MethodGet:
|
||||
h.getModule(w, r, path)
|
||||
case path != "" && !strings.Contains(path, "/") && r.Method == http.MethodPut:
|
||||
h.putModule(w, r, path)
|
||||
default:
|
||||
writeErr(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) listModules(w http.ResponseWriter, r *http.Request) {
|
||||
root := repo.Root()
|
||||
dir := modhost.ModulesDir(root)
|
||||
ents, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"modules": []moduleInfo{}})
|
||||
return
|
||||
}
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
out := []moduleInfo{}
|
||||
for _, e := range ents {
|
||||
if !e.IsDir() || strings.HasPrefix(e.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
info := moduleYAMLInfo(filepath.Join(dir, e.Name()), e.Name())
|
||||
out = append(out, info)
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"modules": out})
|
||||
}
|
||||
|
||||
func (h *Handler) getModule(w http.ResponseWriter, r *http.Request, name string) {
|
||||
if !moduleNameRe.MatchString(name) {
|
||||
writeErr(w, http.StatusBadRequest, "invalid module name")
|
||||
return
|
||||
}
|
||||
info := moduleYAMLInfo(filepath.Join(modhost.ModulesDir(repo.Root()), name), name)
|
||||
writeJSON(w, http.StatusOK, info)
|
||||
}
|
||||
|
||||
func (h *Handler) putModule(w http.ResponseWriter, r *http.Request, name string) {
|
||||
if !h.auth(w, r) {
|
||||
return
|
||||
}
|
||||
if !moduleNameRe.MatchString(name) {
|
||||
writeErr(w, http.StatusBadRequest, "invalid module name")
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, 2<<20))
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
yamlBytes, err := extractModuleYAML(r.Header.Get("Content-Type"), body)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if len(strings.TrimSpace(string(yamlBytes))) == 0 {
|
||||
writeErr(w, http.StatusBadRequest, "empty module.yaml")
|
||||
return
|
||||
}
|
||||
|
||||
modDir := filepath.Join(modhost.ModulesDir(repo.Root()), name)
|
||||
if err := os.MkdirAll(modDir, 0o755); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
dest := filepath.Join(modDir, "module.yaml")
|
||||
tmp := dest + ".tmp"
|
||||
if err := os.WriteFile(tmp, yamlBytes, 0o644); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if err := os.Rename(tmp, dest); err != nil {
|
||||
_ = os.Remove(tmp)
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
info := moduleYAMLInfo(modDir, name)
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"ok": true,
|
||||
"module": info,
|
||||
})
|
||||
}
|
||||
|
||||
func moduleYAMLInfo(modDir, name string) moduleInfo {
|
||||
info := moduleInfo{Name: name}
|
||||
b, err := os.ReadFile(filepath.Join(modDir, "module.yaml"))
|
||||
if err != nil {
|
||||
return info
|
||||
}
|
||||
sum := sha256.Sum256(b)
|
||||
info.HasYAML = true
|
||||
info.YAMLSHA256 = hex.EncodeToString(sum[:])
|
||||
return info
|
||||
}
|
||||
|
||||
// extractModuleYAML accepts raw YAML or JSON {"yaml":"..."}.
|
||||
func extractModuleYAML(contentType string, body []byte) ([]byte, error) {
|
||||
ct := strings.ToLower(strings.TrimSpace(strings.Split(contentType, ";")[0]))
|
||||
switch ct {
|
||||
case "application/json", "text/json":
|
||||
var wrap struct {
|
||||
YAML string `json:"yaml"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &wrap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(wrap.YAML) == "" {
|
||||
return nil, errors.New("empty yaml field")
|
||||
}
|
||||
return []byte(wrap.YAML), nil
|
||||
default:
|
||||
// text/yaml, application/x-yaml, or bare body
|
||||
return body, nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue