From b6fcf39d359b8fc03d8cf13ec123d2e17c0f8f6d Mon Sep 17 00:00:00 2001 From: Pavel Tatarskiy Date: Wed, 25 Sep 2024 01:24:01 +0300 Subject: [PATCH] add support of sending magnet url in path --- .gitignore | 3 ++- README.md | 4 ++-- handlers/resource/handler.go | 9 ++++++++- handlers/resource/post.go | 14 ++++++++------ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index a0d2266..56a294e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ assets/dist/* .env -templates/partials/extend.html \ No newline at end of file +templates/partials/extend.html +/.idea/dataSources.xml diff --git a/README.md b/README.md index b730474..405e1f2 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,9 @@ Some features to mention: - [x] Magnet => DDL - [x] Magnet => Torrent - [x] Torrent => ZIP -- [ ] Misc +- [x] Misc - [x] Feedback form - - [ ] Allow magnet-url as query string + - [x] Allow magnet-url as query string - [x] Add dark/light theme switch - [x] Chrome extension integration - [x] Embed support diff --git a/handlers/resource/handler.go b/handlers/resource/handler.go index 0303eb1..221ca89 100644 --- a/handlers/resource/handler.go +++ b/handlers/resource/handler.go @@ -5,6 +5,7 @@ import ( j "github.com/webtor-io/web-ui-v2/handlers/job" "github.com/webtor-io/web-ui-v2/services/api" "github.com/webtor-io/web-ui-v2/services/template" + "strings" ) type Handler struct { @@ -21,5 +22,11 @@ func RegisterHandler(r *gin.Engine, tm *template.Manager, api *api.Api, jobs *j. tb: tm.MustRegisterViews("resource/*").WithHelper(helper).WithLayout("main"), } r.POST("/", h.post) - r.GET("/:resource_id", h.get) + r.GET("/:resource_id", func(c *gin.Context) { + if strings.HasPrefix(c.Param("resource_id"), "magnet") { + h.post(c) + return + } + h.get(c) + }) } diff --git a/handlers/resource/post.go b/handlers/resource/post.go index 6f822de..a4fdf6f 100644 --- a/handlers/resource/post.go +++ b/handlers/resource/post.go @@ -6,6 +6,7 @@ import ( "io" "mime/multipart" "net/http" + "strings" "github.com/gin-gonic/gin" sv "github.com/webtor-io/web-ui-v2/services" @@ -20,13 +21,14 @@ type PostArgs struct { Claims *api.Claims } -func (s *Handler) bindPostArgs(c *gin.Context) (*PostArgs, error) { +func (s *Handler) bindArgs(c *gin.Context) (*PostArgs, error) { file, _ := c.FormFile("resource") instruction, _ := c.GetPostForm("instruction") - r, ok := c.GetPostFormArray("resource") - query := "" - if ok { - query = r[0] + query, _ := c.GetPostForm("resource") + if query == "" && strings.HasPrefix(c.Request.URL.Path, "/magnet") { + query = strings.TrimPrefix(c.Request.URL.Path, "/") + c.Request.URL.RawQuery + } + if query != "" { sha1 := sv.SHA1R.Find([]byte(query)) if sha1 == nil { return &PostArgs{Query: query}, errors.Errorf("wrong resource provided query=%v", query) @@ -75,7 +77,7 @@ func (s *Handler) post(c *gin.Context) { args *PostArgs loadJob *job.Job ) - args, err = s.bindPostArgs(c) + args, err = s.bindArgs(c) if err != nil { indexTpl.HTMLWithErr(errors.Wrap(err, "wrong args provided"), http.StatusBadRequest, c, d) }