saw-go/handler/file.go

263 lines
8.8 KiB
Go
Raw Normal View History

package handler
import (
"StuAcaWorksAI/dao"
"StuAcaWorksAI/proto"
"StuAcaWorksAI/service"
"github.com/gin-gonic/gin"
"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)
fileGroup.GET("/general/:filename", GetFile)
2025-03-28 19:09:34 +08:00
fileGroup.POST("/file_list", GetUserFileList)
fileGroup.POST("/file_delete", DeleteUserFile)
fileGroup.POST("/file_update", UpdateUserFile)
}
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
}
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) {
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
}
user := dao.FindUserByUserID(id1)
if user.Upload == false {
c.JSON(http.StatusOK, gin.H{"error": "no upload Permissions", "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
}
//计算文件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 = ""
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileExist})
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
}
file_record.FilePath = ""
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": file_record})
}