Refactoring

This commit is contained in:
2023-01-12 21:10:23 +01:00
parent d93fc37943
commit 658cb64a02
7 changed files with 30 additions and 14 deletions

View File

@@ -167,6 +167,10 @@ func (p *Process) Complete() {
// Kill a process and remove it from the memory
func (p *Process) Kill() error {
// yt-dlp uses multiple child process the parent process
// has been spawned with setPgid = true. To properly kill
// all subprocesses a SIGTERM need to be sent to the correct
// process group
pgid, err := syscall.Getpgid(p.proc.Pid)
if err != nil {
return err
@@ -177,6 +181,7 @@ func (p *Process) Kill() error {
return err
}
// Returns the available format for this URL
func (p *Process) GetFormatsSync() (DownloadFormats, error) {
cmd := exec.Command(cfg.GetConfig().DownloaderPath, p.url, "-J")
stdout, err := cmd.Output()

View File

@@ -31,11 +31,12 @@ func RunBlocking(ctx context.Context) {
app := fiber.New()
app.Use(cors.New())
app.Use("/", filesystem.New(filesystem.Config{
Root: http.FS(fe),
}))
// RPC handlers
// websocket
app.Get("/ws-rpc", websocket.New(func(c *websocket.Conn) {
for {
mtype, reader, err := c.NextReader()
@@ -51,7 +52,7 @@ func RunBlocking(ctx context.Context) {
io.Copy(writer, res)
}
}))
// http-post
app.Post("/http-rpc", func(c *fiber.Ctx) error {
reader := c.Context().RequestBodyStream()
writer := c.Response().BodyWriter()

View File

@@ -97,12 +97,14 @@ func (t *Service) FreeSpace(args NoArgs, free *uint64) error {
return err
}
// Return a flattned tree of the download directory
func (t *Service) DirectoryTree(args NoArgs, tree *[]string) error {
dfsTree, err := sys.DirectoryTree()
*tree = *dfsTree
return err
}
// Updates the yt-dlp binary using its builtin function
func (t *Service) UpdateExecutable(args NoArgs, updated *bool) error {
log.Println("Updating yt-dlp executable to the latest release")
err := updater.UpdateExecutable()

View File

@@ -1,11 +1,13 @@
package server
// Progress for the Running call
type DownloadProgress struct {
Percentage string `json:"percentage"`
Speed float32 `json:"speed"`
ETA int `json:"eta"`
}
// Used to deser the yt-dlp -J output
type DownloadInfo struct {
URL string `json:"url"`
Title string `json:"title"`
@@ -17,6 +19,7 @@ type DownloadInfo struct {
Extension string `json:"ext"`
}
// Used to deser the formats in the -J output
type DownloadFormats struct {
Formats []Format `json:"formats"`
Best Format `json:"best"`
@@ -25,6 +28,7 @@ type DownloadFormats struct {
URL string `json:"url"`
}
// A skimmed yt-dlp format node
type Format struct {
Format_id string `json:"format_id"`
Format_note string `json:"format_note"`

View File

@@ -8,6 +8,7 @@ import (
var path = config.Instance().GetConfig().DownloaderPath
// Update using the builtin function of yt-dlp
func UpdateExecutable() error {
cmd := exec.Command(path, "-U")
cmd.Start()