Skip to content

Commit

Permalink
move handlers directory to the root
Browse files Browse the repository at this point in the history
add Tools
add "Download Torrent File" button
  • Loading branch information
vintikzzz committed Sep 24, 2024
1 parent 5be45e4 commit 51df763
Show file tree
Hide file tree
Showing 42 changed files with 245 additions and 59 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ Some features to mention:
- [x] Base player
- [x] Subtitles support
- [x] OpenSubtitles support
- [ ] Subtitle uploading support
- [ ] Chromecast support
- [ ] Subtitle size control
- [ ] Embed control
- [ ] Subtitle uploading support
- [ ] Subtitle size control
- [x] Authentication
- [x] Passwordless authentication
- [x] Patreon account linking
- [x] Ads and statistic integration support
- [ ] Tools
- [ ] Torrent => DDL
- [ ] Magnet => DDL
- [ ] Magnet => Torrent
- [x] Tools
- [x] Torrent => DDL
- [x] Magnet => DDL
- [x] Magnet => Torrent
- [x] Torrent => ZIP
- [ ] Misc
- [x] Feedback form
- [ ] Allow magnet-url as query string
Expand Down
1 change: 1 addition & 0 deletions assets/src/js/lib/loadAsyncView.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function renderBody(target, body, template) {
const event = new CustomEvent('async:' + template, { detail });
window.dispatchEvent(event);
}
target.scrollIntoView();
}

export default loadAsyncView;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action

