Commit 2dc2e306 by Li Feifei

优化代码

parent 316f35e7
Pipeline #15649 passed with stages
in 39 seconds
......@@ -10,18 +10,17 @@ import (
var File = &fileService{
done: make(chan error),
ok: make(chan g.Map),
ok: make(chan bool),
}
type fileService struct {
done chan error
ok chan g.Map
ok chan bool
}
// 上传课件
func (f *fileService) Upload(r *model.FileUploadRequest) {
// 解析上传课件URL
parserURLObject, err := file.Upload(r, f.done, f.ok)
if err != nil {
g.Log().Async().Errorf("Parser Url err: %s\n", err)
......@@ -29,6 +28,7 @@ func (f *fileService) Upload(r *model.FileUploadRequest) {
}
// 检查房间是否存在
var room model.XyuRoom
if err = dao.XyuRoom.Where(dao.XyuRoom.Columns.RoomNum, r.RoomNum).Scan(&room); err != nil {
g.Log().Async().Error(err)
go func() { f.done <- err }()
......@@ -37,13 +37,12 @@ func (f *fileService) Upload(r *model.FileUploadRequest) {
go parserURLObject.Worker(err)
// 回调通知
n := file.NewNotify()
select {
case <-f.done:
// 上传失败
n.Fail(parserURLObject)
parserURLObject.Fail()
return
case res := <-f.ok:
case <-f.ok:
// 上传成功
count, err := dao.XyuRoomFile.Where(dao.XyuRoomFile.Columns.RoomId, room.Id).
Where(dao.XyuRoomFile.Columns.Name, r.Name).Count()
......@@ -55,8 +54,7 @@ func (f *fileService) Upload(r *model.FileUploadRequest) {
if count == 1 {
return
}
res["room_id"] = room.Id
n.Success(res)
parserURLObject.Success(room.Id)
return
}
}
......@@ -13,7 +13,6 @@ func Get(url string) ([]byte, error) {
return handle(g.Client().Timeout(30 * time.Second).Get(url))
}
func handle(response *ghttp.ClientResponse, err error) ([]byte, error) {
if err != nil {
return nil, err
......@@ -33,7 +32,6 @@ func Download(file string, path string) error {
}
defer resp.Body.Close()
// 创建一个文件用于保存
out, err := os.Create(path)
if err != nil {
......
......@@ -29,7 +29,7 @@ func NewSessionHttpClient() *SessionHttpClient {
}
func (client *SessionHttpClient) Post(link string, data ...interface{}) ([]byte, error) {
return handle(client.c.Timeout(10 * time.Second).Post(link, data))
return handle(client.c.Timeout(10*time.Second).Post(link, data))
}
func (client *SessionHttpClient) Get(url string) ([]byte, error) {
......
......@@ -12,15 +12,15 @@ import (
"github.com/gogf/gf/text/gregex"
)
type Notify struct {
type notify struct {
name string
password string
url string
client *http.SessionHttpClient
}
func NewNotify() *Notify {
return &Notify{
func newNotify() *notify {
return &notify{
url: g.Cfg().GetString("notify.url"),
name: os.Getenv("live_name"),
password: os.Getenv("live_pwd"),
......@@ -29,7 +29,7 @@ func NewNotify() *Notify {
}
//回调验权
func (n *Notify) verify(url string, data g.Map) error {
func (n *notify) verify(url string, data g.Map) error {
res, err := n.client.Post(url, data)
if err != nil {
......@@ -48,7 +48,7 @@ func (n *Notify) verify(url string, data g.Map) error {
}
// 登录
func (n *Notify) auth(data g.Map) error {
func (n *notify) auth(data g.Map) error {
if err := n.verify(n.url+"/web/login", g.Map{
"name": n.name,
......@@ -69,7 +69,7 @@ func (n *Notify) auth(data g.Map) error {
}
// 发送通知
func (n *Notify) notify(path string, data g.Map) error {
func (n *notify) notice(path string, data g.Map) error {
var err error
if err = n.auth(data); err != nil {
return err
......@@ -80,22 +80,3 @@ func (n *Notify) notify(path string, data g.Map) error {
g.Log().Async().Infof("Notify Success!")
return nil
}
// 上传课件失败通知
func (n *Notify) Fail(object *ObtainFile) {
if err := n.notify("/web/room_files_error", g.Map{
"code": 400,
"uuid": object.r.Uuid,
"room_num": object.r.RoomNum,
"path": object.parser.dataPath,
}); err != nil {
g.Log().Async().Error(err)
}
}
// 上传成功通知
func (n *Notify) Success(res g.Map) {
if err := n.notify("/web/room_files_add", res); err != nil {
g.Log().Async().Error(err)
}
}
......@@ -28,11 +28,12 @@ import (
// 解析URL返回基本信息
type ObtainFile struct {
r *model.FileUploadRequest // 客户端请求参数
ok chan g.Map // 上传课件响应成功
ok chan bool // 上传课件响应成功
err chan error // 上传课件失败Error
fileSize int64 // 课件大小
pageCount int // 课件图片数量
parser *parser // 课件对象
n *notify
packed *model.Packed // packed.json
}
......@@ -52,7 +53,7 @@ var (
)
func Upload(r *model.FileUploadRequest, errChan chan error,
ok chan g.Map) (file *ObtainFile, err error) {
ok chan bool) (file *ObtainFile, err error) {
f, err := parseURL(r.Url, r.Name)
if err != nil {
......@@ -70,6 +71,7 @@ func Upload(r *model.FileUploadRequest, errChan chan error,
ImagesName: gset.NewSet(),
OssImagesPath: f.splitUploadPath() + "images/",
},
n: newNotify(),
}
return file, nil
}
......@@ -174,10 +176,7 @@ func (f *ObtainFile) uploadPicture() error {
// 上传packed.json
func (f *ObtainFile) uploadPacked() error {
packedBytes, err := json.Marshal(f.packed)
if err != nil {
return err
}
packedBytes, _ := json.Marshal(f.packed)
return oss.Upload(f.parser.splitUploadPath()+"packed.json", bytes.NewReader(packedBytes))
}
......@@ -222,6 +221,23 @@ func (f *ObtainFile) Worker(err error) {
return
}
g.Log().Async().Infof("filename: %s upload success!", f.parser.name)
f.ok <- true
}
// 上传失败通知
func (f *ObtainFile) Fail() {
if err := f.n.notice("/web/room_files_error", g.Map{
"code": 400,
"uuid": f.r.Uuid,
"room_num": f.r.RoomNum,
"path": f.parser.dataPath,
}); err != nil {
g.Log().Async().Error(err)
}
}
// 上传成功通知
func (f *ObtainFile) Success(roomID int) {
m := g.Map{
"name": f.parser.name,
"file_name_hash": f.parser.nameHash(),
......@@ -236,7 +252,11 @@ func (f *ObtainFile) Worker(err error) {
"time": gtime.Datetime(),
"nickname": f.r.Nickanem,
"uuid": f.r.Uuid,
"room_id": roomID,
}
m[typeMaterial[f.r.Type]] = 1
f.ok <- m
if err := f.n.notice("/web/room_files_add", m); err != nil {
g.Log().Async().Error(err)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment