Commit e4ed42da by a2231243

code format

parent 81a1926b
Pipeline #15532 failed with stages
in 11 seconds
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
) )
// Recover异常恢复 // Recover异常恢复
func MiddlewareRecover(r *ghttp.Request) { func Recover(r *ghttp.Request) {
r.Middleware.Next() r.Middleware.Next()
if err := r.GetError(); err != nil { if err := r.GetError(); err != nil {
r.Response.ClearBuffer() r.Response.ClearBuffer()
...@@ -18,19 +18,17 @@ func MiddlewareRecover(r *ghttp.Request) { ...@@ -18,19 +18,17 @@ func MiddlewareRecover(r *ghttp.Request) {
"code": http.StatusBadRequest, "code": http.StatusBadRequest,
"msg": e.FirstString(), "msg": e.FirstString(),
}) })
default: default:
_ = r.Response.WriteJson(g.Map{ _ = r.Response.WriteJson(g.Map{
"code": http.StatusInternalServerError, "code": http.StatusInternalServerError,
"msg": err.Error(), "msg": err.Error(),
}) })
} }
} }
} }
// CORS跨域 // CORS跨域
func MiddlewareCors(r *ghttp.Request) { func Cors(r *ghttp.Request) {
r.Response.CORSDefault() r.Response.CORSDefault()
r.Middleware.Next() r.Middleware.Next()
} }
package packed package packed
import ( import (
"errors"
"fmt" "fmt"
"github.com/gogf/gf/frame/g"
"mime" "mime"
"net/url" "net/url"
"path" "path"
"strings" "strings"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g"
) )
type file struct { type file struct {
...@@ -34,8 +35,8 @@ func NewFile(ossFileLink, name string) (*file, error) { ...@@ -34,8 +35,8 @@ func NewFile(ossFileLink, name string) (*file, error) {
slicePath := strings.Split(strings.TrimLeft(dataPath, "/"), "/") slicePath := strings.Split(strings.TrimLeft(dataPath, "/"), "/")
if len(slicePath) < 4 { if len(slicePath) < 4 {
g.Log().Async().Error(errors.New("上传URL格式不正确")) g.Log().Async().Error(gerror.New("上传URL格式不正确"))
return nil, errors.New("上传URL格式不正确") return nil, gerror.New("上传URL格式不正确")
} }
f := &file{ f := &file{
ext: path.Ext(name), ext: path.Ext(name),
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"image" "image"
"image/jpeg" "image/jpeg"
...@@ -18,15 +17,16 @@ import ( ...@@ -18,15 +17,16 @@ import (
"ppt_server/app/model" "ppt_server/app/model"
"ppt_server/library" "ppt_server/library"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
"github.com/xxjwxc/gowp/workpool" "github.com/xxjwxc/gowp/workpool"
) )
// 解析URL返回基本信息 // 解析URL返回基本信息
type ObtainFile struct { type ObtainFile struct {
r *model.FileUploadRequest //客户端请求参数 r *model.FileUploadRequest // 客户端请求参数
bucket *ossSdk // oss上传bucket bucket *ossSdk // oss上传bucket
ok chan *model.SuccessResponse // 上传传课件状态 ok chan *model.SuccessResponse // 上传课件响应成功
err chan error // 上传课件失败Error err chan error // 上传课件失败Error
fileSize int64 // 课件大小 fileSize int64 // 课件大小
pageCount int // 课件图片数量 pageCount int // 课件图片数量
...@@ -72,6 +72,7 @@ func (f *ObtainFile) fileInfo() error { ...@@ -72,6 +72,7 @@ func (f *ObtainFile) fileInfo() error {
} }
var fileInfo model.UploadFileInfo var fileInfo model.UploadFileInfo
if err := json.Unmarshal(fileBytes, &fileInfo); err != nil { if err := json.Unmarshal(fileBytes, &fileInfo); err != nil {
return err return err
} }
...@@ -115,10 +116,10 @@ func (f *ObtainFile) job(file *zip.File) ([]interface{}, error) { ...@@ -115,10 +116,10 @@ func (f *ObtainFile) job(file *zip.File) ([]interface{}, error) {
case ".jpeg": case ".jpeg":
img, _ = jpeg.Decode(imageReader) img, _ = jpeg.Decode(imageReader)
default: default:
return nil, errors.New("image ext is not found") return nil, gerror.New("image ext is not found")
} }
if img == nil { if img == nil {
return nil, errors.New("Image invalid") return nil, gerror.New("Image invalid")
} }
var filenameOnly string var filenameOnly string
...@@ -206,12 +207,14 @@ func (f *ObtainFile) Worker(err error) { ...@@ -206,12 +207,14 @@ func (f *ObtainFile) Worker(err error) {
f.parserFile.dataPath) f.parserFile.dataPath)
// 下载ZIP包 // 下载ZIP包
if err = library.Download(downloadZipLink, f.parserFile.tempZipPath); err != nil { if err = library.Download(downloadZipLink, f.parserFile.tempZipPath); err != nil {
g.Log().Async().Error(err) g.Log().Async().Error(err)
f.err <- err f.err <- err
return return
} }
// 清理ZIP包
defer os.Remove(f.parserFile.tempZipPath)
g.Log().Async().Infof("file: %s Zip Donwload Complete", f.parserFile.name) g.Log().Async().Infof("file: %s Zip Donwload Complete", f.parserFile.name)
// 解析ZIP包并上传图片到OSS // 解析ZIP包并上传图片到OSS
...@@ -230,8 +233,6 @@ func (f *ObtainFile) Worker(err error) { ...@@ -230,8 +233,6 @@ func (f *ObtainFile) Worker(err error) {
} }
g.Log().Async().Infof("filename == %s upload success!", f.parserFile.name) g.Log().Async().Infof("filename == %s upload success!", f.parserFile.name)
// 清理zip包
_ = os.Remove(f.parserFile.tempZipPath)
res := &model.SuccessResponse{ res := &model.SuccessResponse{
Name: f.parserFile.name, Name: f.parserFile.name,
......
...@@ -2,7 +2,6 @@ package packed ...@@ -2,7 +2,6 @@ package packed
import ( import (
"encoding/json" "encoding/json"
"errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
...@@ -12,6 +11,7 @@ import ( ...@@ -12,6 +11,7 @@ import (
"ppt_server/app/model" "ppt_server/app/model"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv" "github.com/gogf/gf/util/gconv"
) )
...@@ -50,7 +50,7 @@ func (n *Notfiy) post(url string, data g.Map) error { ...@@ -50,7 +50,7 @@ func (n *Notfiy) post(url string, data g.Map) error {
} }
if login.Code != 200 { if login.Code != 200 {
return errors.New(login.Msg) return gerror.New(login.Msg)
} }
return nil return nil
} }
...@@ -128,6 +128,5 @@ func (n *Notfiy) Fail(object *ObtainFile) { ...@@ -128,6 +128,5 @@ func (n *Notfiy) Fail(object *ObtainFile) {
} }
func (n *Notfiy) Success(res *model.SuccessResponse) { func (n *Notfiy) Success(res *model.SuccessResponse) {
n.notify("/web/room_files_add", gconv.Map(res)) n.notify("/web/room_files_add", gconv.Map(res))
} }
...@@ -12,12 +12,14 @@ func init() { ...@@ -12,12 +12,14 @@ func init() {
s := g.Server() s := g.Server()
s.Use( s.Use(
middleware.MiddlewareCors, middleware.Cors,
middleware.MiddlewareRecover, middleware.Recover,
) )
s.Group(`file`, func(group *ghttp.RouterGroup) { s.Group(`file`, func(group *ghttp.RouterGroup) {
group.GET(`/upload`, api.File.Index) group.ALL(`/`, api.File, "Index, Upload")
group.POST(`/upload`, api.File.Upload)
//group.GET(`/upload`, api.File.Index)
//group.POST(`/upload`, api.File.Upload)
}) })
} }
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