it just works
This commit is contained in:
60
server/config/config_singleton.go
Normal file
60
server/config/config_singleton.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var lock = &sync.Mutex{}
|
||||
|
||||
type serverConfig struct {
|
||||
Port int `yaml:"port"`
|
||||
DownloadPath string `yaml:"downloadPath"`
|
||||
DownloaderPath string `yaml:"downloaderPath"`
|
||||
}
|
||||
|
||||
type config struct {
|
||||
cfg serverConfig
|
||||
}
|
||||
|
||||
func (c *config) LoadFromFile(filename string) (serverConfig, error) {
|
||||
bytes, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return serverConfig{}, err
|
||||
}
|
||||
|
||||
yaml.Unmarshal(bytes, &c.cfg)
|
||||
|
||||
return c.cfg, nil
|
||||
}
|
||||
|
||||
func (c *config) GetConfig() serverConfig {
|
||||
return c.cfg
|
||||
}
|
||||
|
||||
func (c *config) SetPort(port int) {
|
||||
c.cfg.Port = port
|
||||
}
|
||||
|
||||
func (c *config) DownloadPath(path string) {
|
||||
c.cfg.DownloadPath = path
|
||||
}
|
||||
|
||||
func (c *config) DownloaderPath(path string) {
|
||||
c.cfg.DownloaderPath = path
|
||||
}
|
||||
|
||||
var instance *config
|
||||
|
||||
func Instance() *config {
|
||||
if instance == nil {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
if instance == nil {
|
||||
instance = &config{serverConfig{}}
|
||||
}
|
||||
}
|
||||
return instance
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/marcopeocchi/fazzoletti/slices"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/rx"
|
||||
)
|
||||
|
||||
@@ -23,7 +25,9 @@ const template = `download:
|
||||
"speed":%(progress.speed)s
|
||||
}`
|
||||
|
||||
const driver = "yt-dlp"
|
||||
var (
|
||||
cfg = config.Instance()
|
||||
)
|
||||
|
||||
type ProgressTemplate struct {
|
||||
Percentage string `json:"percentage"`
|
||||
@@ -43,12 +47,17 @@ type Process struct {
|
||||
proc *os.Process
|
||||
}
|
||||
|
||||
type downloadOutput struct {
|
||||
path string
|
||||
filaneme string
|
||||
}
|
||||
|
||||
// Starts spawns/forks a new yt-dlp process and parse its stdout.
|
||||
// The process is spawned to outputting a custom progress text that
|
||||
// Resembles a JSON Object in order to Unmarshal it later.
|
||||
// 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() {
|
||||
func (p *Process) Start(path, filename string) {
|
||||
// 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 {
|
||||
@@ -56,6 +65,18 @@ func (p *Process) Start() {
|
||||
return !match
|
||||
})
|
||||
|
||||
out := downloadOutput{
|
||||
path: cfg.GetConfig().DownloadPath,
|
||||
filaneme: "%(title)s.%(ext)s",
|
||||
}
|
||||
|
||||
if path != "" {
|
||||
out.path = path
|
||||
}
|
||||
if filename != "" {
|
||||
out.filaneme = filename + ".%(ext)s"
|
||||
}
|
||||
|
||||
params := append([]string{
|
||||
strings.Split(p.url, "?list")[0], //no playlist
|
||||
"--newline",
|
||||
@@ -63,11 +84,11 @@ func (p *Process) Start() {
|
||||
"--no-playlist",
|
||||
"--progress-template", strings.ReplaceAll(template, "\n", ""),
|
||||
"-o",
|
||||
"./downloads/%(title)s.%(ext)s",
|
||||
fmt.Sprintf("%s/%s", out.path, out.filaneme),
|
||||
}, p.params...)
|
||||
|
||||
// ----------------- main block ----------------- //
|
||||
cmd := exec.Command(driver, params...)
|
||||
cmd := exec.Command(cfg.GetConfig().DownloaderPath, params...)
|
||||
r, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
@@ -85,7 +106,7 @@ func (p *Process) Start() {
|
||||
// ----------------- info block ----------------- //
|
||||
// spawn a goroutine that retrieves the info for the download
|
||||
go func() {
|
||||
cmd := exec.Command(driver, p.url, "-J")
|
||||
cmd := exec.Command(cfg.GetConfig().DownloaderPath, p.url, "-J")
|
||||
stdout, err := cmd.Output()
|
||||
if err != nil {
|
||||
log.Println("Cannot retrieve info for", p.url)
|
||||
@@ -100,18 +121,19 @@ func (p *Process) Start() {
|
||||
eventChan := make(chan string)
|
||||
|
||||
// spawn a goroutine that does the dirty job of parsing the stdout
|
||||
// fill the channel with as many stdout line as yt-dlp produces (producer)
|
||||
// filling the channel with as many stdout line as yt-dlp produces (producer)
|
||||
go func() {
|
||||
defer cmd.Wait()
|
||||
defer r.Close()
|
||||
defer p.Complete()
|
||||
defer close(eventChan)
|
||||
for scan.Scan() {
|
||||
eventChan <- scan.Text()
|
||||
}
|
||||
cmd.Wait()
|
||||
}()
|
||||
|
||||
// do the unmarshal operation every 500ms (consumer)
|
||||
go rx.Sample(time.Millisecond*500, eventChan, func(text string) {
|
||||
go rx.Debounce(time.Millisecond*500, eventChan, func(text string) {
|
||||
stdout := ProgressTemplate{}
|
||||
err := json.Unmarshal([]byte(text), &stdout)
|
||||
if err == nil {
|
||||
@@ -147,7 +169,7 @@ func (p *Process) Kill() error {
|
||||
}
|
||||
|
||||
func (p *Process) GetFormatsSync() (DownloadFormats, error) {
|
||||
cmd := exec.Command(driver, p.url, "-J")
|
||||
cmd := exec.Command(cfg.GetConfig().DownloaderPath, p.url, "-J")
|
||||
stdout, err := cmd.Output()
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -23,7 +23,7 @@ func init() {
|
||||
|
||||
func RunBlocking(ctx context.Context) {
|
||||
fe := ctx.Value("frontend").(fs.SubFS)
|
||||
port := ctx.Value("port")
|
||||
port := ctx.Value("port").(int)
|
||||
|
||||
service := new(Service)
|
||||
rpc.Register(service)
|
||||
@@ -62,5 +62,5 @@ func RunBlocking(ctx context.Context) {
|
||||
|
||||
app.Server().StreamRequestBody = true
|
||||
|
||||
log.Fatal(app.Listen(fmt.Sprintf(":%s", port)))
|
||||
log.Fatal(app.Listen(fmt.Sprintf(":%d", port)))
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"log"
|
||||
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/sys"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/updater"
|
||||
)
|
||||
|
||||
type Service int
|
||||
@@ -12,18 +13,27 @@ type Running []ProcessResponse
|
||||
type Pending []string
|
||||
|
||||
type NoArgs struct{}
|
||||
|
||||
type Args struct {
|
||||
Id string
|
||||
URL string
|
||||
Params []string
|
||||
}
|
||||
|
||||
type DownloadSpecificArgs struct {
|
||||
Id string
|
||||
URL string
|
||||
Path string
|
||||
Rename string
|
||||
Params []string
|
||||
}
|
||||
|
||||
// Exec spawns a Process.
|
||||
// The result of the execution is the newly spawned process Id.
|
||||
func (t *Service) Exec(args Args, result *string) error {
|
||||
func (t *Service) Exec(args DownloadSpecificArgs, result *string) error {
|
||||
log.Printf("Spawning new process for %s\n", args.URL)
|
||||
p := Process{mem: &db, url: args.URL, params: args.Params}
|
||||
p.Start()
|
||||
p.Start(args.Path, args.Rename)
|
||||
*result = p.id
|
||||
return nil
|
||||
}
|
||||
@@ -86,7 +96,17 @@ func (t *Service) FreeSpace(args NoArgs, free *uint64) error {
|
||||
}
|
||||
|
||||
func (t *Service) DirectoryTree(args NoArgs, tree *[]string) error {
|
||||
dfsTree, err := sys.DirectoryTree("downloads")
|
||||
dfsTree, err := sys.DirectoryTree()
|
||||
*tree = *dfsTree
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *Service) UpdateExecutable(args NoArgs, updated *bool) error {
|
||||
err := updater.UpdateExecutable()
|
||||
if err != nil {
|
||||
*updated = true
|
||||
return err
|
||||
}
|
||||
*updated = false
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
@@ -13,20 +14,20 @@ import (
|
||||
// FreeSpace gets the available Bytes writable to download directory
|
||||
func FreeSpace() (uint64, error) {
|
||||
var stat unix.Statfs_t
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
unix.Statfs(wd+"/downloads", &stat)
|
||||
unix.Statfs(config.Instance().GetConfig().DownloadPath, &stat)
|
||||
return (stat.Bavail * uint64(stat.Bsize)), nil
|
||||
}
|
||||
|
||||
func DirectoryTree(rootPath string) (*[]string, error) {
|
||||
// Build a directory tree started from the specified path using DFS.
|
||||
// Then return the flattened tree represented as a list.
|
||||
func DirectoryTree() (*[]string, error) {
|
||||
type Node struct {
|
||||
path string
|
||||
children []Node
|
||||
}
|
||||
|
||||
rootPath := config.Instance().GetConfig().DownloadPath
|
||||
|
||||
stack := internal.Stack[Node]{
|
||||
Nodes: make([]*internal.Node[Node], 5),
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ type AbortRequest struct {
|
||||
|
||||
// struct representing the intent to start a download
|
||||
type DownloadRequest struct {
|
||||
Url string `json:"url"`
|
||||
Params []string `json:"params"`
|
||||
Url string `json:"url"`
|
||||
Params []string `json:"params"`
|
||||
RenameTo string `json:"renameTo"`
|
||||
}
|
||||
|
||||
45
server/updater/forced_update.go
Normal file
45
server/updater/forced_update.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
const (
|
||||
gitHubAPILatest = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest"
|
||||
gitHubAPIDownload = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/download"
|
||||
)
|
||||
|
||||
var (
|
||||
client = &http.Client{
|
||||
CheckRedirect: http.DefaultClient.CheckRedirect,
|
||||
}
|
||||
)
|
||||
|
||||
func getLatestReleaseTag() (string, error) {
|
||||
res, err := client.Get(gitHubAPILatest)
|
||||
if err != nil {
|
||||
log.Println("Cannot get release tag from GitHub API")
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
|
||||
if err != nil {
|
||||
log.Println("Cannot parse response from GitHub API")
|
||||
return "", err
|
||||
}
|
||||
|
||||
tag := ReleaseLatestResponse{}
|
||||
json.Unmarshal(body, &tag)
|
||||
|
||||
return tag.TagName, nil
|
||||
}
|
||||
|
||||
func ForceUpdate() {
|
||||
getLatestReleaseTag()
|
||||
}
|
||||
6
server/updater/types.go
Normal file
6
server/updater/types.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package updater
|
||||
|
||||
type ReleaseLatestResponse struct {
|
||||
Name string `json:"name"`
|
||||
TagName string `json:"tag_name"`
|
||||
}
|
||||
17
server/updater/update.go
Normal file
17
server/updater/update.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
||||
)
|
||||
|
||||
var path = config.Instance().GetConfig().DownloaderPath
|
||||
|
||||
func UpdateExecutable() error {
|
||||
cmd := exec.Command(path, "-U")
|
||||
cmd.Start()
|
||||
|
||||
err := cmd.Wait()
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user