/
videoService.go
293 lines (282 loc) · 10.6 KB
/
videoService.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package handler
import (
"context"
"io"
"net/http"
"strconv"
"time"
pb "github.com/Coreychen4444/shortvideo"
"github.com/gin-gonic/gin"
)
type GetVideoFlowResponse struct {
NextTime *int64 `json:"next_time"` // 本次返回的视频中,发布最早的时间,作为下次请求时的latest_time
StatusCode int64 `json:"status_code"` // 状态码,0-成功,其他值-失败
StatusMsg *string `json:"status_msg"` // 返回状态描述
VideoList []*pb.Video `json:"video_list"` // 视频列表
}
// 处理获取用户视频列表请求
func (h *ServiceHandler) GetUserVideoList(c *gin.Context) {
userID := c.Query("user_id")
id, err := strconv.ParseInt(userID, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": "用户id格式错误", "video_list": nil})
return
}
resp, err := h.vc.GetUserVideoList(context.Background(), &pb.UserVideoListRequest{Id: id})
if err != nil {
respCode := http.StatusBadRequest
if err.Error() == "token无效,请重新登录" {
respCode = http.StatusUnauthorized
} else if err.Error() == "获取视频失败" {
respCode = http.StatusInternalServerError
} else if err.Error() == "该用户不存在" {
respCode = http.StatusNotFound
}
c.JSON(respCode, gin.H{"status_code": 1, "status_msg": err.Error(), "video_list": nil})
return
}
if len(resp.VideoList) == 0 {
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "该用户没有发布任何视频", "video_list": resp.VideoList})
return
}
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "获取用户视频列表成功", "video_list": resp.VideoList})
}
func (h *ServiceHandler) GetVideoFlow(c *gin.Context) {
latest_time := c.Query("latest_time")
var latestTime int64
if latest_time == "" {
latestTime = time.Now().Unix()
} else {
latest_Time, err := strconv.ParseInt(latest_time, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": "请求参数错误", "video_list": nil})
return
}
latestTime = latest_Time
}
token := c.Query("token")
http_resp := &GetVideoFlowResponse{
StatusCode: 1,
StatusMsg: nil,
VideoList: nil,
NextTime: nil,
}
resp, err := h.vc.GetVideoFlow(context.Background(), &pb.VideoFlowRequest{LatestTime: latestTime, Token: token})
if err != nil {
respCode := http.StatusBadRequest
errMsg := err.Error()
if errMsg == "获取视频失败" {
respCode = http.StatusInternalServerError
}
http_resp.StatusMsg = &errMsg
c.JSON(respCode, resp)
return
}
http_resp.StatusCode = 0
if len(resp.VideoList) == 0 {
resMsg := "没有更多视频了"
http_resp.StatusMsg = &resMsg
http_resp.VideoList = resp.VideoList
http_resp.NextTime = nil
c.JSON(http.StatusOK, resp)
return
}
http_resp.VideoList = resp.VideoList
http_resp.NextTime = &resp.NextTime
http_resp.StatusMsg = nil
c.JSON(http.StatusOK, resp)
}
// 发布视频
func (h *ServiceHandler) PublishVideo(c *gin.Context) {
fileHeader, err := c.FormFile("data")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": err.Error()})
return
}
//multipart变字节流
file, err := fileHeader.Open()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": err.Error()})
return
}
defer file.Close()
fileBytes, err := io.ReadAll(file)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status_code": 1, "status_msg": "Unable to read file."})
return
}
token, ok := c.Get("userId")
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"status_code": 1, "status_msg": "token无效,请重新登录", "video_list": nil})
return
}
tokenId, ok := token.(float64)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"status_code": 1, "status_msg": "token无效,请重新登录", "video_list": nil})
return
}
title := c.PostForm("title")
_, err = h.vc.PublishVideo(context.Background(), &pb.PublishVideoRequest{TokenUserId: int64(tokenId), Title: title, Content: fileBytes})
if err != nil {
respCode := http.StatusBadRequest
if err.Error() == "token无效,请重新登录" {
respCode = http.StatusUnauthorized
} else if err.Error() == "创建视频失败" || err.Error() == "保存文件失败" || err.Error() == "生成封面失败" ||
err.Error() == "发布视频失败" || err.Error() == "创建存储客户端失败" || err.Error() == "打开封面文件失败" {
respCode = http.StatusInternalServerError
}
c.JSON(respCode, gin.H{"status_code": 1, "status_msg": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "发布视频成功"})
}
// 点赞视频和取消点赞
func (h *ServiceHandler) LikeVideo(c *gin.Context) {
token, ok := c.Get("userId")
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"status_code": 1, "status_msg": "token无效,请重新登录", "video_list": nil})
return
}
tokenId, ok := token.(float64)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"status_code": 1, "status_msg": "token无效,请重新登录", "video_list": nil})
return
}
video_id := c.Query("video_id")
videoId, err := strconv.ParseInt(video_id, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": "视频id格式错误"})
return
}
action_type := c.Query("action_type")
_, err = h.vc.LikeVideo(context.Background(), &pb.LikeVideoRequest{TokenUserId: int64(tokenId), Id: videoId, ActionType: action_type})
if err != nil {
respCode := http.StatusBadRequest
if err.Error() == "token无效, 请重新登录" {
respCode = http.StatusUnauthorized
} else if err.Error() == "视频出错" || err.Error() == "请求参数错误" {
respCode = http.StatusBadRequest
} else if err.Error() == "点赞视频失败" || err.Error() == "取消点赞失败" {
respCode = http.StatusInternalServerError
}
c.JSON(respCode, gin.H{"status_code": 1, "status_msg": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "操作成功"})
}
// 获取用户点赞的视频列表
func (h *ServiceHandler) GetUserLike(c *gin.Context) {
user_id := c.Query("user_id")
userId, err := strconv.ParseInt(user_id, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": "用户id格式错误", "video_list": nil})
return
}
resp, err := h.vc.GetUserLike(context.Background(), &pb.UserLikeRequest{Id: userId})
if err != nil {
respCode := http.StatusBadRequest
if err.Error() == "token无效, 请重新登录" {
respCode = http.StatusUnauthorized
} else if err.Error() == "用户id格式错误" {
respCode = http.StatusBadRequest
} else if err.Error() == "获取列表失败" {
respCode = http.StatusInternalServerError
}
c.JSON(respCode, gin.H{"status_code": 1, "status_msg": err.Error(), "video_list": nil})
return
}
if len(resp.VideoList) == 0 {
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "该用户没有点赞的视频", "video_list": resp.VideoList})
return
}
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "获取列表成功", "video_list": resp.VideoList})
}
// 评论视频和删除评论
func (h *ServiceHandler) CommentVideo(c *gin.Context) {
token, ok := c.Get("userId")
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"status_code": 1, "status_msg": "token无效,请重新登录", "video_list": nil})
return
}
tokenId, ok := token.(float64)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"status_code": 1, "status_msg": "token无效,请重新登录", "video_list": nil})
return
}
video_id := c.Query("video_id")
videoId, err := strconv.ParseInt(video_id, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": "视频id格式错误"})
return
}
action_type := c.Query("action_type")
if action_type == "1" {
comment_text := c.Query("comment_text")
resp, err := h.vc.CommentVideo(context.Background(), &pb.CommentVideoRequest{TokenUserId: int64(tokenId), Id: videoId, Content: comment_text})
if err != nil {
respCode := http.StatusBadRequest
if err.Error() == "token无效, 请重新登录" {
respCode = http.StatusUnauthorized
} else if err.Error() == "视频出错" || err.Error() == "请求参数错误" {
respCode = http.StatusBadRequest
} else if err.Error() == "评论视频失败" {
respCode = http.StatusInternalServerError
} else if err.Error() == "评论发表成功, 但返回评论信息失败" {
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": err.Error(), "comment": nil})
return
}
c.JSON(respCode, gin.H{"status_code": 1, "status_msg": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "评论成功", "comment": resp.Comment})
}
if action_type == "2" {
comment_id := c.Query("comment_id")
commentId, err := strconv.ParseInt(comment_id, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": "评论id格式错误"})
return
}
_, err = h.vc.DeleteComment(context.Background(), &pb.CommentVideoRequest{Id: commentId})
if err != nil {
respCode := http.StatusBadRequest
if err.Error() == "token无效, 请重新登录" {
respCode = http.StatusUnauthorized
} else if err.Error() == "视频出错" || err.Error() == "请求参数错误" {
respCode = http.StatusBadRequest
} else if err.Error() == "删除评论失败" {
respCode = http.StatusInternalServerError
}
c.JSON(respCode, gin.H{"status_code": 1, "status_msg": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "删除评论成功"})
}
}
// 获取视频评论列表
func (h *ServiceHandler) GetVideoComment(c *gin.Context) {
video_id := c.Query("video_id")
videoId, err := strconv.ParseInt(video_id, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status_code": 1, "status_msg": "视频id格式错误", "comment_list": nil})
return
}
resp, err := h.vc.GetVideoComment(context.Background(), &pb.VideoCommentRequest{Id: videoId})
if err != nil {
respCode := http.StatusBadRequest
if err.Error() == "token无效, 请重新登录" {
respCode = http.StatusUnauthorized
} else if err.Error() == "视频出错" {
respCode = http.StatusBadRequest
} else if err.Error() == "获取评论列表失败" {
respCode = http.StatusInternalServerError
}
c.JSON(respCode, gin.H{"status_code": 1, "status_msg": err.Error(), "comment_list": nil})
return
}
if len(resp.CommentList) == 0 {
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "该视频没有评论", "comment_list": resp.CommentList})
return
}
c.JSON(http.StatusOK, gin.H{"status_code": 0, "status_msg": "获取视频评论成功", "comment_list": resp.CommentList})
}