Skip to content

Commit

Permalink
embed: add file, pwd, path params
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Aug 23, 2024
1 parent c6ec5d2 commit 22feb04
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
3 changes: 3 additions & 0 deletions services/models/embed_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ type EmbedSettings struct {
Magnet string `json:"magnet"`
TorrentURL string `json:"torrentUrl"`
Referer string `json:"referer"`
PWD string `json:"pwd"`
File string `json:"file"`
Path string `json:"path"`
}
30 changes: 17 additions & 13 deletions services/web/job/script/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type StreamContent struct {
ExternalData *m.ExternalData
}

func (s *ActionScript) streamContent(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string, template string, settings *m.StreamSettings) (err error) {
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,
ExternalData: &m.ExternalData{},
Expand Down Expand Up @@ -62,8 +62,6 @@ func (s *ActionScript) streamContent(j *job.Job, c *gin.Context, claims *api.Cla
}
}
if resp.Source.MediaFormat == ra.Video {
vsud := m.NewVideoStreamUserData(resourceID, itemID, settings)
vsud.FetchSessionData(c)
sc.VideoStreamUserData = vsud
j.InProgress("loading OpenSubtitles")
subs, err := s.api.GetOpenSubtitles(ctx, resp.ExportItems["subtitles"].URL)
Expand Down Expand Up @@ -93,16 +91,16 @@ func (s *ActionScript) streamContent(j *job.Job, c *gin.Context, claims *api.Cla
return
}

func (s *ActionScript) previewImage(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string, settings *m.StreamSettings) error {
return s.streamContent(j, c, claims, resourceID, itemID, "preview_image", settings)
func (s *ActionScript) previewImage(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string, settings *m.StreamSettings, vsud *m.VideoStreamUserData) error {
return s.streamContent(j, c, claims, resourceID, itemID, "preview_image", settings, vsud)
}

func (s *ActionScript) streamAudio(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string, settings *m.StreamSettings) error {
return s.streamContent(j, c, claims, resourceID, itemID, "stream_audio", settings)
func (s *ActionScript) streamAudio(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string, settings *m.StreamSettings, vsud *m.VideoStreamUserData) error {
return s.streamContent(j, c, claims, resourceID, itemID, "stream_audio", settings, vsud)
}

func (s *ActionScript) streamVideo(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string, settings *m.StreamSettings) error {
return s.streamContent(j, c, claims, resourceID, itemID, "stream_video", settings)
func (s *ActionScript) streamVideo(j *job.Job, c *gin.Context, claims *api.Claims, resourceID string, itemID string, settings *m.StreamSettings, vsud *m.VideoStreamUserData) error {
return s.streamContent(j, c, claims, resourceID, itemID, "stream_video", settings, vsud)
}

func (s *ActionScript) renderActionTemplate(j *job.Job, c *gin.Context, sc *StreamContent, name string) error {
Expand Down Expand Up @@ -202,24 +200,29 @@ type ActionScript struct {
action string
tb template.Builder
settings *m.StreamSettings
vsud *m.VideoStreamUserData
}

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 "preview-image":
return s.previewImage(j, s.c, s.claims, s.resourceId, s.itemId, s.settings)
return s.previewImage(j, s.c, s.claims, s.resourceId, s.itemId, s.settings, s.vsud)
case "stream-audio":
return s.streamAudio(j, s.c, s.claims, s.resourceId, s.itemId, s.settings)
return s.streamAudio(j, s.c, s.claims, s.resourceId, s.itemId, s.settings, s.vsud)
case "stream-video":
return s.streamVideo(j, s.c, s.claims, s.resourceId, s.itemId, s.settings)
return s.streamVideo(j, s.c, s.claims, s.resourceId, s.itemId, s.settings, s.vsud)
}
return
}

func Action(tb template.Builder, api *api.Api, claims *api.Claims, c *gin.Context, resourceID string, itemID string, action string, settings *m.StreamSettings) (r job.Runnable, id string) {
id = fmt.Sprintf("%x", sha1.Sum([]byte(resourceID+"/"+itemID+"/"+action+"/"+claims.Role+"/"+fmt.Sprintf("%+v", settings))))
vsud := m.NewVideoStreamUserData(resourceID, itemID, settings)
vsud.FetchSessionData(c)
vsudID := vsud.AudioID + "/" + vsud.SubtitleID + "/" + fmt.Sprintf("%+v", vsud.AcceptLangTags)
settingsID := fmt.Sprintf("%+v", settings)
id = fmt.Sprintf("%x", sha1.Sum([]byte(resourceID+"/"+itemID+"/"+action+"/"+claims.Role+"/"+settingsID+"/"+vsudID)))
return &ActionScript{
tb: tb,
api: api,
Expand All @@ -229,5 +232,6 @@ func Action(tb template.Builder, api *api.Api, claims *api.Claims, c *gin.Contex
itemId: itemID,
action: action,
settings: settings,
vsud: vsud,
}, id
}
25 changes: 20 additions & 5 deletions services/web/job/script/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"regexp"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -77,7 +78,7 @@ func (s *EmbedScript) Run(j *job.Job) (err error) {
return err
}
id := j.Context.Value("respID").(string)
i, err := s.getBestItem(j, id)
i, err := s.getBestItem(j, id, s.settings)
if err != nil {
return err
}
Expand All @@ -95,19 +96,33 @@ func (s *EmbedScript) Run(j *job.Job) (err error) {
return
}

func (s *EmbedScript) getBestItem(j *job.Job, id string) (i *ra.ListItem, err error) {
func (s *EmbedScript) getBestItem(j *job.Job, id string, settings *models.EmbedSettings) (i *ra.ListItem, err error) {
j.InProgress("searching for stream content")
ctx, cancel := context.WithTimeout(j.Context, 10*time.Second)
defer cancel()
pwd := settings.PWD
file := settings.File
if settings.Path != "" {
parts := strings.Split(settings.Path, "/")
file = parts[len(parts)-1]
pwd = strings.Join(parts[:len(parts)-1], "/")
}
l, err := s.api.ListResourceContent(ctx, s.claims, id, &api.ListResourceContentArgs{
Path: pwd,
Output: api.OutputTree,
})
if err != nil {
return nil, j.Error(err, "failed to list resource content")
}
i = s.findBestItem(l)
if err != nil {
return nil, j.Error(err, "failed to find stream content")
if file != "" {
for _, f := range l.Items {
if f.Name == file {
i = &f
break
}
}
} else {
i = s.findBestItem(l)
}
if i == nil {
return nil, j.Error(err, "failed to find stream content")
Expand Down
3 changes: 3 additions & 0 deletions templates/views/embed/example/path.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ define "main" }}
<video src="{{ demoMagnet }}" controls data-config='{"baseUrl": "{{ domain }}", "path": "/Sintel.mp4"}'></video>
{{ end}}
3 changes: 3 additions & 0 deletions templates/views/embed/example/pwd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ define "main" }}
<video src="{{ demoMagnet }}" controls data-config='{"baseUrl": "{{ domain }}", "pwd": "/"}'></video>
{{ end}}
3 changes: 3 additions & 0 deletions templates/views/embed/example/pwd_with_file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ define "main" }}
<video src="{{ demoMagnet }}" controls data-config='{"baseUrl": "{{ domain }}", "pwd": "/", "file": "Sintel.mp4"}'></video>
{{ end}}

0 comments on commit 22feb04

Please sign in to comment.