pre frontend integration

This commit is contained in:
2023-01-11 12:41:20 +01:00
parent 299b195b52
commit b29cdf802d
4 changed files with 16 additions and 75 deletions

View File

@@ -1,68 +0,0 @@
package server
import (
"log"
"github.com/goccy/go-json"
"github.com/gofiber/websocket/v2"
)
// Websocket handlers
func download(c *websocket.Conn) {
req := DownloadRequest{}
c.ReadJSON(&req)
p := Process{mem: &db, url: req.Url, params: req.Params}
p.Start()
c.WriteJSON(req)
}
func getFormats(c *websocket.Conn) {
log.Println("Requesting formats")
mtype, msg, _ := c.ReadMessage()
req := DownloadRequest{}
json.Unmarshal(msg, &req)
p := Process{mem: &db, url: req.Url}
p.GetFormatsSync()
c.WriteMessage(mtype, msg)
}
func status(c *websocket.Conn) {
mtype, _, _ := c.ReadMessage()
all := db.All()
msg, _ := json.Marshal(all)
c.WriteMessage(mtype, msg)
}
func abort(c *websocket.Conn) {
mtype, msg, _ := c.ReadMessage()
req := AbortRequest{}
json.Unmarshal(msg, &req)
p := db.Get(req.Id)
p.Kill()
c.WriteMessage(mtype, msg)
}
func abortAll(c *websocket.Conn) {
keys := db.Keys()
for _, key := range keys {
proc := db.Get(key)
if proc != nil {
proc.Kill()
}
}
}
func hotUpdate(c *websocket.Conn) {
}

View File

@@ -2,6 +2,7 @@ package server
import (
"bufio"
"regexp"
"github.com/goccy/go-json"
@@ -11,6 +12,7 @@ import (
"strings"
"time"
"github.com/marcopeocchi/fazzoletti/slices"
"github.com/marcopeocchi/yt-dlp-web-ui/server/rx"
)
@@ -47,6 +49,13 @@ type Process struct {
// This approach is anyhow not perfect: quotes are not escaped properly.
// Each process is not identified by its PID but by a UUIDv2
func (p *Process) Start() {
// escape bash variable escaping and command piping, you'll never know
// what they might come with...
p.params = slices.Filter(p.params, func(e string) bool {
match, _ := regexp.MatchString(`(\$\{)|(\&\&)`, e)
return !match
})
params := append([]string{
strings.Split(p.url, "?list")[0], //no playlist
"--newline",
@@ -87,7 +96,7 @@ func (p *Process) Start() {
}()
// --------------- progress block --------------- //
// unbuffered channe connected to stdout
// unbuffered channel connected to stdout
eventChan := make(chan string)
// spawn a goroutine that does the dirty job of parsing the stdout