Files
EdgeAPI/internal/db/models/file_dao.go

111 lines
2.3 KiB
Go
Raw Permalink Normal View History

2020-09-13 20:37:28 +08:00
package models
import (
2022-07-17 11:00:14 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2020-09-13 20:37:28 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
FileStateEnabled = 1 // 已启用
FileStateDisabled = 0 // 已禁用
)
type FileDAO dbs.DAO
func NewFileDAO() *FileDAO {
return dbs.NewDAO(&FileDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeFiles",
Model: new(File),
PkName: "id",
},
}).(*FileDAO)
}
2020-10-13 20:05:13 +08:00
var SharedFileDAO *FileDAO
func init() {
dbs.OnReady(func() {
SharedFileDAO = NewFileDAO()
})
}
2020-09-13 20:37:28 +08:00
2022-07-17 11:00:14 +08:00
// EnableFile 启用条目
func (this *FileDAO) EnableFile(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
2020-09-13 20:37:28 +08:00
Pk(id).
Set("state", FileStateEnabled).
Update()
return err
}
2022-07-17 11:00:14 +08:00
// DisableFile 禁用条目
func (this *FileDAO) DisableFile(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
2020-09-13 20:37:28 +08:00
Pk(id).
Set("state", FileStateDisabled).
Update()
return err
}
2022-07-17 11:00:14 +08:00
// FindEnabledFile 查找启用中的条目
func (this *FileDAO) FindEnabledFile(tx *dbs.Tx, id int64) (*File, error) {
result, err := this.Query(tx).
2020-09-13 20:37:28 +08:00
Pk(id).
Attr("state", FileStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*File), err
}
2022-07-17 11:00:14 +08:00
// CreateFile 创建文件
func (this *FileDAO) CreateFile(tx *dbs.Tx, adminId int64, userId int64, businessType string, description string, filename string, size int64, mimeType string, isPublic bool) (int64, error) {
2022-07-17 11:00:14 +08:00
var op = NewFileOperator()
2021-02-25 21:07:48 +08:00
op.AdminId = adminId
op.UserId = userId
2020-09-13 20:37:28 +08:00
op.Type = businessType
op.Description = description
op.State = FileStateEnabled
2020-11-04 15:51:32 +08:00
op.Size = size
2020-09-13 20:37:28 +08:00
op.Filename = filename
2021-02-25 21:07:48 +08:00
op.IsPublic = isPublic
2022-07-17 11:00:14 +08:00
op.Code = utils.Sha1RandomString()
op.MimeType = mimeType
err := this.Save(tx, op)
2020-09-13 20:37:28 +08:00
if err != nil {
return 0, err
}
2020-11-04 15:51:32 +08:00
return types.Int64(op.Id), nil
2020-09-13 20:37:28 +08:00
}
// CheckUserFile 检查用户ID
func (this *FileDAO) CheckUserFile(tx *dbs.Tx, userId int64, fileId int64) error {
b, err := this.Query(tx).
Pk(fileId).
Attr("userId", userId).
Exist()
if err != nil {
return err
}
if !b {
return ErrNotFound
}
return nil
}
2022-07-17 11:00:14 +08:00
// UpdateFileIsFinished 将文件置为已完成
func (this *FileDAO) UpdateFileIsFinished(tx *dbs.Tx, fileId int64) error {
_, err := this.Query(tx).
2020-11-04 15:51:32 +08:00
Pk(fileId).
Set("isFinished", true).
Update()
return err
2020-09-13 20:37:28 +08:00
}