-
Notifications
You must be signed in to change notification settings - Fork 0
/
details.go
88 lines (74 loc) · 2.53 KB
/
details.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package handlers
import (
"context"
"net/http"
"github.com/gorilla/mux"
"github.com/lmika/broadtail/middleware/errhandler"
"github.com/lmika/broadtail/models"
"github.com/lmika/broadtail/services/favourites"
"github.com/lmika/broadtail/services/videomanager"
"github.com/lmika/broadtail/services/videosources"
"github.com/lmika/gopkgs/http/middleware/render"
"github.com/pkg/errors"
)
type detailsHandler struct {
videoSources *videosources.Service
videoManager *videomanager.VideoManager
favouriteService *favourites.Service
}
func (dh *detailsHandler) QuickLook() http.Handler {
return errhandler.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
videoID := r.FormValue("video_id")
if videoID == "" {
return errhandler.Errorf(http.StatusBadRequest, "missing video ID")
}
http.Redirect(w, r, "/details/video/"+videoID, http.StatusSeeOther)
return nil
})
}
func (dh *detailsHandler) VideoDetails() http.Handler {
return errhandler.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
videoID, ok := mux.Vars(r)["video_id"]
if !ok {
return errhandler.Errorf(http.StatusBadRequest, "invalid video ID: %v", videoID)
}
videoRef, err := models.ParseVideoRef(videoID)
if err != nil {
return errhandler.Wrap(err, http.StatusBadRequest)
}
videoSource, err := dh.videoSources.SourceProvider(videoRef)
if err != nil {
return errhandler.Wrap(err, http.StatusInternalServerError)
}
video, err := videoSource.GetVideoMetadata(ctx, videoRef)
if err != nil {
return errhandler.Wrap(err, http.StatusInternalServerError)
}
videoURL := videoSource.GetVideoURL(videoRef)
var downloadStatusStr string
downloadStatus, err := dh.videoManager.DownloadStatus(videoRef)
if err != nil {
downloadStatusStr = "error: " + err.Error()
} else {
downloadStatusStr = downloadStatus.String()
}
favouriteStatus, err := dh.favouriteService.VideoFavourited(ctx, videoRef)
if err != nil {
return errors.Wrap(err, "cannot get favourite status")
}
if favouriteStatus != nil {
render.Set(r, "favouriteID", favouriteStatus.ID)
} else {
render.Set(r, "favouriteID", "")
}
render.Set(r, "video", video)
render.Set(r, "videoURL", videoURL)
render.Set(r, "downloadStatus", downloadStatusStr)
render.Set(r, "favouriteStatus", favouriteStatus)
if fromFeedID := r.FormValue("from_feed_id"); fromFeedID != "" {
render.Set(r, "fromFeedID", fromFeedID)
}
render.HTML(r, w, http.StatusOK, "details/show.html")
return nil
})
}