49 feat add cookies (#98)
* build client side validation and submission * enabled cookies submission, bug fixes
This commit is contained in:
@@ -71,3 +71,8 @@ type DownloadRequest struct {
|
||||
Rename string `json:"rename"`
|
||||
Params []string `json:"params"`
|
||||
}
|
||||
|
||||
// struct representing request of creating a netscape cookies file
|
||||
type SetCookiesRequest struct {
|
||||
Cookies string `json:"cookies"`
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user