videoplayer/dao/file.go

99 lines
2.6 KiB
Go

package dao
import (
"gorm.io/gorm"
"videoplayer/proto"
)
type File struct {
gorm.Model
// 存储文件名
FileStoreName string `gorm:"column:file_store_name;type:varchar(255);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"`
FilePath string `gorm:"column:file_path"`
AuthID int `gorm:"column:auth_id"`
Md5 string `gorm:"column:md5;type:varchar(255);uniqueIndex:idx_file_name"`
}
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}
result := DB.Create(&file)
if result.Error != nil {
return File{}
}
return file
}
func DeleteFileByID(id, user int) bool {
res := DB.Model(&File{}).Where("id = ? and auth_id = ?", id, user).Delete(&File{})
if res.Error != nil {
return false
}
return true
}
func FindFileByID(id, auth_id int) File {
var file File
DB.Where("id = ? and auth_id = ?", id, auth_id).First(&file)
return file
}
func FindFileByNames(fileName string, auth_id int) File {
var file File
DB.Where("file_name = ? and auth_id = ?", fileName, auth_id).First(&file)
return file
}
func FindFileByName(fileName string) File {
var file File
DB.Where("file_store_name = ?", fileName).First(&file)
return file
}
func FindFileByAuthID(auth_id int) []File {
var files []File
DB.Where("auth_id = ?", auth_id).Find(&files)
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
}
result := DB.Model(&File{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize})
if result.Error != nil {
return false
}
return true
}
func DeleteFileByAuthID(auth_id int) bool {
res := DB.Model(&File{}).Where("auth_id = ?", auth_id).Delete(&File{})
if res.Error != nil {
return false
}
return true
}
func DeleteFileById(id int) bool {
res := DB.Model(&File{}).Where("id = ?", id).Delete(&File{})
if res.Error != nil {
return false
}
return true
}
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
}