-
Notifications
You must be signed in to change notification settings - Fork 1
/
publishService.go
140 lines (132 loc) · 3.79 KB
/
publishService.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
package service
import (
"bytes"
"fmt"
"github.com/disintegration/imaging"
"github.com/gin-gonic/gin"
ffmpeg "github.com/u2takey/ffmpeg-go"
"github.com/zty-f/Mini-Tiktok/common"
"io"
"os"
"path/filepath"
"strings"
)
// ResourceBase 如果映射的域名和改了,需要更改这个配置
const ResourceBase = "http://47.108.239.8:8085/static/"
type PublishService struct {
}
// NewPublishServiceInstance 返回一个发布视频服务类的指针变量,可以方便调用该结构体的方法
func NewPublishServiceInstance() *PublishService {
return &PublishService{}
}
// DoPublishVideo 发布视频
func (p *PublishService) DoPublishVideo(c *gin.Context, title string, userId int64) error {
data, err := c.FormFile("data")
if err != nil {
return err
}
filename := filepath.Base(data.Filename)
finalName := fmt.Sprintf("%d_%s", userId, filename)
saveFile := filepath.Join("./public/", finalName)
if err = c.SaveUploadedFile(data, saveFile); err != nil {
return err
}
playURL, vErr := joinResourceURL(ResourceBase, finalName)
if vErr != nil {
fmt.Printf("wrong join URL")
fmt.Printf("Wrong URL is: %s", playURL)
}
// 获取视频第一帧作为封面图片
reader := ExampleReadFrameAsJpeg("./public/"+finalName, 1)
img, err1 := imaging.Decode(reader)
if err1 != nil {
return err1
}
err2 := imaging.Save(img, "./public/"+finalName+".jpeg")
if err2 != nil {
return err2
}
coverName := finalName + ".jpeg"
coverURL, cErr := joinResourceURL(ResourceBase, coverName)
if cErr != nil {
fmt.Printf("wrong join URL")
fmt.Printf("Wrong URL is: %s", coverURL)
}
fmt.Println("视频上传成功!")
fmt.Println("playUrl:" + playURL)
fmt.Println("coverURL:" + coverURL)
err3 := videoDaoInstance.CreateVideoRecord(userId, playURL, coverURL, title)
if err3 != nil {
return err3
}
return nil
}
// DoPublishList 发布视频列表
func (p *PublishService) DoPublishList(userId, loginUserId int64) ([]common.VideoVo, error) {
videos, err := videoDaoInstance.QueryByOwner(userId)
if err != nil {
return nil, err
}
user, err1 := userDaoInstance.QueryUserById(userId)
if err1 != nil {
return nil, err1
}
isFollow, err3 := relationDaoInstance.QueryIsFollowByUserIdAndToUserId(loginUserId, userId)
if err3 != nil {
return nil, err3
}
curUser := &common.UserVo{
Id: user.Id,
Name: user.Name,
FollowCount: user.FollowCount,
FollowerCount: user.FollowerCount,
IsFollow: isFollow,
}
var PublishedList = make([]common.VideoVo, len(videos))
for i, _ := range PublishedList {
var isFavorite bool
actionType, err2 := favoriteDaoInstance.QueryActionTypeByUserIdAndVideoId(loginUserId, videos[i].Id)
if err2 != nil {
return nil, err2
}
if actionType == 1 {
isFavorite = true
} else {
isFavorite = false
}
PublishedList[i] = common.VideoVo{
Id: videos[i].Id,
Author: *curUser,
PlayUrl: videos[i].PlayUrl,
CoverUrl: videos[i].CoverUrl,
FavoriteCount: videos[i].FavoriteCount,
CommentCount: videos[i].CommentCount,
IsFavorite: isFavorite,
Title: videos[i].Title,
}
}
return PublishedList, nil
}
// 拼接字符串
func joinResourceURL(baseDomain, resourse string) (string, error) {
var sb strings.Builder
_, err := fmt.Fprintf(&sb, "%s/%s", baseDomain, resourse)
if err != nil {
fmt.Printf("joinResource fail %v", err)
return "", err
}
return sb.String(), nil
}
// ExampleReadFrameAsJpeg 视频解析成流
func ExampleReadFrameAsJpeg(inFileName string, frameNum int) io.Reader {
buf := bytes.NewBuffer(nil)
err := ffmpeg.Input(inFileName).
Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", frameNum)}).
Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
WithOutput(buf, os.Stdout).
Run()
if err != nil {
panic(err)
}
return buf
}