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) } // CheckWorldFileStatus 检查world文件oss中是否存在 // @router /world/check/file/status [GET] func CheckWorldFileStatus(ctx context.Context, c *app.RequestContext) { sceneID := c.Query("sceneID") fmt.Println("sceneID", 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文件已存在"}) }