import (
wj "github.com/webtor-io/web-ui-v2/handlers/job"
m "github.com/webtor-io/web-ui-v2/services/models"
"net/http"

Expand All @@ -9,7 +10,6 @@ import (
"github.com/webtor-io/web-ui-v2/services/api"
"github.com/webtor-io/web-ui-v2/services/job"
"github.com/webtor-io/web-ui-v2/services/template"
wj "github.com/webtor-io/web-ui-v2/services/web/job"
)

type PostArgs struct {
Expand Down Expand Up @@ -45,6 +45,9 @@ func RegisterHandler(r *gin.Engine, tm *template.Manager, jobs *wj.Handler) {
r.POST("/download-dir", func(c *gin.Context) {
h.post(c, "download")
})
r.POST("/download-torrent", func(c *gin.Context) {
h.post(c, "download-torrent")
})
r.POST("/preview-image", func(c *gin.Context) {
h.post(c, "preview-image")
})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package embed

import (
j "github.com/webtor-io/web-ui-v2/handlers/job"
"net/http"

"github.com/gin-gonic/gin"
"github.com/webtor-io/web-ui-v2/services/embed"
"github.com/webtor-io/web-ui-v2/services/template"
j "github.com/webtor-io/web-ui-v2/services/web/job"
)

type Handler struct {
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 9 additions & 1 deletion services/web/index/handler.go → handlers/index/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package index

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/webtor-io/web-ui-v2/services/template"
)

type Data struct {
Instruction string
}

type Handler struct {
Expand All @@ -19,8 +21,14 @@ func RegisterHandler(r *gin.Engine, tm *template.Manager) {
tb: tm.MustRegisterViews("*").WithLayout("main"),
}
r.GET("/", h.index)
r.GET("/torrent-to-ddl", h.index)
r.GET("/torrent-to-zip", h.index)
r.GET("/magnet-to-ddl", h.index)
r.GET("/magnet-to-torrent", h.index)
}

func (s *Handler) index(c *gin.Context) {
s.tb.Build("index").HTML(http.StatusOK, c, &Data{})
s.tb.Build("index").HTML(http.StatusOK, c, &Data{
Instruction: strings.TrimPrefix(c.Request.URL.Path, "/"),
})
}
2 changes: 1 addition & 1 deletion services/web/job/action.go → handlers/job/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package job

import (
"context"
"github.com/webtor-io/web-ui-v2/handlers/job/script"
"github.com/webtor-io/web-ui-v2/services/models"
"time"

"github.com/gin-gonic/gin"

"github.com/webtor-io/web-ui-v2/services/api"
"github.com/webtor-io/web-ui-v2/services/job"
"github.com/webtor-io/web-ui-v2/services/web/job/script"
)

func (s *Handler) Action(c *gin.Context, claims *api.Claims, resourceID string, itemID string, action string, settings *models.StreamSettings) (j *job.Job, err error) {
Expand Down
2 changes: 1 addition & 1 deletion services/web/job/embed.go → handlers/job/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package job

import (
"context"
"github.com/webtor-io/web-ui-v2/handlers/job/script"
"github.com/webtor-io/web-ui-v2/services/models"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/webtor-io/web-ui-v2/services/api"
"github.com/webtor-io/web-ui-v2/services/job"
"github.com/webtor-io/web-ui-v2/services/web/job/script"
)

func (s *Handler) Embed(c *gin.Context, hCl *http.Client, claims *api.Claims, settings *models.EmbedSettings) (j *job.Job, err error) {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion services/web/job/load.go → handlers/job/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package job

import (
"context"
"github.com/webtor-io/web-ui-v2/handlers/job/script"
"time"

"github.com/webtor-io/web-ui-v2/services/api"
"github.com/webtor-io/web-ui-v2/services/job"
"github.com/webtor-io/web-ui-v2/services/web/job/script"
)

func (s *Handler) Load(claims *api.Claims, args *script.LoadArgs) (j *job.Job, err error) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package script

import (
"bytes"
"context"
"crypto/sha1"
"fmt"
"github.com/anacrolix/torrent/metainfo"
"io"
"strings"
"time"
Expand All @@ -28,6 +30,13 @@ type StreamContent struct {
ExternalData *m.ExternalData
}

type TorrentDownload struct {
Data []byte
Infohash string
Name string
Size int
}

func (s *ActionScript) streamContent(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string, template string, settings *m.StreamSettings, vsud *m.VideoStreamUserData) (err error) {
sc := &StreamContent{
Settings: settings,
Expand Down Expand Up @@ -134,6 +143,48 @@ func (s *ActionScript) download(j *job.Job, claims *api.Claims, resourceID strin
return
}

func (s *ActionScript) downloadTorrent(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string) (err error) {
j.InProgress("retrieving torrent")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, err := s.api.GetTorrent(ctx, claims, resourceID)
if err != nil {
return j.Error(err, "failed to retrieve for 30 seconds")
}
defer func(resp io.ReadCloser) {
_ = resp.Close()
}(resp)
torrent, err := io.ReadAll(resp)
if err != nil {
return j.Error(err, "failed to read torrent")
}
mi, err := metainfo.Load(bytes.NewBuffer(torrent))
if err != nil {
return j.Error(err, "failed to load torrent metainfo")
}
info, err := mi.UnmarshalInfo()
if err != nil {
return j.Error(err, "failed to unmarshal torrent metainfo")
}
j.DoneWithMessage("success! download should start right now!")
tpl := s.tb.Build("action/download_torrent").WithLayoutBody(`{{ template "main" . }}`)
name := info.Name
if name == "" {
name = resourceID
}
str, err := tpl.ToString(c, &TorrentDownload{
Data: torrent,
Infohash: resourceID,
Name: name + ".torrent",
Size: len(torrent),
})
if err != nil {
return err
}
j.RenderTemplate("action/download_torrent", strings.TrimSpace(str))
return nil
}

func (s *ActionScript) warmUp(j *job.Job, m string, u string, su string, size int, limitStart int, limitEnd int, tagSuff string, useStatus bool) (err error) {
tag := "download"
if tagSuff != "" {
Expand Down Expand Up @@ -211,6 +262,8 @@ func (s *ActionScript) Run(j *job.Job) (err error) {
switch s.action {
case "download":
return s.download(j, s.claims, s.resourceId, s.itemId)
case "download-torrent":
return s.downloadTorrent(j, s.c, s.claims, s.resourceId)
case "preview-image":
return s.previewImage(j, s.c, s.claims, s.resourceId, s.itemId, s.settings, s.vsud)
case "stream-audio":
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package resource

import (
"github.com/gin-gonic/gin"
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"
j "github.com/webtor-io/web-ui-v2/services/web/job"
)

type Handler struct {
Expand Down
24 changes: 22 additions & 2 deletions services/web/resource/helpers.go → handlers/resource/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ type Pagination struct {
Number bool
}

func (s *Helper) MakeButton(ctx *w.Context, gd *GetData, name string, icon string, endpoint string) *ButtonItem {
func (s *Helper) MakeButton(ctx *w.Context, gd *GetData, name string, action string, endpoint string) *ButtonItem {
return &ButtonItem{
ID: gd.Item.ID,
ItemID: gd.Item.ID,
ResourceID: gd.Resource.ID,
Name: name,
Action: icon,
Action: action,
Endpoint: endpoint,
CSRF: ctx.CSRF,
}
Expand All @@ -57,6 +57,18 @@ func (s *Helper) MakeDirButton(ctx *w.Context, gd *GetData, name string, action
}
}

func (s *Helper) MakeResourceButton(ctx *w.Context, gd *GetData, name string, action string, endpoint string) *ButtonItem {
return &ButtonItem{
ID: gd.Resource.ID,
ItemID: gd.Resource.ID,
ResourceID: gd.Resource.ID,
Name: name,
Action: action,
Endpoint: endpoint,
CSRF: ctx.CSRF,
}
}

func (s *Helper) MakeFileDownload(ctx *w.Context, gd *GetData) *ButtonItem {
return s.MakeButton(ctx, gd,
fmt.Sprintf("Download [%v]", h.Bytes(uint64(gd.Item.Size))),
Expand Down Expand Up @@ -96,6 +108,14 @@ func (s *Helper) MakeDirDownload(ctx *w.Context, gd *GetData) *ButtonItem {
)
}

func (s *Helper) MakeTorrentDownload(ctx *w.Context, gd *GetData) *ButtonItem {
return s.MakeResourceButton(ctx, gd,
fmt.Sprintf("Download Torrent File"),
"download",
"/download-torrent",
)
}

func (s *Helper) HasBreadcrumbs(lr *ra.ListResponse) bool {
hasDir := false
for _, i := range lr.Items {
Expand Down
32 changes: 19 additions & 13 deletions services/web/resource/post.go → handlers/resource/post.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package resource

import (
"github.com/pkg/errors"
"github.com/webtor-io/web-ui-v2/handlers/job/script"
"io"
"mime/multipart"
"net/http"

"github.com/pkg/errors"

"github.com/gin-gonic/gin"
sv "github.com/webtor-io/web-ui-v2/services"
"github.com/webtor-io/web-ui-v2/services/job"
"github.com/webtor-io/web-ui-v2/services/web/job/script"

"github.com/webtor-io/web-ui-v2/services/api"
"github.com/webtor-io/web-ui-v2/services/job"
)

type PostArgs struct {
File []byte
Query string
Claims *api.Claims
File []byte
Query string
Instruction string
Claims *api.Claims
}

func (s *Handler) bindPostArgs(c *gin.Context) (*PostArgs, error) {
file, _ := c.FormFile("resource")
instruction, _ := c.GetPostForm("instruction")
r, ok := c.GetPostFormArray("resource")
query := ""
if ok {
Expand Down Expand Up @@ -54,15 +54,17 @@ func (s *Handler) bindPostArgs(c *gin.Context) (*PostArgs, error) {
}

return &PostArgs{
File: fd,
Query: query,
Claims: api.GetClaimsFromContext(c),
File: fd,
Query: query,
Claims: api.GetClaimsFromContext(c),
Instruction: instruction,
}, nil
}

type PostData struct {
Job *job.Job
Args *PostArgs
Job *job.Job
Args *PostArgs
Instruction string
}

func (s *Handler) post(c *gin.Context) {
Expand All @@ -74,7 +76,11 @@ func (s *Handler) post(c *gin.Context) {
loadJob *job.Job
)
args, err = s.bindPostArgs(c)
if err != nil {
indexTpl.HTMLWithErr(errors.Wrap(err, "wrong args provided"), http.StatusBadRequest, c, d)
}
d.Args = args
d.Instruction = args.Instruction
if err != nil {
indexTpl.HTMLWithErr(errors.Wrap(err, "wrong args provided"), http.StatusBadRequest, c, d)
return
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 51df763

Please sign in to comment.