添加文件增删改查
This commit is contained in:
parent
5b007c0a73
commit
4bad0b1d59
55
dao/file.go
55
dao/file.go
|
|
@ -177,3 +177,58 @@ func FindConfigFileByAuthID(auth_id int) []ConfigFile {
|
||||||
}
|
}
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 通用通过用户id及类型查询
|
||||||
|
func FileUserFileList(userID int, fileType string) []FileAuth {
|
||||||
|
var files []FileAuth
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
DB.Debug().Where("auth_id = ? and upload_type = ?", userID, fileType).Find(&files)
|
||||||
|
} else {
|
||||||
|
DB.Where("auth_id = ? and upload_type = ?", userID, fileType).Find(&files)
|
||||||
|
}
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通过文件名及用户id查询(文件名模糊查询),索引不生效查询较慢
|
||||||
|
func FindFileByUserFileName(fileName string, userID int) []FileAuth {
|
||||||
|
var files []FileAuth
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
DB.Debug().Where("auth_id = ? and user_file_name LIKE ?", userID, "%"+fileName+"%").Find(&files)
|
||||||
|
} else {
|
||||||
|
DB.Where("auth_id = ? and user_file_name LIKE ?", userID, "%"+fileName+"%").Find(&files)
|
||||||
|
}
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除文件
|
||||||
|
func DeleteFileAuthByID(id int) error {
|
||||||
|
var res *gorm.DB
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
res = DB.Debug().Delete(&FileAuth{}, id)
|
||||||
|
} else {
|
||||||
|
res = DB.Delete(&FileAuth{}, id)
|
||||||
|
}
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改文件名(暂时只能改文件名)
|
||||||
|
func UpdateFileAuthByID(id int, file FileAuth) error {
|
||||||
|
var res *gorm.DB
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
res = DB.Debug().Model(&FileAuth{}).Where("id = ?", id).Updates(&file)
|
||||||
|
} else {
|
||||||
|
res = DB.Model(&FileAuth{}).Where("id = ?", id).Updates(&file)
|
||||||
|
}
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据FileAuth的id查询
|
||||||
|
func FindFileAuthByID(id int) FileAuth {
|
||||||
|
var file FileAuth
|
||||||
|
if proto.Config.SERVER_SQL_LOG {
|
||||||
|
DB.Debug().Where("id = ?", id).First(&file)
|
||||||
|
} else {
|
||||||
|
DB.Where("id = ?", id).First(&file)
|
||||||
|
}
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,77 @@ func SetUpFileGroup(router *gin.Engine) {
|
||||||
fileGroup.POST("/config_search", SearchConfigFile)
|
fileGroup.POST("/config_search", SearchConfigFile)
|
||||||
fileGroup.POST("/upload", UploadFileV2)
|
fileGroup.POST("/upload", UploadFileV2)
|
||||||
fileGroup.GET("/general/:filename", GetFile)
|
fileGroup.GET("/general/:filename", GetFile)
|
||||||
|
fileGroup.POST("/file_list", GetUserFileList)
|
||||||
|
fileGroup.POST("/file_delete", DeleteUserFile)
|
||||||
|
fileGroup.POST("/file_update", UpdateUserFile)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetUserFileListReq struct {
|
||||||
|
FileID int `json:"file_id"` // 文件ID
|
||||||
|
FileName string `json:"file_name"` // 文件名search时必须
|
||||||
|
Type string `json:"type"` // all,search
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUserFileList(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
user_id := int(id.(float64))
|
||||||
|
var req GetUserFileListReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
if req.Type == "all" {
|
||||||
|
fileList := dao.FileUserFileList(user_id, proto.UserFileTypeFile)
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": fileList})
|
||||||
|
} else if req.Type == "search" {
|
||||||
|
fileList := dao.FindFileByUserFileName(req.FileName, user_id)
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": fileList})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "search file type error", "code": proto.ParameterError, "message": "failed"})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteUserFile(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
userId := int(id.(float64))
|
||||||
|
var req GetUserFileListReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
err2 := service.DeleteUserFile(userId, req.FileID)
|
||||||
|
if err2 != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "delete file failed", "code": proto.DeleteFileFailed, "message": "failed"})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateUserFile(c *gin.Context) {
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
userId := int(id.(float64))
|
||||||
|
var req GetUserFileListReq
|
||||||
|
if err := c.ShouldBind(&req); err == nil {
|
||||||
|
err2 := service.UpdateUserFile(userId, req.FileID, req.FileName)
|
||||||
|
if err2 != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "update file failed:" + err2.Error(), "code": proto.SaveFileInfoFailed, "message": "failed"})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func AddConfigFile(c *gin.Context) {
|
func AddConfigFile(c *gin.Context) {
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
user_id := int(id.(float64))
|
user_id := int(id.(float64))
|
||||||
|
|
|
||||||
|
|
@ -138,3 +138,12 @@ const (
|
||||||
DefaultTopK = 0.5
|
DefaultTopK = 0.5
|
||||||
DefaultTopP = 0.8
|
DefaultTopP = 0.8
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 文件
|
||||||
|
const (
|
||||||
|
UserFileTypeIM = "im" // IM文件
|
||||||
|
UserFileTypeAvatar = "avatar" // 用户头像
|
||||||
|
UserFileTypeFile = "file" // 通用文件
|
||||||
|
UserFileTypeConfig = "config" // 配置文件
|
||||||
|
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"StuAcaWorksAI/proto"
|
"StuAcaWorksAI/proto"
|
||||||
"StuAcaWorksAI/worker"
|
"StuAcaWorksAI/worker"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
@ -217,3 +218,27 @@ func (c *ConfigFileService) SearchAllConfigFile(userId int) ([]dao.ConfigFile, e
|
||||||
config_files := dao.FindConfigFileByAuthID(userId)
|
config_files := dao.FindConfigFileByAuthID(userId)
|
||||||
return config_files, nil
|
return config_files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpdateUserFile(userID int, fileAuthID int, fileAuthName string) error {
|
||||||
|
fileAuth := dao.FindFileAuthByID(fileAuthID)
|
||||||
|
if fileAuth.ID == 0 {
|
||||||
|
return errors.New("file auth not found")
|
||||||
|
}
|
||||||
|
if fileAuth.AuthID != userID {
|
||||||
|
return errors.New("no permission")
|
||||||
|
}
|
||||||
|
fileAuth.UserFileName = fileAuthName
|
||||||
|
err := dao.UpdateFileAuthByID(userID, fileAuth)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
func DeleteUserFile(userID, fileAuthID int) error {
|
||||||
|
fileAuth := dao.FindFileAuthByID(fileAuthID)
|
||||||
|
if fileAuth.ID == 0 {
|
||||||
|
return errors.New("file auth not found")
|
||||||
|
}
|
||||||
|
if fileAuth.AuthID != userID {
|
||||||
|
return errors.New("no permission")
|
||||||
|
}
|
||||||
|
err := dao.DeleteFileAuthByID(fileAuthID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue