Merge branch 'refs/heads/feat-model-file-img'

This commit is contained in:
junleea 2025-04-08 14:15:57 +08:00
commit 6eee65aa32
5 changed files with 140 additions and 9 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -33,6 +33,8 @@ type MessageModelIDCountSt struct {
type FileContentReq struct {
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 {

View File

@ -316,6 +316,55 @@ 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")
}
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 {

View File

@ -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)