videos.go
4523 bytes
1package ytrssil
2
3import (
4 "errors"
5 "net/http"
6 "strconv"
7
8 "github.com/gin-gonic/gin"
9
10 "github.com/TheEdgeOfRage/ytrssil-api/handler"
11)
12
13func (srv *server) GetNewVideosJSON(c *gin.Context) {
14 videos, err := srv.handler.GetNewVideos(c.Request.Context(), true)
15 if err != nil {
16 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
17 return
18 }
19
20 c.JSON(http.StatusOK, gin.H{"videos": videos})
21}
22
23func (srv *server) GetWatchedVideosJSON(c *gin.Context) {
24 page := 1
25 if pageParam := c.Query("page"); pageParam != "" {
26 if parsedPage, err := strconv.Atoi(pageParam); err == nil && parsedPage > 0 {
27 page = parsedPage
28 }
29 }
30
31 videos, err := srv.handler.GetWatchedVideos(c.Request.Context(), true, page)
32 if err != nil {
33 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
34 return
35 }
36
37 c.JSON(http.StatusOK, gin.H{"videos": videos})
38}
39
40func (srv *server) FetchVideosJSON(c *gin.Context) {
41 err := srv.handler.FetchVideos(c.Request.Context())
42 if err != nil {
43 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
44 return
45 }
46
47 c.JSON(http.StatusOK, gin.H{"msg": "videos fetched successfully"})
48}
49
50func (srv *server) GetVideoJSON(c *gin.Context) {
51 video, err := srv.handler.GetVideo(c.Request.Context(), c.Param("video_id"))
52 if err != nil {
53 if errors.Is(err, handler.ErrVideoNotFound) {
54 c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
55 return
56 }
57 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
58 return
59 }
60
61 c.JSON(http.StatusOK, gin.H{"video": video})
62}
63
64func (srv *server) AddCustomVideoJSON(c *gin.Context) {
65 var request struct {
66 VideoID string `json:"video_id"`
67 }
68 if err := c.ShouldBindJSON(&request); err != nil {
69 c.JSON(http.StatusBadRequest, gin.H{"error": "invalid video request"})
70 return
71 }
72
73 err := srv.handler.AddCustomVideo(c.Request.Context(), request.VideoID)
74 if err != nil {
75 if errors.Is(err, handler.ErrInvalidVideoInput) {
76 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
77 return
78 }
79 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
80 return
81 }
82
83 c.JSON(http.StatusCreated, gin.H{"msg": "video added"})
84}
85
86func (srv *server) MarkVideoAsWatchedJSON(c *gin.Context) {
87 err := srv.handler.MarkVideoAsWatched(c.Request.Context(), c.Param("video_id"))
88 if err != nil {
89 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
90 return
91 }
92
93 c.JSON(http.StatusOK, gin.H{"msg": "marked video as watched"})
94}
95
96func (srv *server) MarkVideoAsUnwatchedJSON(c *gin.Context) {
97 err := srv.handler.MarkVideoAsUnwatched(c.Request.Context(), c.Param("video_id"))
98 if err != nil {
99 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
100 return
101 }
102
103 c.JSON(http.StatusOK, gin.H{"msg": "cleared video from watch history"})
104}
105
106func (srv *server) SetVideoProgressJSON(c *gin.Context) {
107 var request struct {
108 Progress string `json:"progress"`
109 }
110 if err := c.ShouldBindJSON(&request); err != nil {
111 c.JSON(http.StatusBadRequest, gin.H{"error": "invalid progress request"})
112 return
113 }
114 if request.Progress == "" {
115 c.JSON(http.StatusBadRequest, gin.H{"error": "missing progress"})
116 return
117 }
118
119 video, err := srv.handler.SetVideoProgress(c.Request.Context(), c.Param("video_id"), request.Progress)
120 if err != nil {
121 if errors.Is(err, handler.ErrInvalidProgress) {
122 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
123 return
124 }
125 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
126 return
127 }
128
129 c.JSON(http.StatusOK, gin.H{"video": video})
130}
131
132func (srv *server) DownloadVideoJSON(c *gin.Context) {
133 var request struct {
134 Format int `json:"format"`
135 }
136 if err := c.ShouldBindJSON(&request); err != nil {
137 c.JSON(http.StatusBadRequest, gin.H{"error": "invalid download request"})
138 return
139 }
140
141 if request.Format <= 0 {
142 c.JSON(http.StatusBadRequest, gin.H{"error": "invalid download format"})
143 return
144 }
145
146 err := srv.handler.DownloadVideo(c.Request.Context(), c.Param("video_id"), strconv.Itoa(request.Format))
147 if err != nil {
148 c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
149 return
150 }
151
152 c.JSON(http.StatusAccepted, gin.H{"msg": "download started"})
153}
154
155func (srv *server) ServeVideoFileJSON(c *gin.Context) {
156 filePath, filename, err := srv.handler.ServeVideoFile(c.Request.Context(), c.Param("video_id"))
157 if err != nil {
158 c.JSON(http.StatusNotFound, gin.H{"error": "video file not found"})
159 return
160 }
161
162 c.Header("Content-Disposition", "attachment; filename=\""+filename+"\"")
163 c.File(filePath)
164}