saw-go/dao/file.go

382 lines
11 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dao
import (
"StuAcaWorksAI/proto"
"gorm.io/gorm"
"log"
)
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"`
}
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"` // 分享码,用于分享时的验证,构建分享链接
}
// 存储文本文件的内容主要是pdf、docx等文件的内容其它文本文件也可以存储
type FileContent struct {
gorm.Model
FileID int `gorm:"column:file_id"` // 文件id
FileContent string `gorm:"column:file_content"` // 文件内容最大长度为1677721516M
}
type ConfigFile struct {
gorm.Model
AuthID int `gorm:"column:auth_id"`
FileName string `gorm:"column:file_name"`
FilePath string `gorm:"column:file_path"`
}
type FileAuthListResp struct {
FileAuth
FileStoreName string `gorm:"column:file_store_name;type:varchar(255);uniqueIndex:idx_file_name"`
}
type UserFileListResp struct {
FileAuth
FileStoreName string `json:"file_store_name" form:"file_store_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
}
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
}
// 通用通过用户id及类型查询
func FileUserFileList(userID int, fileType string) ([]FileAuth, []File) {
var fileAuths []FileAuth
var files []File
query := DB.Where("file_auths.auth_id = ? AND file_auths.upload_type = ?", userID, fileType).
Joins("LEFT JOIN files ON file_auths.file_id = files.id")
if proto.Config.SERVER_SQL_LOG {
query = query.Debug()
}
// 先查询 FileAuth 相关记录
resultAuth := query.Find(&fileAuths)
if resultAuth.Error != nil {
log.Printf("查询文件认证列表时出错UserID: %d, FileType: %s, 错误信息: %v", userID, fileType, resultAuth.Error)
return nil, nil
}
// 如果有 FileAuth 记录,再查询对应的 File 记录
if len(fileAuths) > 0 {
fileIDs := make([]int, 0, len(fileAuths))
for _, auth := range fileAuths {
fileIDs = append(fileIDs, auth.FileID) // 假设 FileAuth 结构体中有 FileID 字段
}
resultFiles := DB.Where("id IN ?", fileIDs).Find(&files)
if resultFiles.Error != nil {
log.Printf("查询文件列表时出错UserID: %d, FileType: %s, 错误信息: %v", userID, fileType, resultFiles.Error)
return nil, nil
}
}
return fileAuths, files
}
// 通过文件名及用户id查询(文件名模糊查询),索引不生效查询较慢
func FindFileByUserFileName(fileName string, userID int) []FileAuth {
var files []FileAuth
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("auth_id = ? and user_file_name LIKE ?", userID, "%"+fileName+"%").Find(&files)
} else {
DB.Where("auth_id = ? and user_file_name LIKE ?", userID, "%"+fileName+"%").Find(&files)
}
return files
}
// 删除文件
func DeleteFileAuthByID(id int) error {
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Delete(&FileAuth{}, id)
} else {
res = DB.Delete(&FileAuth{}, id)
}
return res.Error
}
// 修改文件名(暂时只能改文件名)
func UpdateFileAuthByID(id int, file FileAuth) error {
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Model(&FileAuth{}).Where("id = ?", id).Updates(&file)
} else {
res = DB.Model(&FileAuth{}).Where("id = ?", id).Updates(&file)
}
return res.Error
}
// 根据FileAuth的id查询
func FindFileAuthByID(id int) FileAuth {
var file FileAuth
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("id = ?", id).First(&file)
} else {
DB.Where("id = ?", id).First(&file)
}
return file
}
// 获取用户文件占用空间
type UserFileSpace struct {
TotalSize int `gorm:"column:total_size"`
}
func GetUserFileSpace(userID int) UserFileSpace {
var space UserFileSpace
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Raw("SELECT SUM(file_size) as total_size FROM file_auths left join files on file_auths.file_id = files.id where file_auths.auth_id=? AND file_auths.deleted_at IS NULL", userID).Scan(&space)
} else {
DB.Raw("SELECT SUM(file_size) as total_size FROM file_auths left join files on file_auths.file_id = files.id where file_auths.auth_id= ? AND file_auths.deleted_at IS NULL ", userID).Scan(&space)
}
return space
}
func FindFileAuthByName(fileName string, userID int) FileAuth {
var file FileAuth
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
db2.Where("user_file_name = ? and auth_id = ?", fileName, userID).First(&file)
return file
}
func FindFileContentByFileID(id int) []FileContent {
var fileContent []FileContent
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
db2.Where("file_id = ?", id).Find(&fileContent)
return fileContent
}
func CreateFileContent(fileID int, fileContent string) (uint, error) {
content := FileContent{FileID: fileID, FileContent: fileContent}
var result *gorm.DB
if proto.Config.SERVER_SQL_LOG {
result = DB.Debug().Create(&content)
} else {
result = DB.Create(&content)
}
return content.ID, result.Error
}
func DeleteFileContentByID(id int) error {
var res *gorm.DB
if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Delete(&FileContent{}, id)
} else {
res = DB.Delete(&FileContent{}, id)
}
return res.Error
}
func UpdateFileContentByID(id uint, fileContent string) (uint, error) {
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
res := db2.Model(&FileContent{}).Where("id = ?", id).Updates(FileContent{FileContent: fileContent})
return id, res.Error
}
func GetFileWillConvertContentFileList() ([]File, error) {
//获取文件内容表里没有内容的文件列表
var files []File
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
db2.Table("files").Select("files.*").Joins("LEFT JOIN file_contents ON files.id = file_contents.file_id").Where("file_contents.file_id IS NULL").Find(&files)
if db2.Error != nil {
log.Printf("查询文件列表时出错,错误信息: %v", db2.Error)
return nil, db2.Error
}
return files, nil
}
// 根据文件id查找文件内容表是否有内容
func FindFileContentByFileIDAndContentID(fileID int) (FileContent, error) {
var fileContent FileContent
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
} else {
db2 = DB
}
db2.Where("file_id = ?", fileID).First(&fileContent)
if db2.Error != nil {
log.Printf("查询文件内容时出错fileID: %d, 错误信息: %v", fileID, db2.Error)
return FileContent{}, db2.Error
}
return fileContent, nil
}