Merge branch 'refs/heads/feat-file'
This commit is contained in:
commit
7d458f70e0
11
dao/file.go
11
dao/file.go
|
|
@ -6,6 +6,7 @@ type File struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
// 存储文件名
|
// 存储文件名
|
||||||
FileStoreName string `gorm:"column:file_store_name;uniqueIndex:idx_file_name"`
|
FileStoreName string `gorm:"column:file_store_name;uniqueIndex:idx_file_name"`
|
||||||
|
NeedAuth bool `gorm:"column:need_auth"`
|
||||||
FileName string `gorm:"column:file_name"`
|
FileName string `gorm:"column:file_name"`
|
||||||
FileSize int `gorm:"column:file_size"`
|
FileSize int `gorm:"column:file_size"`
|
||||||
FileType string `gorm:"column:file_type"`
|
FileType string `gorm:"column:file_type"`
|
||||||
|
|
@ -13,8 +14,8 @@ type File struct {
|
||||||
AuthID int `gorm:"column:auth_id"`
|
AuthID int `gorm:"column:auth_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFile(fileStoreName, fileName, fileType, filePath string, fileSize, authID int) uint {
|
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}
|
file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID, NeedAuth: NeedAuth}
|
||||||
result := DB.Create(&file)
|
result := DB.Create(&file)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return 0
|
return 0
|
||||||
|
|
@ -42,6 +43,12 @@ func FindFileByNames(fileName string, auth_id int) File {
|
||||||
return 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 {
|
func FindFileByAuthID(auth_id int) []File {
|
||||||
var files []File
|
var files []File
|
||||||
DB.Where("auth_id = ?", auth_id).Find(&files)
|
DB.Where("auth_id = ?", auth_id).Find(&files)
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ func SetUpToolGroup(router *gin.Engine) {
|
||||||
//文件上传、下载
|
//文件上传、下载
|
||||||
toolGroup.POST("/upload", UploadFile)
|
toolGroup.POST("/upload", UploadFile)
|
||||||
toolGroup.GET("/download", DownloadFile)
|
toolGroup.GET("/download", DownloadFile)
|
||||||
|
toolGroup.GET("/file/:filename", GetFile)
|
||||||
//文件管理
|
//文件管理
|
||||||
toolGroup.POST("/file_del", DelFile)
|
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) {
|
func UploadFile(c *gin.Context) {
|
||||||
//先查看是否有权限
|
//先查看是否有权限
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
id1 := int(id.(float64))
|
id1 := int(id.(float64))
|
||||||
//从请求头获取upload_type
|
//从请求头获取upload_type
|
||||||
uploadType := c.PostForm("upload_type")
|
uploadType := c.PostForm("upload_type")
|
||||||
|
authType := c.PostForm("auth_type")
|
||||||
if uploadType == "" {
|
if uploadType == "" {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
|
||||||
return
|
return
|
||||||
|
|
@ -130,7 +153,13 @@ func UploadFile(c *gin.Context) {
|
||||||
fileSize := int(file.Size)
|
fileSize := int(file.Size)
|
||||||
fileName := file.Filename
|
fileName := file.Filename
|
||||||
fileType := file.Header.Get("file_type")
|
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 {
|
if fileID == 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
var Config ConfigStruct
|
var Config ConfigStruct
|
||||||
var SigningKey = []byte{}
|
var SigningKey = []byte{}
|
||||||
var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true, "/user/sync": true} // 不需要token验证的url
|
var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true, "/user/sync": true, "/tool/file/": true} // 不需要token验证的url
|
||||||
var Per_menu_map = map[string]int{"/video/": 1, "/device/": 2, "/cid/": 3}
|
var Per_menu_map = map[string]int{"/video/": 1, "/device/": 2, "/cid/": 3}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue