Fix streaming scenes not able to be deleted (#2549)

* Don't navigate away from scene if delete failed
* Close connection on cancel
This commit is contained in:
WithoutPants
2022-05-04 09:27:22 +10:00
committed by GitHub
parent 0c2dc17e8e
commit e87fd516d6
4 changed files with 46 additions and 3 deletions

View File

@@ -164,7 +164,8 @@ func (rs sceneRoutes) streamTranscode(w http.ResponseWriter, r *http.Request, st
encoder := manager.GetInstance().FFMPEG
lm := manager.GetInstance().ReadLockManager
lockCtx := lm.ReadLock(r.Context(), scene.Path)
streamRequestCtx := manager.NewStreamRequestContext(w, r)
lockCtx := lm.ReadLock(streamRequestCtx, scene.Path)
defer lockCtx.Cancel()
stream, err := encoder.GetTranscodeStream(lockCtx, options)

View File

@@ -1,6 +1,7 @@
package manager
import (
"context"
"net/http"
"github.com/stashapp/stash/internal/manager/config"
@@ -10,6 +11,31 @@ import (
"github.com/stashapp/stash/pkg/utils"
)
type StreamRequestContext struct {
context.Context
ResponseWriter http.ResponseWriter
}
func NewStreamRequestContext(w http.ResponseWriter, r *http.Request) *StreamRequestContext {
return &StreamRequestContext{
Context: r.Context(),
ResponseWriter: w,
}
}
func (c *StreamRequestContext) Cancel() {
hj, ok := (c.ResponseWriter).(http.Hijacker)
if !ok {
return
}
// hijack and close the connection
conn, _, _ := hj.Hijack()
if conn != nil {
conn.Close()
}
}
func KillRunningStreams(scene *models.Scene, fileNamingAlgo models.HashAlgorithm) {
instance.ReadLockManager.Cancel(scene.Path)
@@ -31,7 +57,8 @@ func (s *SceneServer) StreamSceneDirect(scene *models.Scene, w http.ResponseWrit
fileNamingAlgo := config.GetInstance().GetVideoFileNamingAlgorithm()
filepath := GetInstance().Paths.Scene.GetStreamPath(scene.Path, scene.GetHash(fileNamingAlgo))
lockCtx := GetInstance().ReadLockManager.ReadLock(r.Context(), filepath)
streamRequestCtx := NewStreamRequestContext(w, r)
lockCtx := GetInstance().ReadLockManager.ReadLock(streamRequestCtx, filepath)
defer lockCtx.Cancel()
http.ServeFile(w, r, filepath)
}