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) {
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 {
response.Fail(r, err)
}
......
package boot
import (
"fmt"
"os"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"os"
)
func init() {
link := fmt.Sprintf("mysql:%s:%s@tcp(%s:3306)/xyu",
os.Getenv("read_user"),
os.Getenv("read_pass"),
os.Getenv("read_sql"))
_ = g.Config().Set("database.link", link)
gdb.SetConfig(gdb.Config{
"default": gdb.ConfigGroup{
gdb.ConfigNode{
Host: os.Getenv("read_sql"),
Port: "3306",
User: os.Getenv("read_user"),
Pass: os.Getenv("read_pass"),
Type: "mysql",
Charset: "utf8mb4",
},
},
})
g.Server().SetNameToUriType(ghttp.URI_TYPE_CAMEL)
g.Log().SetAsync(true)
......
......@@ -2,10 +2,11 @@ package file
import (
"encoding/json"
"net/http"
"os"
"ppt_server/app/model"
"ppt_server/library/http"
pHttp "ppt_server/library/http"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g"
......@@ -16,38 +17,36 @@ type notify struct {
name string
password string
url string
client *http.SessionHttpClient
client *pHttp.SessionHttpClient
}
func newNotify() *notify {
return &notify{
url: g.Cfg().GetString("notify.url"),
name: os.Getenv("live_name"),
password: os.Getenv("live_pwd"),
client: http.NewSessionHttpClient(),
}
var n = notify{
url: g.Cfg().GetString("notify.url"),
name: os.Getenv("live_name"),
password: os.Getenv("live_pwd"),
client: pHttp.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)
if err != nil {
return err
}
g.Log().Async().Infof("url: %s, res: %s", url, string(res))
var notifyResponse model.NotifyResponse
if err = json.Unmarshal(res, &notifyResponse); err != nil {
return err
}
if notifyResponse.Code != 200 {
if notifyResponse.Code != http.StatusOK {
return gerror.New(notifyResponse.Msg)
}
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{
"name": n.name,
......@@ -61,16 +60,16 @@ func (n *notify) auth(data g.Map) error {
return err
}
buf, _ := gregex.Match(`<meta name="csrf-token" content="([^"]+)">`, bufferBytes)
buf, _ := gregex.Match(reTokenPattern, bufferBytes)
data["_token"] = string(buf[1])
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 {
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 {
// 数据库存储path
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
......
......@@ -26,14 +26,14 @@ import (
// 解析URL返回基本信息
type ObtainFile struct {
R *model.FileUploadRequest // 客户端请求参数
ok chan bool // 上传课件响应成功
err chan error // 上传课件失败Error
fileSize int64 // 课件大小
pageCount int // 课件图片数量
parser *parser // 课件对象
n *notify // 事件通知
packed *model.Packed // packed.json
R model.FileUploadRequest // 客户端请求参数
ok chan bool // 上传课件响应成功
err chan error // 上传课件失败Error
fileSize int64 // 课件大小
pageCount int // 课件图片数量
parser *parser // 课件对象
n notify // 事件通知
packed *model.Packed // packed.json
}
const (
......@@ -43,9 +43,10 @@ const (
)
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) {
f, err := parseURL(r.Url)
......@@ -63,7 +64,7 @@ func Upload(r *model.FileUploadRequest, errChan chan error,
Hash: f.contentHash(),
OssImagesPath: f.splitUploadPath() + "images/",
},
n: newNotify(),
n: n,
}
return file, nil
}
......@@ -167,18 +168,18 @@ func (f *ObtainFile) uploadPicture() error {
// 上传packed.json
func (f *ObtainFile) uploadPacked() error {
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)
if err != nil {
return err
return err
}
return oss.Upload(f.parser.splitUploadPath()+"packed.json", bytes.NewReader(packedBytes))
}
// 拼接下载zip包地址
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"),
f.parser.dataPath())
}
......@@ -259,7 +260,7 @@ func (f *ObtainFile) Success(roomID int) {
"nickname": f.R.NickName,
"uuid": f.R.Uuid,
"room_id": roomID,
"is_courseware" : 1,
"is_courseware": 1,
}); 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