videoplayer/dao/file.go

180 lines
5.2 KiB
Go
Raw Normal View History

2024-08-30 11:28:27 +08:00
package dao
2024-12-27 20:27:20 +08:00
import (
"gorm.io/gorm"
"videoplayer/proto"
)
2024-08-30 11:28:27 +08:00
type File struct {
gorm.Model
// 存储文件名
2024-12-27 19:37:19 +08:00
FileStoreName string `gorm:"column:file_store_name;type:varchar(255);uniqueIndex:idx_file_name"`
2024-12-27 17:41:45 +08:00
NeedAuth bool `gorm:"column:need_auth"`
2024-08-30 11:28:27 +08:00
FileName string `gorm:"column:file_name"`
FileSize int `gorm:"column:file_size"`
FileType string `gorm:"column:file_type"`
FilePath string `gorm:"column:file_path"`
AuthID int `gorm:"column:auth_id"`
2024-12-27 20:27:20 +08:00
Md5 string `gorm:"column:md5;type:varchar(255);uniqueIndex:idx_file_name"`
2024-08-30 11:28:27 +08:00
}
type FileAuth struct {
gorm.Model
AuthID int `gorm:"column:auth_id"`
FileID int `gorm:"column:file_id"`
UserFileName string `gorm:"column:user_file_name;type:varchar(255);uniqueIndex:idx_file_name"` //对于同一个文件,不同用户有不同的文件名
UploadType string `gorm:"column:upload_type"` // 上传类型: im,avatar,file,config,config为系统文件
IsPrivate int `gorm:"column:is_private"` // 是否私有,私有文件只能自己下载或者通过分享链接下载,1为私有0为公开
ShareCode string `gorm:"column:share_code"` // 分享码,用于分享时的验证,构建分享链接
}
type ConfigFile struct {
gorm.Model
AuthID int `gorm:"column:auth_id"`
FileName string `gorm:"column:file_name"`
FilePath string `gorm:"column:file_path"`
}
2024-12-27 20:36:04 +08:00
func CreateFile(fileStoreName, fileName, fileType, filePath, md5Str string, fileSize, authID int, NeedAuth bool) File {
file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID, NeedAuth: NeedAuth, Md5: md5Str}
2024-10-04 11:37:25 +08:00
result := DB.Create(&file)
2024-08-30 11:28:27 +08:00
if result.Error != nil {
2024-12-27 20:36:04 +08:00
return File{}
2024-08-30 11:28:27 +08:00
}
2024-12-27 20:30:05 +08:00
return file
2024-08-30 11:28:27 +08:00
}
func DeleteFileByID(id, user int) bool {
2024-10-04 11:37:25 +08:00
res := DB.Model(&File{}).Where("id = ? and auth_id = ?", id, user).Delete(&File{})
2024-08-30 11:28:27 +08:00
if res.Error != nil {
return false
}
return true
}
func FindFileByID(id, auth_id int) File {
var file File
2024-10-04 11:37:25 +08:00
DB.Where("id = ? and auth_id = ?", id, auth_id).First(&file)
2024-08-30 11:28:27 +08:00
return file
}
func FindFileByNames(fileName string, auth_id int) File {
var file File
2024-10-04 11:37:25 +08:00
DB.Where("file_name = ? and auth_id = ?", fileName, auth_id).First(&file)
2024-08-30 11:28:27 +08:00
return file
}
2024-12-27 17:41:45 +08:00
func FindFileByName(fileName string) File {
var file File
2024-12-27 18:45:48 +08:00
DB.Where("file_store_name = ?", fileName).First(&file)
2024-12-27 17:41:45 +08:00
return file
}
2024-08-30 11:28:27 +08:00
func FindFileByAuthID(auth_id int) []File {
var files []File
2024-10-04 11:37:25 +08:00
DB.Where("auth_id = ?", auth_id).Find(&files)
2024-08-30 11:28:27 +08:00
return files
}
func UpdateFileByID(id, auth_id int, fileStoreName, fileName, fileType, filePath string, fileSize int) bool {
pd := FindFileByID(id, auth_id)
if pd.ID == 0 {
return false
}
2024-10-04 11:37:25 +08:00
result := DB.Model(&File{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize})
2024-08-30 11:28:27 +08:00
if result.Error != nil {
return false
}
return true
}
2024-08-30 21:46:28 +08:00
func DeleteFileByAuthID(auth_id int) bool {
2024-10-04 11:37:25 +08:00
res := DB.Model(&File{}).Where("auth_id = ?", auth_id).Delete(&File{})
2024-08-30 21:46:28 +08:00
if res.Error != nil {
return false
}
return true
}
func DeleteFileById(id int) bool {
2024-10-04 11:37:25 +08:00
res := DB.Model(&File{}).Where("id = ?", id).Delete(&File{})
2024-08-30 21:46:28 +08:00
if res.Error != nil {
return false
}
return true
}
2024-12-27 20:27:20 +08:00
func FindFileByMd5(md5 string) File {
var file File
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("md5 = ?", md5).First(&file)
} else {
DB.Where("md5 = ?", md5).First(&file)
}
return file
}
func CreateFileAuth(authID, fileID int, userFileName, uploadType string, isPrivate int, shareCode string) FileAuth {
fileAuth := FileAuth{AuthID: authID, FileID: fileID, UserFileName: userFileName, UploadType: uploadType, IsPrivate: isPrivate, ShareCode: shareCode}
var result *gorm.DB
if proto.Config.SERVER_SQL_LOG {
result = DB.Debug().Create(&fileAuth)
} else {
result = DB.Create(&fileAuth)
}
if result.Error != nil {
return FileAuth{}
}
return fileAuth
}
func CreateConfigFile(file ConfigFile) (id uint, err error) {
var result *gorm.DB
if proto.Config.SERVER_SQL_LOG {
result = DB.Debug().Create(&file)
} else {
result = DB.Create(&file)
}
return file.ID, result.Error
}
func FindConfigFileByID(id int, user_id int) ConfigFile {
var file ConfigFile
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("id = ? and auth_id = ?", id, user_id).First(&file)
} else {
DB.Where("id = ? and auth_id = ?", id, user_id).First(&file)
}
return file
}
func DeleteConfigFileByID(id int) error {
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Delete(&ConfigFile{}, id)
} else {
res = DB.Delete(&ConfigFile{}, id)
}
return res.Error
}
func UpdateConfigFileByID(id int, file ConfigFile) error {
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Model(&ConfigFile{}).Where("id = ?", id).Updates(&file)
} else {
res = DB.Model(&ConfigFile{}).Where("id = ?", id).Updates(&file)
}
return res.Error
}
func FindConfigFileByAuthID(auth_id int) []ConfigFile {
var files []ConfigFile
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("auth_id = ?", auth_id).Find(&files)
} else {
DB.Where("auth_id = ?", auth_id).Find(&files)
}
return files
}