saw-go/handler/file.go

457 lines
15 KiB
Go
Raw Normal View History

package handler
import (
"StuAcaWorksAI/dao"
"StuAcaWorksAI/proto"
"StuAcaWorksAI/service"
"github.com/gin-gonic/gin"
2025-03-29 13:58:40 +08:00
"log"
"net/http"
)
func SetUpFileGroup(router *gin.Engine) {
fileGroup := router.Group("/file")
fileGroup.POST("/config_add", AddConfigFile)
fileGroup.POST("/config_delete", DeleteConfigFile)
fileGroup.POST("/config_update", UpdateConfigFile)
fileGroup.POST("/config_search", SearchConfigFile)
fileGroup.POST("/upload", UploadFileV2)
2025-05-16 16:24:04 +08:00
fileGroup.GET("/general/:filename", GetFileV2)
2025-03-28 19:09:34 +08:00
fileGroup.POST("/file_list", GetUserFileList)
fileGroup.POST("/file_delete", DeleteUserFile)
fileGroup.POST("/file_update", UpdateUserFile)
fileGroup.POST("/find_file_content", FindFileContent)
2025-05-21 14:10:59 +08:00
fileGroup.POST("/update_file_content", UpdateFileContent)
fileGroup.POST("/create_file_content", CreateFileContent)
fileGroup.POST("/get_file_will_convert_content", GetFileWillConvertContent) //需要将文件转为文件内容的文件列表接口
fileGroup.POST("/convert_msg_to_file", ConvertMsgToFile) //将消息转为文件
}
2025-03-28 19:09:34 +08:00
type GetUserFileListReq struct {
2025-03-28 19:21:16 +08:00
FileID int `json:"file_id" form:"file_id"` // 文件ID
FileName string `json:"file_name" form:"file_name"` // 文件名search时必须
Type string `json:"type" form:"type"` // all,search
2025-03-28 19:09:34 +08:00
}
2025-05-16 16:24:04 +08:00
func GetFileV2(c *gin.Context) {
//先查看是否有权限
filename := c.Param("filename")
var resp proto.FileContentResp
var req proto.GetFileRequest
err := c.ShouldBindQuery(&req)
if err != nil || filename == "" {
resp.Code = proto.ParameterError
resp.Message = "upload form parameter decode error:" + err.Error()
c.JSON(http.StatusOK, resp)
return
} else {
//查询文件信息
file := dao.FindFileByName(filename)
if file.ID == 0 {
resp.Code = proto.FileNotFound
resp.Message = "file not found"
c.JSON(http.StatusOK, resp)
return
}
//下载文件
if file.NeedAuth == false {
c.Header("Content-Disposition", "attachment; filename="+file.FileName)
c.File(file.FilePath + "/" + file.FileStoreName)
} else {
if req.Token == "" {
resp.Code = proto.TokenIsNull
resp.Message = "token is null"
c.JSON(http.StatusOK, resp)
} else {
user, err2 := service.GetUserInfoByToken(req.Token)
if err2 != nil {
resp.Code = proto.TokenInvalid
resp.Message = "token error"
c.JSON(http.StatusOK, resp)
} else if int(user.ID) != file.AuthID {
resp.Code = proto.PermissionDenied
resp.Message = "permission denied"
c.JSON(http.StatusOK, resp)
} else {
c.Header("Content-Disposition", "attachment; filename="+file.FileName)
c.File(file.FilePath + "/" + file.FileStoreName)
}
}
}
}
}
2025-03-28 19:09:34 +08:00
func GetUserFileList(c *gin.Context) {
id, _ := c.Get("id")
2025-03-29 13:55:50 +08:00
userId := int(id.(float64))
2025-03-28 19:09:34 +08:00
var req GetUserFileListReq
if err := c.ShouldBind(&req); err == nil {
if req.Type == "all" {
2025-03-29 13:58:40 +08:00
log.Println("get user file list by all")
2025-03-29 13:55:50 +08:00
userFiles := service.FindUserFileList(userId, proto.UserFileTypeFile)
2025-03-29 13:38:30 +08:00
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": userFiles})
2025-03-28 19:09:34 +08:00
} else if req.Type == "search" {
2025-03-29 13:55:50 +08:00
fileList := dao.FindFileByUserFileName(req.FileName, userId)
2025-03-28 19:09:34 +08:00
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": fileList})
} else if req.Type == "one" {
if req.FileID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "file_id is empty", "code": proto.ParameterError, "message": "failed"})
return
}
userFile, err2 := service.FindUserFileByID(req.FileID, userId)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "find user file failed", "code": proto.ParameterError, "message": "failed"})
return
} else {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": userFile})
}
2025-03-28 19:09:34 +08:00
} 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) {
id, _ := c.Get("id")
user_id := int(id.(float64))
var req proto.AddConfigFileReq
if err := c.ShouldBind(&req); err == nil {
err2 := service.CreateConfigFile(&req, user_id)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "add config file failed:" + err2.Error(), "code": proto.AddConfigFileFailed, "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 DeleteConfigFile(c *gin.Context) {
id, _ := c.Get("id")
user_id := int(id.(float64))
var req proto.ConfigFileReq
if err := c.ShouldBind(&req); err == nil {
err2 := service.DeleteConfigFile(&req, user_id)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "delete config file failed:" + err2.Error(), "code": proto.DeleteConfigFailed, "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 UpdateConfigFile(c *gin.Context) {
id, _ := c.Get("id")
user_id := int(id.(float64))
var req proto.ConfigFileReq
if err := c.ShouldBind(&req); err == nil {
var configFileService service.ConfigFileService
err2 := configFileService.UpdateConfigFile(&req, user_id)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "update config file failed:" + err2.Error(), "code": proto.UpdateConfigFailed, "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 SearchConfigFile(c *gin.Context) {
id, _ := c.Get("id")
user_id := int(id.(float64))
var req proto.ConfigFileReq
if err := c.ShouldBind(&req); err == nil {
var configFileService service.ConfigFileService
if req.Type == "one" {
//有文件内容
configFile, err2 := configFileService.SearchOneConfigFile(&req, user_id)
if err2 == nil {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": configFile})
} else {
c.JSON(http.StatusOK, gin.H{"code": proto.SearchConfigFileFailed, "msg": "info:" + err2.Error(), "data": configFile})
}
} else if req.Type == "all" {
configFileList, err3 := configFileService.SearchAllConfigFile(user_id)
if err3 == nil {
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "msg": "success", "data": configFileList})
} else {
c.JSON(http.StatusOK, gin.H{"code": proto.SearchConfigFileFailed, "msg": "info:" + err3.Error(), "data": configFileList})
}
} else {
c.JSON(http.StatusOK, gin.H{"error": "search config 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 UploadFileV2(c *gin.Context) {
//先查看是否有权限
id, _ := c.Get("id")
id1 := int(id.(float64))
var req proto.FileUploadReq
//获取post form参数
if err := c.ShouldBind(&req); err == nil {
//检查参数
if err2 := service.CheckUploadRequestParameters(&req); err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter check error:" + err2.Error(), "code": proto.ParameterError, "message": "failed"})
return
}
//
} else {
c.JSON(http.StatusOK, gin.H{"error": "upload form parameter decode error:" + err.Error(), "code": proto.ParameterError, "message": "failed"})
return
}
//从请求头获取upload_type
uploadType := c.PostForm("upload_type")
authType := c.PostForm("auth_type")
md5_ := c.PostForm("md5")
if uploadType == "" {
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
return
}
2025-03-30 16:38:09 +08:00
//查看用户上传的所有文件大小是否超过限制
userFileSpace := dao.GetUserFileSpace(id1)
user := service.GetUserByIDWithCache(id1)
if userFileSpace.TotalSize > proto.UserMaxUploadSize && user.Role != "admin" {
c.JSON(http.StatusOK, gin.H{"error": "user file space is full", "code": proto.NoUploadPermissions, "message": "failed"})
return
}
//上传文件
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": "upload file failed", "code": proto.UploadFileFailed, "message": "failed"})
return
}
2025-03-30 16:38:09 +08:00
//计算文件md5值
if md5_ == "" {
file_, _ := file.Open()
md5_ = service.CalculateFileMd5(file_)
if md5_ == "" {
c.JSON(http.StatusOK, gin.H{"error": "计算文件MD5值失败", "code": proto.UploadFileFailed, "message": "failed"})
return
}
}
//查询文件是否已存在
fileExist := dao.FindFileByMd5(md5_)
if fileExist.ID != 0 {
fileExist.FilePath = ""
2025-03-30 16:38:09 +08:00
//添加用户文件
fileAuth := service.CreateUserFile(id1, fileExist.FileName, int(fileExist.ID), uploadType)
if fileAuth.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "save user file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileExist, "file_auth": fileAuth})
return
}
//保存文件
filePath, fileStoreName, err := service.SaveFile(c, file, uploadType)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": "save file failed", "code": proto.SaveFileFailed, "message": "failed"})
return
}
//保存文件信息
fileSize := int(file.Size)
fileName := file.Filename
fileType := file.Header.Get("file_type")
var auth_type_ bool
if authType == "public" || authType == "" {
auth_type_ = false
} else if authType == "private" {
auth_type_ = true
}
file_record := dao.CreateFile(fileStoreName, fileName, fileType, filePath, md5_, fileSize, id1, auth_type_)
if file_record.ID == 0 {
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
return
}
2025-03-28 19:53:25 +08:00
//添加用户文件
fileAuth := service.CreateUserFile(id1, file_record.FileName, int(file_record.ID), uploadType)
2025-03-28 19:58:46 +08:00
if fileAuth.ID == 0 {
2025-03-28 19:53:25 +08:00
c.JSON(http.StatusOK, gin.H{"error": "save user file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
return
}
file_record.FilePath = ""
2025-03-28 19:53:25 +08:00
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": file_record, "file_auth": fileAuth})
}
func FindFileContent(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 {
fileContent, err2 := service.FindFileContent(userId, &req)
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 = fileContent
}
} else {
resp.Code = proto.ParameterError
resp.Message = "upload form parameter decode error:" + err.Error()
}
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
2025-05-21 14:10:59 +08:00
resp.Message = "create 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 UpdateFileContent(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.UpdateFileContent(userId, req.FileID, req.FileContent)
if err2 != nil {
resp.Code = proto.ParameterError
resp.Message = "update 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)
}
func ConvertMsgToFile(c *gin.Context) {
id, _ := c.Get("id")
userId := int(id.(float64))
var req proto.MessageConvertFileReq
var resp proto.GenerateResp
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 = "message convert file form parameter decode error:" + err.Error()
}
c.JSON(http.StatusOK, resp)
}