@@ -1,6 +1,8 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
@@ -8,12 +10,14 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/utils"
|
||||
)
|
||||
|
||||
@@ -194,3 +198,50 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
func BulkDownload(mdb *internal.MemoryDB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
procs := slices.DeleteFunc(*mdb.All(), func(e internal.ProcessResponse) bool {
|
||||
return e.Progress.Status != 2 // status completed
|
||||
})
|
||||
|
||||
var (
|
||||
buff bytes.Buffer
|
||||
zipWriter = zip.NewWriter(&buff)
|
||||
)
|
||||
|
||||
for _, p := range procs {
|
||||
wr, err := zipWriter.Create(filepath.Base(p.Output.SavedFilePath))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
fd, err := os.Open(p.Output.SavedFilePath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(wr, fd)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err := zipWriter.Close()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add(
|
||||
"Content-Disposition",
|
||||
"inline; filename=download-archive-"+time.Now().Format(time.RFC3339)+".zip",
|
||||
)
|
||||
w.Header().Set("Content-Type", "application/zip")
|
||||
|
||||
io.Copy(w, &buff)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,8 +55,9 @@ type Process struct {
|
||||
}
|
||||
|
||||
type DownloadOutput struct {
|
||||
Path string
|
||||
Filename string
|
||||
Path string
|
||||
Filename string
|
||||
SavedFilePath string `json:"savedFilePath"`
|
||||
}
|
||||
|
||||
// Starts spawns/forks a new yt-dlp process and parse its stdout.
|
||||
@@ -91,6 +92,8 @@ func (p *Process) Start() {
|
||||
|
||||
buildFilename(&p.Output)
|
||||
|
||||
go p.GetFileName(&out)
|
||||
|
||||
params := []string{
|
||||
strings.Split(p.Url, "?list")[0], //no playlist
|
||||
"--newline",
|
||||
@@ -269,6 +272,24 @@ func (p *Process) GetFormatsSync() (DownloadFormats, error) {
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (p *Process) GetFileName(o *DownloadOutput) error {
|
||||
cmd := exec.Command(
|
||||
config.Instance().DownloaderPath,
|
||||
"--print", "filename",
|
||||
"-o", fmt.Sprintf("%s/%s", o.Path, o.Filename),
|
||||
p.Url,
|
||||
)
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.Output.SavedFilePath = strings.Trim(string(out), "\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Process) SetPending() {
|
||||
// Since video's title isn't available yet, fill in with the URL.
|
||||
p.Info = DownloadInfo{
|
||||
|
||||
@@ -163,6 +163,7 @@ func newServer(c serverConfig) *http.Server {
|
||||
r.Post("/delete", handlers.DeleteFile)
|
||||
r.Get("/d/{id}", handlers.DownloadFile)
|
||||
r.Get("/v/{id}", handlers.SendFile)
|
||||
r.Get("/bulk", handlers.BulkDownload(c.mdb))
|
||||
})
|
||||
|
||||
// Authentication routes
|
||||
|
||||
Reference in New Issue
Block a user