mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Close streams/encodes before deleting file
This commit is contained in:
58
pkg/manager/running_streams.go
Normal file
58
pkg/manager/running_streams.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package manager
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/stashapp/stash/pkg/ffmpeg"
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
)
|
||||
|
||||
var streamingFiles = make(map[string][]*http.ResponseWriter)
|
||||
|
||||
func RegisterStream(filepath string, w *http.ResponseWriter) {
|
||||
streams := streamingFiles[filepath]
|
||||
|
||||
streamingFiles[filepath] = append(streams, w)
|
||||
}
|
||||
|
||||
func deregisterStream(filepath string, w *http.ResponseWriter) {
|
||||
streams := streamingFiles[filepath]
|
||||
|
||||
for i, v := range streams {
|
||||
if v == w {
|
||||
streamingFiles[filepath] = append(streams[:i], streams[i+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WaitAndDeregisterStream(filepath string, w *http.ResponseWriter, r *http.Request) {
|
||||
notify := r.Context().Done()
|
||||
go func() {
|
||||
<-notify
|
||||
deregisterStream(filepath, w)
|
||||
}()
|
||||
}
|
||||
|
||||
func KillRunningStreams(path string) {
|
||||
ffmpeg.KillRunningEncoders(path)
|
||||
|
||||
streams := streamingFiles[path]
|
||||
|
||||
for _, w := range streams {
|
||||
hj, ok := (*w).(http.Hijacker)
|
||||
if !ok {
|
||||
// if we can't close the connection can't really do anything else
|
||||
logger.Warnf("cannot close running stream for: %s", path)
|
||||
return
|
||||
}
|
||||
|
||||
// hijack and close the connection
|
||||
conn, _, err := hj.Hijack()
|
||||
if err != nil {
|
||||
logger.Errorf("cannot close running stream for '%s' due to error: %s", path, err.Error())
|
||||
} else {
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user