Commit ccc072cf by Li Feifei

format code

parent a9c2f290
Pipeline #15962 passed with stages
in 34 seconds
...@@ -38,7 +38,7 @@ func (f *fileApi) Upload(r *ghttp.Request) { ...@@ -38,7 +38,7 @@ func (f *fileApi) Upload(r *ghttp.Request) {
response.Fail(r, err) response.Fail(r, err)
} }
// 检测课件格式 // 检测课件格式
parserFile, err := file.Upload(&req, service.File.Done, service.File.Ok) parserFile, err := file.Upload(req, service.File.Done, service.File.Ok)
if err != nil { if err != nil {
response.Fail(r, err) response.Fail(r, err)
} }
......
package boot package boot
import ( import (
"fmt" "os"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/net/ghttp"
"os"
) )
func init() { func init() {
gdb.SetConfig(gdb.Config{
link := fmt.Sprintf("mysql:%s:%s@tcp(%s:3306)/xyu", "default": gdb.ConfigGroup{
os.Getenv("read_user"), gdb.ConfigNode{
os.Getenv("read_pass"), Host: os.Getenv("read_sql"),
os.Getenv("read_sql")) Port: "3306",
_ = g.Config().Set("database.link", link) User: os.Getenv("read_user"),
Pass: os.Getenv("read_pass"),
Type: "mysql",
Charset: "utf8mb4",
},
},
})
g.Server().SetNameToUriType(ghttp.URI_TYPE_CAMEL) g.Server().SetNameToUriType(ghttp.URI_TYPE_CAMEL)
g.Log().SetAsync(true) g.Log().SetAsync(true)
......
...@@ -2,10 +2,11 @@ package file ...@@ -2,10 +2,11 @@ package file
import ( import (
"encoding/json" "encoding/json"
"net/http"
"os" "os"
"ppt_server/app/model" "ppt_server/app/model"
"ppt_server/library/http" pHttp "ppt_server/library/http"
"github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
...@@ -16,38 +17,36 @@ type notify struct { ...@@ -16,38 +17,36 @@ type notify struct {
name string name string
password string password string
url string url string
client *http.SessionHttpClient client *pHttp.SessionHttpClient
} }
func newNotify() *notify { var n = 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"), client: pHttp.NewSessionHttpClient(),
client: http.NewSessionHttpClient(),
}
} }
//回调验权 //回调验权
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 {
return err return err
} }
g.Log().Async().Infof("url: %s, res: %s", url, string(res)) g.Log().Async().Infof("url: %s, res: %s", url, string(res))
var notifyResponse model.NotifyResponse var notifyResponse model.NotifyResponse
if err = json.Unmarshal(res, &notifyResponse); err != nil { if err = json.Unmarshal(res, &notifyResponse); err != nil {
return err return err
} }
if notifyResponse.Code != 200 { if notifyResponse.Code != http.StatusOK {
return gerror.New(notifyResponse.Msg) return gerror.New(notifyResponse.Msg)
} }
return nil return nil
} }
// 登录 // 登录
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,
...@@ -61,16 +60,16 @@ func (n *notify) auth(data g.Map) error { ...@@ -61,16 +60,16 @@ func (n *notify) auth(data g.Map) error {
return err return err
} }
buf, _ := gregex.Match(`<meta name="csrf-token" content="([^"]+)">`, bufferBytes) buf, _ := gregex.Match(reTokenPattern, bufferBytes)
data["_token"] = string(buf[1]) data["_token"] = string(buf[1])
return nil return nil
} }
// 发送通知 // 发送通知
func (n *notify) notice(path string, data g.Map) error { func (n notify) notice(path string, data g.Map) error {
if err := n.auth(data); err != nil { if err := n.auth(data); err != nil {
return err return err
} }
return n.verify(n.url+path, data) return n.verify(n.url+path, data)
} }
...@@ -78,7 +78,7 @@ func (f *parser) contentHash() string { ...@@ -78,7 +78,7 @@ func (f *parser) contentHash() string {
// 数据库存储path // 数据库存储path
func (f *parser) dataPath() string { func (f *parser) dataPath() string {
return f.splitUploadPath() + gurl.Encode(f.slicePath[len(f.slicePath)-1:][0]) return f.splitUploadPath() + gurl.Encode(f.slicePath[len(f.slicePath)-1:][0])
} }
// 解析doctype // 解析doctype
......
...@@ -26,14 +26,14 @@ import ( ...@@ -26,14 +26,14 @@ import (
// 解析URL返回基本信息 // 解析URL返回基本信息
type ObtainFile struct { type ObtainFile struct {
R *model.FileUploadRequest // 客户端请求参数 R model.FileUploadRequest // 客户端请求参数
ok chan bool // 上传课件响应成功 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 // 事件通知 n notify // 事件通知
packed *model.Packed // packed.json packed *model.Packed // packed.json
} }
const ( const (
...@@ -43,9 +43,10 @@ const ( ...@@ -43,9 +43,10 @@ const (
) )
var reFileNameCompile, _ = regexp.Compile(`p([\d]+)[.].*`) var reFileNameCompile, _ = regexp.Compile(`p([\d]+)[.].*`)
var reTokenPattern = `<meta name="csrf-token" content="([^"]+)">`
func Upload(r *model.FileUploadRequest, errChan chan error, func Upload(r model.FileUploadRequest, errChan chan error,
ok chan bool) (file *ObtainFile, err error) { ok chan bool) (file *ObtainFile, err error) {
f, err := parseURL(r.Url) f, err := parseURL(r.Url)
...@@ -63,7 +64,7 @@ func Upload(r *model.FileUploadRequest, errChan chan error, ...@@ -63,7 +64,7 @@ func Upload(r *model.FileUploadRequest, errChan chan error,
Hash: f.contentHash(), Hash: f.contentHash(),
OssImagesPath: f.splitUploadPath() + "images/", OssImagesPath: f.splitUploadPath() + "images/",
}, },
n: newNotify(), n: n,
} }
return file, nil return file, nil
} }
...@@ -167,18 +168,18 @@ func (f *ObtainFile) uploadPicture() error { ...@@ -167,18 +168,18 @@ func (f *ObtainFile) uploadPicture() error {
// 上传packed.json // 上传packed.json
func (f *ObtainFile) uploadPacked() error { func (f *ObtainFile) uploadPacked() error {
for i := 1; i <= f.pageCount; i++ { for i := 1; i <= f.pageCount; i++ {
f.packed.ImagesName = append(f.packed.ImagesName, fmt.Sprintf("%05d", i) +".png") f.packed.ImagesName = append(f.packed.ImagesName, fmt.Sprintf("%05d", i)+".png")
} }
packedBytes, err := json.Marshal(f.packed) packedBytes, err := json.Marshal(f.packed)
if err != nil { if err != nil {
return err return err
} }
return oss.Upload(f.parser.splitUploadPath()+"packed.json", bytes.NewReader(packedBytes)) return oss.Upload(f.parser.splitUploadPath()+"packed.json", bytes.NewReader(packedBytes))
} }
// 拼接下载zip包地址 // 拼接下载zip包地址
func (f *ObtainFile) splitDownloadLink() string { func (f *ObtainFile) splitDownloadLink() string {
return fmt.Sprintf("%s?info=1&words=%v&ssl=1&furl=%s/%s", return fmt.Sprintf("%s?info=1&words=%v&ssl=1&furl=%s/%s",
parserFileLink, f.pageCount, g.Cfg().GetString("oss.url"), parserFileLink, f.pageCount, g.Cfg().GetString("oss.url"),
f.parser.dataPath()) f.parser.dataPath())
} }
...@@ -259,7 +260,7 @@ func (f *ObtainFile) Success(roomID int) { ...@@ -259,7 +260,7 @@ func (f *ObtainFile) Success(roomID int) {
"nickname": f.R.NickName, "nickname": f.R.NickName,
"uuid": f.R.Uuid, "uuid": f.R.Uuid,
"room_id": roomID, "room_id": roomID,
"is_courseware" : 1, "is_courseware": 1,
}); err != nil { }); err != nil {
g.Log().Async().Error(err) 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