-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop.go
More file actions
55 lines (51 loc) · 1.61 KB
/
stop.go
File metadata and controls
55 lines (51 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"net/http"
"time"
"github.com/jqtmviyu/iinaServer/internal/httpapi"
"github.com/jqtmviyu/iinaServer/internal/model"
"github.com/jqtmviyu/iinaServer/internal/player"
"github.com/jqtmviyu/iinaServer/internal/review"
"github.com/jqtmviyu/iinaServer/internal/session"
)
func newStopHandler(store *review.Store, sessions *session.Manager) httpapi.StopFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
httpapi.WriteError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
current := sessions.Current()
if current == nil {
httpapi.WriteError(w, http.StatusNotFound, "no active session")
return
}
inst := sessions.CurrentPlayer()
if inst != nil {
stopCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
client := player.NewIPCClient(inst.SocketPath, 2*time.Second)
if err := client.WaitUntilReady(stopCtx); err != nil {
sessions.ClearPlayer()
} else if err := client.StopPlayback(); err != nil {
sessions.ClearPlayer()
store.AddError(current.SessionID, err.Error())
httpapi.WriteError(w, http.StatusInternalServerError, "无法停止播放器")
return
} else {
sessions.ClearPlayer()
}
}
sessions.StopCurrent()
store.SetStatus(current.SessionID, "stopped")
httpapi.WriteReview(w, mustReview(store, current.SessionID))
}
}
func mustReview(store *review.Store, sessionID string) *model.SessionReview {
if store != nil {
if review, ok := store.Get(sessionID); ok {
return review
}
}
return &model.SessionReview{SessionID: sessionID}
}