49 feat add cookies (#98)

* build client side validation and submission

* enabled cookies submission, bug fixes
This commit is contained in:
Marco
2023-10-21 15:46:24 +02:00
committed by GitHub
parent 9361d9ce29
commit 8eb2831bc6
10 changed files with 238 additions and 12 deletions

View File

@@ -21,5 +21,6 @@ func ApplyRouter(db *internal.MemoryDB, mq *internal.MessageQueue) func(chi.Rout
r.Use(middlewares.Authenticated)
r.Post("/exec", h.Exec())
r.Get("/running", h.Running())
r.Post("/cookies", h.SetCookies())
}
}

View File

@@ -19,7 +19,7 @@ func (h *Handler) Exec() http.HandlerFunc {
req := internal.DownloadRequest{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
if err := json.NewDecoder(r.Body).DecodeContext(r.Context(), &req); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@@ -29,7 +29,7 @@ func (h *Handler) Exec() http.HandlerFunc {
return
}
err = json.NewEncoder(w).Encode(id)
err = json.NewEncoder(w).EncodeContext(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@@ -48,7 +48,34 @@ func (h *Handler) Running() http.HandlerFunc {
return
}
err = json.NewEncoder(w).Encode(res)
err = json.NewEncoder(w).EncodeContext(r.Context(), res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func (h *Handler) SetCookies() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
req := new(internal.SetCookiesRequest)
err := json.NewDecoder(r.Body).DecodeContext(r.Context(), req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = h.service.SetCookies(r.Context(), req.Cookies)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).EncodeContext(r.Context(), "ok")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

View File

@@ -3,6 +3,7 @@ package rest
import (
"context"
"errors"
"os"
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
)
@@ -36,3 +37,15 @@ func (s *Service) Running(ctx context.Context) (*[]internal.ProcessResponse, err
return s.db.All(), nil
}
}
func (s *Service) SetCookies(ctx context.Context, cookies string) error {
fd, err := os.Create("cookies.txt")
if err != nil {
return err
}
defer fd.Close()
fd.WriteString(cookies)
return nil
}