Commit 2dc2e306 by Li Feifei

优化代码

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