Skip to content

Commit

Permalink
add support of sending magnet url in path
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Sep 24, 2024
1 parent 51df763 commit b6fcf39
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ assets/dist/*

.env

templates/partials/extend.html
templates/partials/extend.html
/.idea/dataSources.xml
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion handlers/resource/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
})
}
14 changes: 8 additions & 6 deletions handlers/resource/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit b6fcf39

Please sign in to comment.