From 46ad8baa3b3a6fa4d6f4ee67cc57f8a865eaa0c3 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Fri, 27 Dec 2024 17:41:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=8F=8A?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/file.go | 11 +++++++++-- handler/tool.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/dao/file.go b/dao/file.go index 54b79cb..2fad62a 100644 --- a/dao/file.go +++ b/dao/file.go @@ -6,6 +6,7 @@ type File struct { gorm.Model // 存储文件名 FileStoreName string `gorm:"column:file_store_name;uniqueIndex:idx_file_name"` + NeedAuth bool `gorm:"column:need_auth"` FileName string `gorm:"column:file_name"` FileSize int `gorm:"column:file_size"` FileType string `gorm:"column:file_type"` @@ -13,8 +14,8 @@ type File struct { AuthID int `gorm:"column:auth_id"` } -func CreateFile(fileStoreName, fileName, fileType, filePath string, fileSize, authID int) uint { - file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID} +func CreateFile(fileStoreName, fileName, fileType, filePath string, fileSize, authID int, NeedAuth bool) uint { + file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID, NeedAuth: NeedAuth} result := DB.Create(&file) if result.Error != nil { return 0 @@ -42,6 +43,12 @@ func FindFileByNames(fileName string, auth_id int) File { return file } +func FindFileByName(fileName string) File { + var file File + DB.Where("file_name = ?", fileName).First(&file) + return file +} + func FindFileByAuthID(auth_id int) []File { var files []File DB.Where("auth_id = ?", auth_id).Find(&files) diff --git a/handler/tool.go b/handler/tool.go index e414c89..39d512e 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -34,6 +34,7 @@ func SetUpToolGroup(router *gin.Engine) { //文件上传、下载 toolGroup.POST("/upload", UploadFile) toolGroup.GET("/download", DownloadFile) + toolGroup.GET("/file/:filename", GetFile) //文件管理 toolGroup.POST("/file_del", DelFile) //服务器、设备状态接口 @@ -97,12 +98,34 @@ func DelFile(c *gin.Context) { } +func GetFile(c *gin.Context) { + //先查看是否有权限 + filename := c.Param("filename") + if filename == "" { + c.JSON(http.StatusOK, gin.H{"error": "filename is empty", "code": proto.ParameterError, "message": "failed"}) + return + } + //查询文件信息 + file := dao.FindFileByName(filename) + if file.ID == 0 { + c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"}) + return + } + //下载文件 + if file.NeedAuth == false { + c.File(file.FilePath + "/" + file.FileStoreName) + } else { + c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "file must auth", "data": "file must auth"}) + } +} + func UploadFile(c *gin.Context) { //先查看是否有权限 id, _ := c.Get("id") id1 := int(id.(float64)) //从请求头获取upload_type uploadType := c.PostForm("upload_type") + authType := c.PostForm("auth_type") if uploadType == "" { c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"}) return @@ -130,7 +153,13 @@ func UploadFile(c *gin.Context) { fileSize := int(file.Size) fileName := file.Filename fileType := file.Header.Get("file_type") - fileID := dao.CreateFile(fileStoreName, fileName, fileType, filePath, fileSize, id1) + var auth_type_ bool + if authType == "public" || authType == "" { + auth_type_ = false + } else if authType == "private" { + auth_type_ = true + } + fileID := dao.CreateFile(fileStoreName, fileName, fileType, filePath, fileSize, id1, auth_type_) if fileID == 0 { c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"}) return