添加文件内容创建接口,获取待转换文件列表
This commit is contained in:
parent
e960783f5b
commit
3071926f11
38
dao/file.go
38
dao/file.go
|
|
@ -335,7 +335,7 @@ func DeleteFileContentByID(id int) error {
|
||||||
return res.Error
|
return res.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateFileContentByID(id int, fileContent string) error {
|
func UpdateFileContentByID(id uint, fileContent string) (uint, error) {
|
||||||
var db2 *gorm.DB
|
var db2 *gorm.DB
|
||||||
if proto.Config.SERVER_SQL_LOG {
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
db2 = DB.Debug()
|
db2 = DB.Debug()
|
||||||
|
|
@ -343,5 +343,39 @@ func UpdateFileContentByID(id int, fileContent string) error {
|
||||||
db2 = DB
|
db2 = DB
|
||||||
}
|
}
|
||||||
res := db2.Model(&FileContent{}).Where("id = ?", id).Updates(FileContent{FileContent: fileContent})
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ func SetUpFileGroup(router *gin.Engine) {
|
||||||
fileGroup.POST("/file_delete", DeleteUserFile)
|
fileGroup.POST("/file_delete", DeleteUserFile)
|
||||||
fileGroup.POST("/file_update", UpdateUserFile)
|
fileGroup.POST("/file_update", UpdateUserFile)
|
||||||
fileGroup.POST("/find_file_content", FindFileContent)
|
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)
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ type DashBoardStatisticsResp struct {
|
||||||
|
|
||||||
type FileContentReq 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 {
|
type FileContentResp struct {
|
||||||
|
|
|
||||||
|
|
@ -316,6 +316,31 @@ func FindFileContent(userID int, userReq *proto.FileContentReq) ([]dao.FileConte
|
||||||
return fileContents, nil
|
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) {
|
func readFileContent(filePath string) (string, error) {
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -201,12 +201,12 @@ func SparkV2(modelParam proto.ModelParam, imCtx *proto.IMParamContext) {
|
||||||
go func() {
|
go func() {
|
||||||
data := genSparkParams(imCtx.UserID, modelParam.APPID, modelParam.Domain, imCtx.SessionID, modelParam.System)
|
data := genSparkParams(imCtx.UserID, modelParam.APPID, modelParam.Domain, imCtx.SessionID, modelParam.System)
|
||||||
//将数据转换为json
|
//将数据转换为json
|
||||||
dataByte, err3 := json.Marshal(data)
|
//dataByte, err3 := json.Marshal(data)
|
||||||
if err3 != nil {
|
//if err3 != nil {
|
||||||
fmt.Println("Error parsing JSON:", err)
|
// fmt.Println("Error parsing JSON:", err)
|
||||||
return
|
// return
|
||||||
}
|
//}
|
||||||
log.Println("spark send message:", string(dataByte))
|
//log.Println("spark send message:", string(dataByte))
|
||||||
err2 := conn.WriteJSON(data)
|
err2 := conn.WriteJSON(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("write message error:", err2)
|
fmt.Println("write message error:", err2)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue