-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
147 lines (134 loc) · 4.43 KB
/
handler.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
package main
import (
"context"
"fmt"
"github.com/benxinm/tiktok/cmd/video/pack"
"github.com/benxinm/tiktok/cmd/video/service"
video "github.com/benxinm/tiktok/kitex_gen/video"
"github.com/benxinm/tiktok/pkg/myerrors"
"github.com/benxinm/tiktok/pkg/utils"
"github.com/cloudwego/kitex/pkg/klog"
"golang.org/x/sync/errgroup"
)
// VideoServiceImpl implements the last service interface defined in the IDL.
type VideoServiceImpl struct{}
// Feed implements the VideoServiceImpl interface.
func (s *VideoServiceImpl) Feed(ctx context.Context, req *video.FeedRequest) (resp *video.FeedResponse, err error) {
resp = new(video.FeedResponse)
if _, err := utils.VerifyToken(req.Token); err != nil {
resp.Base = pack.MakeBaseResp(myerrors.AuthFailedError)
return resp, err
}
return
}
// PutVideo implements the VideoServiceImpl interface.
func (s *VideoServiceImpl) PutVideo(ctx context.Context, req *video.PutVideoRequest) (resp *video.PutVideoResponse, err error) {
resp = new(video.PutVideoResponse)
claim, err := utils.VerifyToken(req.Token)
if err != nil {
return nil, myerrors.AuthFailedError
}
videoName := pack.GenVideoName(claim.UserId)
coverName := pack.GenCoverName(claim.UserId)
var eg errgroup.Group
eg.Go(func() error {
err = service.NewVideoService(ctx).UploadVideo(req, videoName)
if err != nil {
klog.Error(err)
return err
}
return nil
})
eg.Go(func() error {
err = service.NewVideoService(ctx).UploadCover(req, coverName)
if err != nil {
klog.Error(err)
return err
}
return nil
})
eg.Go(func() error { //TODO endpoint should get from config file
playUrl := fmt.Sprintf("https://%s/%s/%s", "endpoint", "direction", videoName)
coverUrl := fmt.Sprintf("https://%s/%s/%s", "endpoint", "direction", coverName)
_, err = service.NewVideoService(ctx).Create(req, claim.UserId, playUrl, coverUrl)
if err != nil {
klog.Error(err)
return err
}
return nil
})
if err := eg.Wait(); err != nil {
resp.Base = pack.MakeBaseResp(err)
return resp, err
}
resp.Base = pack.MakeBaseResp(nil)
return resp, nil
}
// GetFavoriteVideoInfo implements the VideoServiceImpl interface.
func (s *VideoServiceImpl) GetFavoriteVideoInfo(ctx context.Context, req *video.GetFavoriteVideoInfoRequest) (resp *video.GetFavoriteVideoInfoResponse, err error) {
resp = new(video.GetFavoriteVideoInfoResponse)
if len(req.VideoId) == 0 {
resp.Base = pack.MakeBaseResp(myerrors.ParamError)
return resp, nil
}
if _, err := utils.VerifyToken(req.Token); err != nil {
resp.Base = pack.MakeBaseResp(myerrors.AuthFailedError)
return resp, nil
}
videoList, err := service.NewVideoService(ctx).GetFavouriteVideo(req)
if err != nil {
klog.Error(err)
resp.Base = pack.MakeBaseResp(err)
return resp, nil
}
resp.Base = pack.MakeBaseResp(nil)
resp.VideoList = videoList
return
}
// GetPublishList implements the VideoServiceImpl interface.
func (s *VideoServiceImpl) GetPublishList(ctx context.Context, req *video.GetPublishListRequest) (resp *video.GetPublishListResponse, err error) {
resp = new(video.GetPublishListResponse)
if _, err := utils.VerifyToken(req.Token); err != nil {
resp.Base = pack.MakeBaseResp(myerrors.AuthFailedError)
return resp, nil
}
if req.UserId < 10000 {
resp.Base = pack.MakeBaseResp(myerrors.ParamError)
return resp, nil
}
videoList, err := service.NewVideoService(ctx).GetPublishVideoInfo(req)
if err != nil {
klog.Error(err)
resp.Base = pack.MakeBaseResp(err)
return resp, nil
}
resp.Base = pack.MakeBaseResp(nil)
resp.ListVideo = videoList
return
}
// GetWorkCount implements the VideoServiceImpl interface.
func (s *VideoServiceImpl) GetWorkCount(ctx context.Context, req *video.GetWorkCountRequest) (resp *video.GetWorkCountResponse, err error) {
resp = new(video.GetWorkCountResponse)
if _, err := utils.VerifyToken(req.Token); err != nil {
resp.Base = pack.MakeBaseResp(myerrors.AuthFailedError)
return resp, nil
}
if req.UserId < 10000 {
resp.Base = pack.MakeBaseResp(myerrors.ParamError)
return resp, nil
}
count, err := service.NewVideoService(ctx).WorkCount(req)
if err != nil {
klog.Error(err)
resp.Base = pack.MakeBaseResp(err)
return resp, nil
}
resp.Base = pack.MakeBaseResp(nil)
resp.WorkCount = count
return
}
// GetVideoIDByUid implements the VideoServiceImpl interface.
func (s *VideoServiceImpl) GetVideoIDByUid(ctx context.Context, req *video.GetVideoIDByUidRequest) (resp *video.GetVideoIDByUidResponse, err error) {
// TODO: Your code here...
return
}