Add apikey to streams (#2981)

This commit is contained in:
WithoutPants
2022-10-10 10:11:51 +11:00
committed by GitHub
parent 351dcb708b
commit 6c04f9199f
3 changed files with 36 additions and 15 deletions

View File

@@ -168,7 +168,7 @@ func (r *sceneResolver) Paths(ctx context.Context, obj *models.Scene) (*ScenePat
builder.APIKey = config.GetAPIKey() builder.APIKey = config.GetAPIKey()
screenshotPath := builder.GetScreenshotURL(obj.UpdatedAt) screenshotPath := builder.GetScreenshotURL(obj.UpdatedAt)
previewPath := builder.GetStreamPreviewURL() previewPath := builder.GetStreamPreviewURL()
streamPath := builder.GetStreamURL() streamPath := builder.GetStreamURL().String()
webpPath := builder.GetStreamPreviewImageURL() webpPath := builder.GetStreamPreviewImageURL()
vttPath := builder.GetSpriteVTTURL() vttPath := builder.GetSpriteVTTURL()
spritePath := builder.GetSpriteURL() spritePath := builder.GetSpriteURL()
@@ -357,6 +357,7 @@ func (r *sceneResolver) SceneStreams(ctx context.Context, obj *models.Scene) ([]
baseURL, _ := ctx.Value(BaseURLCtxKey).(string) baseURL, _ := ctx.Value(BaseURLCtxKey).(string)
builder := urlbuilders.NewSceneURLBuilder(baseURL, obj.ID) builder := urlbuilders.NewSceneURLBuilder(baseURL, obj.ID)
builder.APIKey = config.GetAPIKey()
return manager.GetSceneStreamPaths(obj, builder.GetStreamURL(), config.GetMaxStreamingTranscodeSize()) return manager.GetSceneStreamPaths(obj, builder.GetStreamURL(), config.GetMaxStreamingTranscodeSize())
} }

View File

@@ -2,6 +2,7 @@ package urlbuilders
import ( import (
"fmt" "fmt"
"net/url"
"strconv" "strconv"
"time" "time"
) )
@@ -19,12 +20,19 @@ func NewSceneURLBuilder(baseURL string, sceneID int) SceneURLBuilder {
} }
} }
func (b SceneURLBuilder) GetStreamURL() string { func (b SceneURLBuilder) GetStreamURL() *url.URL {
var apiKeyParam string u, err := url.Parse(fmt.Sprintf("%s/scene/%s/stream", b.BaseURL, b.SceneID))
if b.APIKey != "" { if err != nil {
apiKeyParam = fmt.Sprintf("?apikey=%s", b.APIKey) // shouldn't happen
panic(err)
} }
return fmt.Sprintf("%s/scene/%s/stream%s", b.BaseURL, b.SceneID, apiKeyParam)
if b.APIKey != "" {
v := u.Query()
v.Set("apikey", b.APIKey)
u.RawQuery = v.Encode()
}
return u
} }
func (b SceneURLBuilder) GetStreamPreviewURL() string { func (b SceneURLBuilder) GetStreamPreviewURL() string {

View File

@@ -2,6 +2,7 @@ package manager
import ( import (
"fmt" "fmt"
"net/url"
"github.com/stashapp/stash/internal/manager/config" "github.com/stashapp/stash/internal/manager/config"
"github.com/stashapp/stash/pkg/ffmpeg" "github.com/stashapp/stash/pkg/ffmpeg"
@@ -58,15 +59,20 @@ type SceneStreamEndpoint struct {
Label *string `json:"label"` Label *string `json:"label"`
} }
func makeStreamEndpoint(streamURL string, streamingResolution models.StreamingResolutionEnum, mimeType, label string) *SceneStreamEndpoint { func makeStreamEndpoint(streamURL *url.URL, streamingResolution models.StreamingResolutionEnum, mimeType, label string) *SceneStreamEndpoint {
urlCopy := *streamURL
v := urlCopy.Query()
v.Set("resolution", streamingResolution.String())
urlCopy.RawQuery = v.Encode()
return &SceneStreamEndpoint{ return &SceneStreamEndpoint{
URL: fmt.Sprintf("%s?resolution=%s", streamURL, streamingResolution.String()), URL: urlCopy.String(),
MimeType: &mimeType, MimeType: &mimeType,
Label: &label, Label: &label,
} }
} }
func GetSceneStreamPaths(scene *models.Scene, directStreamURL string, maxStreamingTranscodeSize models.StreamingResolutionEnum) ([]*SceneStreamEndpoint, error) { func GetSceneStreamPaths(scene *models.Scene, directStreamURL *url.URL, maxStreamingTranscodeSize models.StreamingResolutionEnum) ([]*SceneStreamEndpoint, error) {
if scene == nil { if scene == nil {
return nil, fmt.Errorf("nil scene") return nil, fmt.Errorf("nil scene")
} }
@@ -93,10 +99,16 @@ func GetSceneStreamPaths(scene *models.Scene, directStreamURL string, maxStreami
// don't care if we can't get the container // don't care if we can't get the container
container, _ := GetVideoFileContainer(pf) container, _ := GetVideoFileContainer(pf)
replaceSuffix := func(suffix string) *url.URL {
urlCopy := *directStreamURL
urlCopy.Path += suffix
return &urlCopy
}
if HasTranscode(scene, config.GetInstance().GetVideoFileNamingAlgorithm()) || ffmpeg.IsValidAudioForContainer(audioCodec, container) { if HasTranscode(scene, config.GetInstance().GetVideoFileNamingAlgorithm()) || ffmpeg.IsValidAudioForContainer(audioCodec, container) {
label := "Direct stream" label := "Direct stream"
ret = append(ret, &SceneStreamEndpoint{ ret = append(ret, &SceneStreamEndpoint{
URL: directStreamURL, URL: directStreamURL.String(),
MimeType: &mimeMp4, MimeType: &mimeMp4,
Label: &label, Label: &label,
}) })
@@ -106,7 +118,7 @@ func GetSceneStreamPaths(scene *models.Scene, directStreamURL string, maxStreami
if container == ffmpeg.Matroska { if container == ffmpeg.Matroska {
label := "mkv" label := "mkv"
ret = append(ret, &SceneStreamEndpoint{ ret = append(ret, &SceneStreamEndpoint{
URL: directStreamURL + ".mkv", URL: replaceSuffix(".mkv").String(),
// set mkv to mp4 to trick the client, since many clients won't try mkv // set mkv to mp4 to trick the client, since many clients won't try mkv
MimeType: &mimeMp4, MimeType: &mimeMp4,
Label: &label, Label: &label,
@@ -131,8 +143,8 @@ func GetSceneStreamPaths(scene *models.Scene, directStreamURL string, maxStreami
var webmStreams []*SceneStreamEndpoint var webmStreams []*SceneStreamEndpoint
var mp4Streams []*SceneStreamEndpoint var mp4Streams []*SceneStreamEndpoint
webmURL := directStreamURL + ".webm" webmURL := replaceSuffix(".webm")
mp4URL := directStreamURL + ".mp4" mp4URL := replaceSuffix(".mp4")
if includeSceneStreamPath(pf, models.StreamingResolutionEnumFourK, maxStreamingTranscodeSize) { if includeSceneStreamPath(pf, models.StreamingResolutionEnumFourK, maxStreamingTranscodeSize) {
webmStreams = append(webmStreams, makeStreamEndpoint(webmURL, models.StreamingResolutionEnumFourK, mimeMp4, webmLabelFourK)) webmStreams = append(webmStreams, makeStreamEndpoint(webmURL, models.StreamingResolutionEnumFourK, mimeMp4, webmLabelFourK))
@@ -164,7 +176,7 @@ func GetSceneStreamPaths(scene *models.Scene, directStreamURL string, maxStreami
defaultStreams := []*SceneStreamEndpoint{ defaultStreams := []*SceneStreamEndpoint{
{ {
URL: directStreamURL + ".webm", URL: replaceSuffix(".webm").String(),
MimeType: &mimeWebm, MimeType: &mimeWebm,
Label: &labelWebm, Label: &labelWebm,
}, },
@@ -173,7 +185,7 @@ func GetSceneStreamPaths(scene *models.Scene, directStreamURL string, maxStreami
ret = append(ret, defaultStreams...) ret = append(ret, defaultStreams...)
hls := SceneStreamEndpoint{ hls := SceneStreamEndpoint{
URL: directStreamURL + ".m3u8", URL: replaceSuffix(".m3u8").String(),
MimeType: &mimeHLS, MimeType: &mimeHLS,
Label: &labelHLS, Label: &labelHLS,
} }