123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package world_service
- import (
- "context"
- "fmt"
- "github.com/cloudwego/hertz/pkg/app"
- "github.com/cloudwego/hertz/pkg/protocol/consts"
- uuid "github.com/satori/go.uuid"
- "net/http"
- "pji_desktop_http/biz/dal/mysql"
- "pji_desktop_http/biz/model"
- "pji_desktop_http/common/config"
- "pji_desktop_http/common/config/c_log"
- "pji_desktop_http/common/entity"
- )
- // UploadWorldFile 将world文件上传到oss
- // @router /world/upload/worldfile [GET]
- func UploadWorldFile(ctx context.Context, c *app.RequestContext) {
- equipmentNo := c.Query("equipmentNo")
- fmt.Println("equipmentNo", equipmentNo)
- sceneNo := c.Query("sceneNo")
- fmt.Println("sceneNo", sceneNo)
- header, err := c.FormFile("file")
- if err != nil {
- c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
- return
- }
- fileName := header.Filename
- fmt.Println("filename", fileName)
- ossWorldObjectKey := config.WorldOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + "map.world"
- fmt.Println("ossWorldObjectKey", ossWorldObjectKey)
- f, _ := header.Open()
- defer f.Close()
- config.OssMutex.Lock()
- err = config.OssBucket.PutObject(ossWorldObjectKey, f)
- config.OssMutex.Unlock()
- if err != nil {
- c_log.GlobalLogger.Error("程序异常退出。上传world文件", fileName, "->", ossWorldObjectKey, "出错:", err)
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传world文件失败"})
- return
- }
- c_log.GlobalLogger.Info("上传world文件", fileName, "->", ossWorldObjectKey, "成功。")
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传world文件成功"})
- // 保存world记录到数据库
- world := model.World{ID: uuid.NewV1().String(), SceneID: sceneNo, WorldURL: ossWorldObjectKey}
- mysql.AddWorld(ctx, world)
- }
- // UploadSTLFile 将map.stl文件上传到oss
- // @router /world/upload/stlfile [GET]
- func UploadSTLFile(ctx context.Context, c *app.RequestContext) {
- equipmentNo := c.Query("equipmentNo")
- fmt.Println("equipmentNo", equipmentNo)
- sceneNo := c.Query("sceneNo")
- fmt.Println("sceneNo", sceneNo)
- header, err := c.FormFile("file")
- if err != nil {
- c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
- return
- }
- fileName := header.Filename
- fmt.Println("filename", fileName)
- ossSTLObjectKey := config.WorldOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + "map.stl"
- fmt.Println("ossSTLObjectKey", ossSTLObjectKey)
- f, _ := header.Open()
- defer f.Close()
- config.OssMutex.Lock()
- err = config.OssBucket.PutObject(ossSTLObjectKey, f)
- config.OssMutex.Unlock()
- if err != nil {
- c_log.GlobalLogger.Error("程序异常退出。上传stl文件", fileName, "->", ossSTLObjectKey, "出错:", err)
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传stl文件失败"})
- return
- }
- c_log.GlobalLogger.Info("上传stl文件", fileName, "->", ossSTLObjectKey, "成功。")
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传stl文件成功"})
- // 更新stl记录到world数据库
- updateValue := model.World{SceneID: sceneNo, StlURL: &ossSTLObjectKey}
- //world := model.World{ID: uuid.NewV1().String(), SceneID: sceneNo, WorldURL: ossSTLObjectKey}
- err = mysql.UpdateWorld(ctx, updateValue)
- if err != nil {
- return
- }
- }
- // CheckWorldFileStatus 检查world文件在oss中是否存在
- // @router /world/check/file/world/status [GET]
- func CheckWorldFileStatus(ctx context.Context, c *app.RequestContext) {
- sceneID := c.Query("id")
- fmt.Println("id", sceneID)
- world, err := mysql.QueryWorld(ctx, sceneID)
- if err != nil || world == nil {
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "world文件不存在"})
- return
- }
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "world文件已存在"})
- }
|