123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package controller
- import (
- "context"
- "file_upload/app/e"
- "file_upload/app/model"
- "file_upload/global"
- "file_upload/simple_zap"
- "github.com/bytedance/sonic"
- "github.com/gin-gonic/gin"
- )
- type Body struct {
- Name string
- Data any
- Type bool
- }
- // func FuileUpload(c *gin.Context) {
- // const maxMemory = 1 << 20
- // if err := c.Request.ParseMultipartForm(maxMemory); err != nil {
- // e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
- // simple_zap.WithCtx(context.Background()).Sugar().Warn(err)
- // return
- // }
- // file, err := c.FormFile("previewImage")
- // formFile, err := c.FormFile("excalidrawLib")
- // if err != nil {
- // e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
- // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
- // return
- // }
- // title := c.PostForm("name")
- // if err := c.SaveUploadedFile(file, "./upload/"+file.Filename+title+"."+"png"); err != nil {
- // e.ResponseWithMsg(c, e.ERROR, "保存文件失败")
- // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
- // return
- // }
- // if err := c.SaveUploadedFile(formFile, "./upload/"+formFile.Filename+title+"."+"excalidrawlib"); err != nil {
- // e.ResponseWithMsg(c, e.ERROR, "保存文件失败")
- // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "保存文件失败")
- // return
- // }
- // e.ResponseSuccess(c, e.SUCCESS)
- // }
- //
- // func FileDwon(c *gin.Context) {
- // filname := "cec.json"
- // filpath := "./upload/" + filname
- // if _, err := os.Stat(filpath); os.IsNotExist(err) {
- // simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "文件获取失败")
- // c.AbortWithStatus(http.StatusNotFound)
- // return
- // }
- // // 设置 Content-Disposition 响应头,提供默认的文件名
- // c.Header("Content-Disposition", "attachment; filename="+filname)
- // // 发送文件内容
- // c.File(filpath)
- // }
- func SaveFile(c *gin.Context) {
- file := Body{}
- err := c.BindJSON(&file)
- if err != nil {
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取参数失败")
- e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
- return
- }
- m := map[string]any{
- "type": file.Type,
- "data": file.Data,
- }
- marshal, err := sonic.Marshal(m)
- if err != nil {
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
- return
- }
- result, err := global.Rdb.Exists(context.Background(), file.Name).Result()
- if result == 1 {
- e.ResponseWithMsg(c, e.AlreadyExists, e.AlreadyExists.GetMsg())
- return
- }
- set := global.Rdb.Set(context.Background(), file.Name, marshal, 0)
- if set.Err() != nil {
- simple_zap.WithCtx(context.Background()).Sugar().Warn(set.Err(), "保存文件失败")
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
- return
- }
- e.ResponseSuccess(c, e.SUCCESS)
- }
- // TemplateItem 用于获取所有的模板项
- // 参数:
- // - c *gin.Context: Gin框架的上下文对象,用于处理HTTP请求和响应
- // 返回值:
- // - 无
- func TemplateItem(c *gin.Context) {
- type Types struct {
- Type bool `json:"type"`
- }
- t := &Types{}
- c.BindJSON(&t)
- var allKeys []string // 存储所有检索到的模板项的键名
- var cursor uint64 = 0 // 用于分页查询的游标
- // 循环检索模板项,直到遍历完所有项或出现错误
- for {
- keys, newCursor, err := global.Rdb.Scan(context.Background(), cursor, "*", 100).Result() // 每次检索最多100个键名
- if err != nil {
- // 记录日志并返回错误信息
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
- return
- }
- allKeys = append(allKeys, keys...) // 将检索到的键名添加到allKeys列表
- cursor = newCursor // 更新游标
- if cursor == 0 {
- break // 如果游标为0,表示已遍历完所有项,退出循环
- }
- }
- m := make(map[string]any) // 创建一个map用于存储模板项的键名和对应的值
- // 遍历所有键名,获取其对应的值
- for _, key := range allKeys {
- result, err := global.Rdb.Get(context.Background(), key).Result()
- if err != nil {
- // 记录日志并返回错误信息
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取模板失败")
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
- return
- }
- file := model.Root{}
- //marshal, err := sonic.Marshal(result)
- err = sonic.Unmarshal([]byte(result), &file)
- if err != nil {
- // 记录日志并返回错误信息
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "序列化失败")
- e.ResponseWithMsg(c, e.ERROR, e.ERROR.GetMsg())
- return
- }
- if t.Type == file.Type {
- m[key] = result // 将键名和对应的值添加到map中
- }
- }
- // 返回所有模板项的键值对
- e.ResponseSuccess(c, m)
- }
- // GetTemplate 从服务器获取模板
- func GetTemplate(c *gin.Context) {
- name := c.Query("name")
- if name == "" {
- e.ResponseWithMsg(c, e.ERROR, "参数错误")
- return
- }
- result, err := global.Rdb.Get(context.Background(), name).Result()
- if err != nil {
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "获取文件失败")
- e.ResponseWithMsg(c, e.ERROR, "获取文件失败")
- return
- }
- e.ResponseSuccess(c, result)
- }
- func DeleteTemplate(c *gin.Context) {
- name := c.Query("name")
- if name == "" {
- e.ResponseWithMsg(c, e.ERROR, "参数错误")
- return
- }
- err := global.Rdb.Del(context.Background(), name).Err()
- if err != nil {
- simple_zap.WithCtx(context.Background()).Sugar().Warn(err, "删除文件失败")
- e.ResponseWithMsg(c, e.ERROR, "删除文件失败")
- return
- }
- e.ResponseSuccess(c, e.SUCCESS)
- }
- //// SaveUsernameToLocalStorage 保存用户名到本地
- //func SaveUsernameToLocalStorage(c *gin.Context) {
- // usernam := c.PostForm("username")
- // simple_zap.WithCtx(context.Background()).Sugar().Info(usernam)
- // global.Rdb.Set(context.Background(), "username", usernam, 0)
- // e.ResponseSuccess(c, e.SUCCESS)
- //}
- //
- //// SerializedItems 保存素材到redis数据库
- //func SerializedItems(c *gin.Context) {
- // type Params struct {
- // Params string
- // }
- // serializedItem := Params{}
- // err := c.BindJSON(&serializedItem)
- // if err != nil {
- // simple_zap.WithCtx(context.Background()).Sugar().Warn("获取参数失败")
- // }
- // fmt.Println("serializedItems:", serializedItem.Params)
- // global.Rdb.Set(context.Background(), "serializedItems", serializedItem.Params, 0)
- // e.ResponseSuccess(c, e.SUCCESS)
- //}
- //// GetSerializedItems 从redis数据库获取素材信息
- //func GetSerializedItems(c *gin.Context) {
- // get := global.Rdb.Get(context.Background(), "serializedItems")
- // fmt.Println("GetserializedItems:", get.Val())
- // e.ResponseSuccess(c, get.Val())
- //}
|