文件上传及获取

This commit is contained in:
junleea 2024-12-27 17:41:45 +08:00
parent 8689f78181
commit 46ad8baa3b
2 changed files with 39 additions and 3 deletions

View File

@ -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)

View File

@ -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