diff --git a/server/process.go b/server/process.go index 10ba5d3..0f92180 100644 --- a/server/process.go +++ b/server/process.go @@ -137,17 +137,17 @@ func (p *Process) Kill() error { return err } -func (p *Process) GetFormatsSync() (DownloadInfo, error) { +func (p *Process) GetFormatsSync() (DownloadFormats, error) { cmd := exec.Command(driver, p.url, "-J") stdout, err := cmd.Output() if err != nil { - return DownloadInfo{}, err + return DownloadFormats{}, err } cmd.Wait() - info := DownloadInfo{URL: p.url} + info := DownloadFormats{URL: p.url} json.Unmarshal(stdout, &info) return info, nil diff --git a/server/server.go b/server/server.go index ae139e4..947eed7 100644 --- a/server/server.go +++ b/server/server.go @@ -3,9 +3,11 @@ package server import ( "context" "fmt" + "io" "io/fs" "log" "net/http" + "net/rpc" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/filesystem" @@ -22,6 +24,9 @@ func RunBlocking(ctx context.Context) { fe := ctx.Value("frontend").(fs.SubFS) port := ctx.Value("port") + service := new(Service) + rpc.Register(service) + app := fiber.New() app.Use("/", filesystem.New(filesystem.Config{ @@ -30,32 +35,16 @@ func RunBlocking(ctx context.Context) { app.Get("/ws", websocket.New(func(c *websocket.Conn) { for { - mtype, msg, err := c.ReadMessage() + mtype, reader, err := c.NextReader() if err != nil { break } - - switch string(msg) { - case "send-url-format-selection": - getFormats(c) - case "send-url": - download(c) - case "abort": - abort(c) - case "abort-all": - abortAll(c) - case "status": - status(c) - case "update-bin": - hotUpdate(c) - } - - log.Printf("Read: %s", msg) - - err = c.WriteMessage(mtype, msg) + writer, err := c.NextWriter(mtype) if err != nil { break } + res := NewRPCRequest(reader).Call() + io.Copy(writer, res) } })) diff --git a/server/service.go b/server/service.go index 0e5c8c6..1889572 100644 --- a/server/service.go +++ b/server/service.go @@ -34,6 +34,14 @@ func (t *Service) Progess(args Args, progress *DownloadProgress) error { return nil } +// Progess retrieves the Progress of a specific Process given its Id +func (t *Service) Formats(args Args, progress *DownloadFormats) error { + var err error + p := Process{url: args.URL} + *progress, err = p.GetFormatsSync() + return err +} + // Pending retrieves a slice of all Pending/Running processes ids func (t *Service) Pending(args NoArgs, pending *Pending) error { *pending = Pending(db.Keys()) diff --git a/server/types.go b/server/types.go index 4138249..26fcede 100644 --- a/server/types.go +++ b/server/types.go @@ -17,6 +17,23 @@ type DownloadInfo struct { Extension string `json:"ext"` } +type DownloadFormats struct { + Formats []Format `json:"formats"` + Best Format `json:"best"` + Thumbnail string `json:"thumbnail"` + Title string `json:"title"` + URL string `json:"url"` +} + +type Format struct { + Format_id string `json:"format_id"` + Format_note string `json:"format_note"` + FPS float32 `json:"fps"` + Resolution string `json:"resolution"` + VCodec string `json:"vcodec"` + ACodec string `json:"acodec"` +} + // struct representing the response sent to the client // as JSON-RPC result field type ProcessResponse struct {