From 3071926f1169ad4600ae74f62a868521c882df77 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 8 Apr 2025 14:12:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E5=88=9B=E5=BB=BA=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BE=85=E8=BD=AC=E6=8D=A2=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/file.go | 38 ++++++++++++++++++++++++++++++++-- handler/file.go | 46 ++++++++++++++++++++++++++++++++++++++++++ proto/tool.go | 4 +++- service/fileService.go | 25 +++++++++++++++++++++++ service/spark.go | 12 +++++------ 5 files changed, 116 insertions(+), 9 deletions(-) diff --git a/dao/file.go b/dao/file.go index 203f1aa..2277e83 100644 --- a/dao/file.go +++ b/dao/file.go @@ -335,7 +335,7 @@ func DeleteFileContentByID(id int) error { return res.Error } -func UpdateFileContentByID(id int, fileContent string) error { +func UpdateFileContentByID(id uint, fileContent string) (uint, error) { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() @@ -343,5 +343,39 @@ func UpdateFileContentByID(id int, fileContent string) error { db2 = DB } res := db2.Model(&FileContent{}).Where("id = ?", id).Updates(FileContent{FileContent: fileContent}) - return res.Error + return id, res.Error +} + +func GetFileWillConvertContentFileList() ([]File, error) { + //获取文件内容表里没有内容的文件列表 + var files []File + var db2 *gorm.DB + if proto.Config.SERVER_SQL_LOG { + db2 = DB.Debug() + } else { + db2 = DB + } + db2.Table("files").Select("files.*").Joins("LEFT JOIN file_contents ON files.id = file_contents.file_id").Where("file_contents.file_id IS NULL").Find(&files) + if db2.Error != nil { + log.Printf("查询文件列表时出错,错误信息: %v", db2.Error) + return nil, db2.Error + } + return files, nil +} + +// 根据文件id查找文件内容表是否有内容 +func FindFileContentByFileIDAndContentID(fileID int) (FileContent, error) { + var fileContent FileContent + var db2 *gorm.DB + if proto.Config.SERVER_SQL_LOG { + db2 = DB.Debug() + } else { + db2 = DB + } + db2.Where("file_id = ?", fileID).First(&fileContent) + if db2.Error != nil { + log.Printf("查询文件内容时出错,fileID: %d, 错误信息: %v", fileID, db2.Error) + return FileContent{}, db2.Error + } + return fileContent, nil } diff --git a/handler/file.go b/handler/file.go index f243b43..1251bff 100644 --- a/handler/file.go +++ b/handler/file.go @@ -21,6 +21,8 @@ func SetUpFileGroup(router *gin.Engine) { fileGroup.POST("/file_delete", DeleteUserFile) fileGroup.POST("/file_update", UpdateUserFile) fileGroup.POST("/find_file_content", FindFileContent) + fileGroup.POST("/create_file_content", CreateFileContent) + fileGroup.POST("/get_file_will_convert_content", GetFileWillConvertContent) //需要将文件转为文件内容的文件列表接口 } @@ -301,3 +303,47 @@ func FindFileContent(c *gin.Context) { } c.JSON(http.StatusOK, resp) } + +func CreateFileContent(c *gin.Context) { + id, _ := c.Get("id") + userId := int(id.(float64)) + var req proto.FileContentReq + var resp proto.FileContentResp + if err := c.ShouldBind(&req); err == nil { + fileContentID, err2 := service.CreateFileContent(userId, req.FileID, req.FileContent) + if err2 != nil { + resp.Code = proto.ParameterError + resp.Message = "find file content failed:" + err2.Error() + } else { + resp.Code = proto.SuccessCode + resp.Message = "success" + resp.Data = fileContentID + } + } else { + resp.Code = proto.ParameterError + resp.Message = "upload form parameter decode error:" + err.Error() + } + c.JSON(http.StatusOK, resp) +} + +func GetFileWillConvertContent(c *gin.Context) { + id, _ := c.Get("id") + userId := int(id.(float64)) + var req proto.FileContentReq + var resp proto.FileContentResp + if err := c.ShouldBind(&req); err == nil { + files, err2 := service.GetFileWillConvertContentFileList(userId) + if err2 != nil { + resp.Code = proto.ParameterError + resp.Message = "find file content failed:" + err2.Error() + } else { + resp.Code = proto.SuccessCode + resp.Message = "success" + resp.Data = files + } + } else { + resp.Code = proto.ParameterError + resp.Message = "upload form parameter decode error:" + err.Error() + } + c.JSON(http.StatusOK, resp) +} diff --git a/proto/tool.go b/proto/tool.go index ac166a5..920ead5 100644 --- a/proto/tool.go +++ b/proto/tool.go @@ -13,7 +13,9 @@ type DashBoardStatisticsResp struct { } type FileContentReq struct { - UserFileID int `json:"user_file_id" form:"user_file_id"` // 用户文件ID + UserFileID int `json:"user_file_id" form:"user_file_id"` // 用户文件ID + FileID int `json:"file_id" form:"file_id"` // 文件ID + FileContent string `json:"file_content" form:"file_content"` // 文件内容 } type FileContentResp struct { diff --git a/service/fileService.go b/service/fileService.go index 44dc0e6..0580b9d 100644 --- a/service/fileService.go +++ b/service/fileService.go @@ -316,6 +316,31 @@ func FindFileContent(userID int, userReq *proto.FileContentReq) ([]dao.FileConte return fileContents, nil } +func CreateFileContent(userID, fileID int, fileContent string) (uint, error) { + user := GetUserByIDWithCache(userID) + if user.Role != "admin" { + return 0, errors.New("no permission") + } + //查找文件是否存在 + fileContentC, err := dao.FindFileContentByFileIDAndContentID(fileID) + if err != nil { + return 0, err + } + //如果文件存在,则更新文件内容 + if fileContentC.ID != 0 { + return dao.UpdateFileContentByID(fileContentC.ID, fileContent) + } + return dao.CreateFileContent(fileID, fileContent) +} + +func GetFileWillConvertContentFileList(userID int) ([]dao.File, error) { + user := GetUserByIDWithCache(userID) + if user.Role != "admin" { + return nil, errors.New("no permission") + } + return dao.GetFileWillConvertContentFileList() +} + func readFileContent(filePath string) (string, error) { file, err := os.Open(filePath) if err != nil { diff --git a/service/spark.go b/service/spark.go index ee54e87..17762ce 100644 --- a/service/spark.go +++ b/service/spark.go @@ -201,12 +201,12 @@ func SparkV2(modelParam proto.ModelParam, imCtx *proto.IMParamContext) { go func() { data := genSparkParams(imCtx.UserID, modelParam.APPID, modelParam.Domain, imCtx.SessionID, modelParam.System) //将数据转换为json - dataByte, err3 := json.Marshal(data) - if err3 != nil { - fmt.Println("Error parsing JSON:", err) - return - } - log.Println("spark send message:", string(dataByte)) + //dataByte, err3 := json.Marshal(data) + //if err3 != nil { + // fmt.Println("Error parsing JSON:", err) + // return + //} + //log.Println("spark send message:", string(dataByte)) err2 := conn.WriteJSON(data) if err != nil { fmt.Println("write message error:", err2) From fcd37a598db258d385b57dbd1896b8fe13422e9f Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Tue, 8 Apr 2025 14:15:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E5=88=9B=E5=BB=BA=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BE=85=E8=BD=AC=E6=8D=A2=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=9A=84=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/fileService.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/service/fileService.go b/service/fileService.go index 0580b9d..eb667f6 100644 --- a/service/fileService.go +++ b/service/fileService.go @@ -338,9 +338,33 @@ func GetFileWillConvertContentFileList(userID int) ([]dao.File, error) { if user.Role != "admin" { return nil, errors.New("no permission") } - return dao.GetFileWillConvertContentFileList() + files, err2 := dao.GetFileWillConvertContentFileList() + if err2 != nil { + return nil, err2 + } + var res []dao.File + for _, file := range files { + fileType := strings.Split(file.FileStoreName, ".")[1] + //如果文件类型是图片则不需要返回 + if fileType == "jpg" || fileType == "png" || fileType == "jpeg" || fileType == "gif" { + continue + } + //如果文件类型是视频则不需要返回 + if fileType == "mp4" || fileType == "avi" || fileType == "rmvb" || fileType == "mkv" { + continue + } + //如果文件类型是音频则不需要返回 + if fileType == "mp3" || fileType == "wav" || fileType == "wma" { + continue + } + //如果文件类型是压缩包则不需要返回 + if fileType == "zip" || fileType == "rar" || fileType == "7z" { + continue + } + res = append(res, file) + } + return res, nil } - func readFileContent(filePath string) (string, error) { file, err := os.Open(filePath) if err != nil {