Commit df4051dc by a2231243

code format

parent 3f4aebf9
Pipeline #15530 failed with stages
in 10 seconds
......@@ -10,8 +10,7 @@ import (
var File = fileApi{}
type fileApi struct {
}
type fileApi struct{}
func (f *fileApi) Index(r *ghttp.Request) {
_ = r.Response.WriteJson(g.Map{
......
......@@ -39,3 +39,24 @@ type NotifyResponse struct {
Code int64 `json:"code"`
Msg string `json:"msg"`
}
// 上传成功响应内容
type SuccessResponse struct {
Name string `c:"name"`
Nickanem string `c:"nickname"`
NameHash string `c:"file_name_hash"`
Uuid string `c:"uuid"`
ContentHash string `c:"hash"`
DataPath string `c:"path"`
DocType string `c:"doc_type"`
MineType string `c:"type"`
FileSize int64 `c:"files_size"`
PageCount int `c:"page_count"`
Link string `c:"link"`
Private string `c:"private"`
Time string `c:"time"`
RoomID int `c:"room_id"`
IsCourseware int `c:"is_courseware, omitempty"`
IsMaterial int `c:"is_material, omitempty"`
IsTitlebook int `c:"is_titlebook, omitempty"`
}
......@@ -10,12 +10,12 @@ import (
var File = &fileService{
done: make(chan error),
ok: make(chan bool),
ok: make(chan *model.SuccessResponse),
}
type fileService struct {
done chan error
ok chan bool
ok chan *model.SuccessResponse
}
// 上传课件
......@@ -30,9 +30,10 @@ func (f *fileService) Upload(r *model.FileUploadRequest) {
var room model.XyuRoom
err = dao.XyuRoom.Where(dao.XyuRoom.Columns.RoomNum, r.RoomNum).Scan(&room)
if err != nil {
go func() { f.done <- err }()
g.Log().Async().Errorf("房间不存在, room_num: %s\n", r.RoomNum)
go func() { f.done <- err }()
}
// 上传操作
go parserURLObject.Worker(err)
......@@ -41,10 +42,9 @@ func (f *fileService) Upload(r *model.FileUploadRequest) {
select {
case <-f.done:
// 上传失败
n.Notify("/web/room_files_error", packed.FailGmap(r, parserURLObject))
n.Fail(parserURLObject)
return
case <-f.ok:
case res := <-f.ok:
// 上传成功
count, err := dao.XyuRoomFile.Where(dao.XyuRoomFile.Columns.RoomId, room.Id).
Where(dao.XyuRoomFile.Columns.Name, r.Name).Count()
......@@ -56,6 +56,7 @@ func (f *fileService) Upload(r *model.FileUploadRequest) {
if count == 1 {
return
}
n.Notify("/web/room_files_add", packed.SuccessGmap(r, parserURLObject, room.Id))
res.RoomID = room.Id
n.Success(res)
}
}
# HTTP Server
[server]
Address = ":80"
ServerRoot = "public"
ServerAgent = "gf-app"
LogPath = "/tmp/log/gf-app/server"
DumpRouterMap = true
Address = ":80"
ServerAgent = "gf-app"
LogPath = "/tmp/log/gf-app/server"
DumpRouterMap = true
# Logger.
......@@ -14,17 +13,22 @@
StdoutPrint = true
HeaderPrint = true
# 回调通知
[notify]
url = "http://offcn-live-svc"
url = "http://test-live.offcncloud.com"
name = "offcn"
password = "123123"
[oss]
url = "https://xiaoyu-live.oss-cn-beijing-internal.aliyuncs.com"
url = "https://xiaoyu-live.oss-cn-beijing.aliyuncs.com"
point = "oss-cn-beijing.aliyuncs.com"
keyid = "LTAI1fMvVUPBXl2E"
serect = "cTAMLufmPFznfE0peur8oMmy2c5kvk"
bucket = "xiaoyu-live"
# 下载zip包存储目录
[temp]
path = "D:/"
# Database.
[database]
......@@ -35,10 +39,3 @@
Path = "/tmp/log/gf-app/sql"
Level = "all"
Stdout = true
# Template.
[viewer]
Path = "template"
DefaultFile = "index.html"
Delimiters = ["${", "}"]
......@@ -281,4 +281,4 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gorm.io/driver/mysql v1.0.1/go.mod h1:KtqSthtg55lFp3S5kUXqlGaelnWpKitn4k1xZTnoiPw=
gorm.io/gorm v1.9.19/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.2/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
\ No newline at end of file
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
......@@ -19,6 +19,7 @@ func Get(url string) ([]byte, error) {
}
func Download(file string, path string) error {
g.Log().Async().Infof("Download zip url: %s ", file)
resp, err := http.Get(file)
if err != nil {
return err
......
package packed
import (
"errors"
"fmt"
"github.com/gogf/gf/frame/g"
"mime"
"net/url"
"path"
"strings"
)
type file struct {
ext string
url string //课件地址
ext string //课件后缀
name string //课件名称
nameHash string //课件名hash
contentHash string //课件内容hash
dataPath string //数据库存储路径
fileInfoURL string //解析课件的URL
tempZipPath string //临时存放zip包地址
uploadPath string //oss存储路径
docType string //课件type类型
mineType string //课件mine类型
}
func NewFile(ossFileLink, name string) (*file, error) {
u, err := url.Parse(ossFileLink)
if err != nil {
g.Log().Async().Error(err)
return nil, err
}
dataPath := u.EscapedPath()
slicePath := strings.Split(strings.TrimLeft(dataPath, "/"), "/")
if len(slicePath) < 4 {
g.Log().Async().Error(errors.New("上传URL格式不正确"))
return nil, errors.New("上传URL格式不正确")
}
f := &file{
ext: path.Ext(name),
url: ossFileLink,
dataPath: dataPath[1:],
nameHash: slicePath[3],
contentHash: slicePath[2],
name: name,
fileInfoURL: fmt.Sprintf("%s?info=0&ssl=1&furl=%s%s",
ParserFileLink,
g.Cfg().GetString("oss.url"),
dataPath),
tempZipPath: fmt.Sprintf("%s%s.zip",
g.Cfg().GetString("temp.path"),
slicePath[2]),
uploadPath: strings.Join(slicePath[0:len(slicePath)-1], "/") + "/",
}
f.docType = f.getDocType()
f.mineType = f.getMine()
return f, nil
}
// 解析doctype
......@@ -26,6 +77,5 @@ func (f *file) getDocType() string {
// 获取文件mine
func (f *file) getMine() string {
_ = mime.AddExtensionType(".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation")
return mime.TypeByExtension(f.ext)
}
......@@ -10,10 +10,10 @@ import (
"image/jpeg"
"image/png"
"io/ioutil"
"net/url"
"os"
"path"
"strings"
"time"
"ppt_server/app/model"
"ppt_server/library"
......@@ -22,64 +22,43 @@ import (
"github.com/xxjwxc/gowp/workpool"
)
// 解析URL返回基本信息
type ObtainFile struct {
url string //上传URL
fileName string //上传文件名
NameHash string //文件名hash
ContentHash string //内容hash
DocType string //doc_type
MineType string //文件mine类型
fileInfoURL string //解析文件URL地址
uploadPath string //上传URL路径
DataPath string // 数据库存储Path
Link string //数据库存储link
FileSize int64
PageCount int
bucket *ossSdk
ok chan bool //上传传课件状态
err chan error // 上传课件失败Error
r *model.FileUploadRequest //客户端请求参数
bucket *ossSdk // oss上传bucket
ok chan *model.SuccessResponse // 上传传课件状态
err chan error // 上传课件失败Error
fileSize int64 // 课件大小
pageCount int // 课件图片数量
parserFile *file // 课件对象
}
func ParserURL(r *model.FileUploadRequest, errChan chan error, ok chan bool) (*ObtainFile, error) {
u, err := url.Parse(r.Url)
const (
ParserFileLink = "http://doc.offcncloud.com/"
OssIntranetLink = "xiaoyu-live.oss-cn-beijing-internal.aliyuncs.com"
UploadFileLinkHost = "desktop.offcncloud.com"
)
func ParserURL(r *model.FileUploadRequest, errChan chan error,
ok chan *model.SuccessResponse) (*ObtainFile, error) {
f, err := NewFile(r.Url, r.Name)
if err != nil {
g.Log().Async().Error(err)
return nil, err
}
dataPath := u.EscapedPath()[1:]
slicePath := strings.Split(dataPath, "/")
if len(slicePath) < 4 {
g.Log().Async().Error(errors.New("上传URL格式不正确"))
return nil, errors.New("上传URL格式不正确")
}
f := &file{
ext: path.Ext(r.Name),
}
buecket, err := NewOss("xiaoyu-live")
bucket, err := NewOss(g.Cfg().GetString("oss.bucket"))
if err != nil {
g.Log().Async().Error(err)
return nil, err
}
file := &ObtainFile{
url: r.Url,
fileName: r.Name,
NameHash: slicePath[3],
ContentHash: slicePath[2],
DocType: f.getDocType(),
MineType: f.getMine(),
bucket: buecket,
fileInfoURL: "http://doc.offcncloud.com/?info=0&ssl=1&furl=" + g.Cfg().GetString("oss.url") + u.EscapedPath(),
uploadPath: strings.Join(slicePath[0:len(slicePath)-1], "/") + "/",
DataPath: dataPath,
err: errChan,
ok: ok,
Link: strings.Replace(r.Url, "xiaoyu-live.oss-cn-beijing-internal.aliyuncs.com", "desktop.offcncloud.com", -1),
bucket: bucket,
ok: ok,
err: errChan,
parserFile: f,
r: r,
}
return file, nil
......@@ -87,26 +66,27 @@ func ParserURL(r *model.FileUploadRequest, errChan chan error, ok chan bool) (*O
//从web365获取上传文件信息
func (f *ObtainFile) fileInfo() error {
fileBytes, err := library.Get(f.fileInfoURL)
fileBytes, err := library.Get(f.parserFile.fileInfoURL)
if err != nil {
return err
}
var fileInfo model.UploadFileInfo
if err := json.Unmarshal(fileBytes, &fileInfo); err != nil {
return err
}
if fileInfo.FileSize != 0 {
f.FileSize = fileInfo.FileSize
f.fileSize = fileInfo.FileSize
}
switch {
case fileInfo.SlideCount != 0:
f.PageCount = fileInfo.SlideCount
f.pageCount = fileInfo.SlideCount
case fileInfo.PageCount != 0:
f.PageCount = fileInfo.PageCount
f.pageCount = fileInfo.PageCount
case fileInfo.SheetCount != 0:
f.PageCount = fileInfo.SheetCount
f.pageCount = fileInfo.SheetCount
}
return nil
......@@ -120,13 +100,12 @@ func (f *ObtainFile) job(file *zip.File) ([]interface{}, error) {
defer fd.Close()
reqBody := []byte{}
reqBody, _ = ioutil.ReadAll(fd)
fileBytes := []byte{}
fileBytes, _ = ioutil.ReadAll(fd)
// 获取图片信息reader
imageReader := ioutil.NopCloser(bytes.NewBuffer(reqBody))
imageReader := ioutil.NopCloser(bytes.NewBuffer(fileBytes))
// 上传图片reader
fileReader := ioutil.NopCloser(bytes.NewBuffer(reqBody))
fileReader := ioutil.NopCloser(bytes.NewBuffer(fileBytes))
var img image.Image
fileSuffix := path.Ext(file.Name) //获取文件后缀
......@@ -155,7 +134,7 @@ func (f *ObtainFile) job(file *zip.File) ([]interface{}, error) {
g.Log().Async().Infof("Upload picture file: %s Success", fileName)
return imageInfo, f.bucket.Upload(f.uploadPath+"images/"+fileName, fileReader)
return imageInfo, f.bucket.Upload(f.parserFile.uploadPath+"images/"+fileName, fileReader)
}
func (f *ObtainFile) uploadPng(zipFileAddress string) ([][]interface{}, error) {
......@@ -182,6 +161,7 @@ func (f *ObtainFile) uploadPng(zipFileAddress string) ([][]interface{}, error) {
if err = pool.Wait(); err != nil {
return nil, err
}
return imageInfos, nil
}
......@@ -189,20 +169,20 @@ func (f *ObtainFile) uploadPng(zipFileAddress string) ([][]interface{}, error) {
func (f *ObtainFile) uploadPacked(imagesInfos [][]interface{}) error {
packed := model.Packed{
FileName: f.fileName,
Hash: f.ContentHash,
OssImagesPath: f.uploadPath + "images/",
FileName: f.parserFile.name,
Hash: f.parserFile.contentHash,
OssImagesPath: f.parserFile.uploadPath + "images/",
ImageInfos: imagesInfos,
}
for i := 1; i <= f.PageCount; i++ {
for i := 1; i <= f.pageCount; i++ {
packed.ImagesName = append(packed.ImagesName, fmt.Sprintf("%05d", i)+".png")
}
data, err := json.Marshal(packed)
if err != nil {
return err
}
return f.bucket.Upload(f.uploadPath+"packed.json", bytes.NewReader(data))
return f.bucket.Upload(f.parserFile.uploadPath+"packed.json", bytes.NewReader(data))
}
// 进行上传操作
......@@ -217,28 +197,31 @@ func (f *ObtainFile) Worker(err error) {
f.err <- err
return
}
g.Log().Async().Infof("file: %s file info complete", f.fileName)
g.Log().Async().Infof("file: %s file info complete", f.parserFile.name)
zipURL := fmt.Sprintf("http://doc.offcncloud.com/?info=1&words=%v&ssl=1&furl=%s",
f.PageCount, g.Cfg().GetString("oss.url")+"/"+f.DataPath)
downloadZipLink := fmt.Sprintf("%s?info=1&words=%v&ssl=1&furl=%s/%s",
ParserFileLink,
f.pageCount,
g.Cfg().GetString("oss.url"),
f.parserFile.dataPath)
// 下载ZIP包
zipURLAddress := "D:/" + f.ContentHash + ".zip"
if err = library.Download(zipURL, zipURLAddress); err != nil {
if err = library.Download(downloadZipLink, f.parserFile.tempZipPath); err != nil {
g.Log().Async().Error(err)
f.err <- err
return
}
g.Log().Async().Infof("file: %s Zip Donwload Complete", f.fileName)
g.Log().Async().Infof("file: %s Zip Donwload Complete", f.parserFile.name)
// 解析ZIP包并上传图片到OSS
imagesInfos, err := f.uploadPng(zipURLAddress)
imagesInfos, err := f.uploadPng(f.parserFile.tempZipPath)
if err != nil {
g.Log().Async().Error(err)
f.err <- err
return
}
g.Log().Async().Infof("file: %s Upload picture Complete!", f.fileName)
g.Log().Async().Infof("file: %s Upload picture Complete!", f.parserFile.name)
// 上传packed.json到oss
if err := f.uploadPacked(imagesInfos); err != nil {
g.Log().Async().Error(err)
......@@ -246,8 +229,32 @@ func (f *ObtainFile) Worker(err error) {
return
}
g.Log().Async().Infof("filename == %s upload success!", f.fileName)
g.Log().Async().Infof("filename == %s upload success!", f.parserFile.name)
// 清理zip包
_ = os.Remove(zipURLAddress)
f.ok <- true
_ = os.Remove(f.parserFile.tempZipPath)
res := &model.SuccessResponse{
Name: f.parserFile.name,
NameHash: f.parserFile.nameHash,
ContentHash: f.parserFile.contentHash,
DataPath: f.parserFile.dataPath,
DocType: f.parserFile.docType,
MineType: f.parserFile.mineType,
FileSize: f.fileSize,
PageCount: f.pageCount,
Link: strings.Replace(f.parserFile.url, OssIntranetLink, UploadFileLinkHost, -1),
Private: "1",
Time: time.Now().Format("2006-01-02 15:04:05"),
Nickanem: f.r.Nickanem,
Uuid: f.r.Uuid,
}
switch f.r.Type {
case 1:
res.IsMaterial = 1
case 2:
res.IsCourseware = 1
case 3:
res.IsTitlebook = 1
}
f.ok <- res
}
......@@ -9,7 +9,6 @@ import (
"net/url"
"regexp"
"strings"
"time"
"ppt_server/app/model"
......@@ -18,7 +17,6 @@ import (
)
type Notfiy struct {
token string
name string
password string
url string
......@@ -40,7 +38,7 @@ func NewNotfiy() *Notfiy {
}
}
func (n *Notfiy) Post(url string, data g.Map) error {
func (n *Notfiy) post(url string, data g.Map) error {
res, err := n.request("POST", url, data)
if err != nil {
......@@ -57,7 +55,7 @@ func (n *Notfiy) Post(url string, data g.Map) error {
return nil
}
func (n *Notfiy) Get(url string, data g.Map) ([]byte, error) {
func (n *Notfiy) get(url string, data g.Map) ([]byte, error) {
return n.request("GET", url, data)
}
......@@ -85,77 +83,51 @@ func (n *Notfiy) request(method, r_url string, data g.Map) ([]byte, error) {
return ioutil.ReadAll(res.Body)
}
func (n *Notfiy) setToken() error {
if err := n.Post(n.url+"/web/login", g.Map{
func (n *Notfiy) auth() (string, error) {
if err := n.post(n.url+"/web/login", g.Map{
"name": n.name,
"password": n.password,
}); err != nil {
return err
return "", err
}
// 获取Token
tokenTypes, err := n.Get(n.url+"/web/admin", nil)
tokenTypes, err := n.get(n.url+"/web/admin", nil)
if err != nil {
return err
return "", err
}
re := regexp.MustCompile(`<meta name="csrf-token" content="([^"]+)">`)
token := re.FindAllSubmatch(tokenTypes, -1)
n.token = string(token[0][1])
return nil
return string(token[0][1]), nil
}
// 上传失败通知
func (n *Notfiy) Notify(path string, data g.Map) {
func (n *Notfiy) notify(path string, data g.Map) {
var err error
if err = n.setToken(); err != nil {
csrfToken, err := n.auth()
if err != nil {
g.Log().Async().Error(err)
return
}
data["_token"] = n.token
if err = n.Post(n.url+path, data); err != nil {
data["_token"] = csrfToken
if err = n.post(n.url+path, data); err != nil {
g.Log().Async().Errorf("notify fail, err : %s\n", err)
}
g.Log().Async().Infof("Notify Success!")
return
}
func FailGmap(r *model.FileUploadRequest, object *ObtainFile) g.Map {
return g.Map{
func (n *Notfiy) Fail(object *ObtainFile) {
n.notify("/web/room_files_error", g.Map{
"code": 400,
"uuid": r.Uuid,
"room_num": r.RoomNum,
"path": object.DataPath,
}
"uuid": object.r.Uuid,
"room_num": object.r.RoomNum,
"path": object.parserFile.dataPath,
})
}
func SuccessGmap(r *model.FileUploadRequest, object *ObtainFile, room_id int) g.Map {
data := g.Map{
"room_id": room_id,
"is_courseware": "1",
"private": "1",
"nickname": r.Nickanem,
"name": r.Name,
"time": time.Now().Format("2006-01-02 15:04:05"),
"uuid": r.Uuid,
"file_name_hash": object.NameHash,
"hash": object.ContentHash,
"doc_type": object.DocType,
"type": object.MineType,
"path": object.DataPath,
"files_size": object.FileSize,
"link": object.Link,
"page_count": object.PageCount,
}
switch r.Type {
case 1:
data["is_material"] = 1
case 2:
data["is_courseware"] = 1
case 3:
data["is_titlebook"] = 1
}
func (n *Notfiy) Success(res *model.SuccessResponse) {
return data
n.notify("/web/room_files_add", gconv.Map(res))
}
package packed
import (
"io"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/gogf/gf/frame/g"
"io"
)
type ossSdk struct {
......@@ -11,7 +12,6 @@ type ossSdk struct {
bucket *oss.Bucket
}
func NewOss(bucket string) (*ossSdk, error) {
client, err := oss.New(g.Cfg().GetString("oss.point"),
g.Cfg().GetString("oss.keyid"), g.Cfg().GetString("oss.serect"))
......
......@@ -11,8 +11,15 @@ import (
func init() {
s := g.Server()
s.Use(middleware.MiddlewareCors, middleware.MiddlewareRecover)
s.Use(
middleware.MiddlewareCors,
middleware.MiddlewareRecover,
)
s.Group(`file`, func(group *ghttp.RouterGroup) {
group.ALL(`/`, api.File, "Index, 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