feat: add s3 blob handler(by readll all :( )

This commit is contained in:
loveuer
2024-05-05 19:11:57 +08:00
parent 410a4c0d8d
commit 105d26efe4
30 changed files with 1483 additions and 19 deletions

View File

@ -0,0 +1,57 @@
package dbs
import (
"context"
"github.com/glebarez/sqlite"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"nf-repo/internal/interfaces"
"nf-repo/internal/opt"
"os"
"path"
)
type tx struct {
db *gorm.DB
}
func (t *tx) TX(ctx context.Context) *gorm.DB {
db := t.db.Session(&gorm.Session{}).WithContext(ctx)
if opt.Debug {
db = db.Debug()
}
return db
}
func newTX(db *gorm.DB) interfaces.Database {
return &tx{db: db}
}
func Must(database interfaces.Database, err error) interfaces.Database {
if err != nil {
logrus.
WithField("path", "tx.Must").
WithField("err", err.Error()).
Panic()
}
if database == nil {
logrus.
WithField("path", "tx.Must").
WithField("err", "database is nil").
Panic()
}
return database
}
func NewSqliteTX(filepath string) (interfaces.Database, error) {
if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil {
return nil, err
}
db, err := gorm.Open(sqlite.Open(filepath), &gorm.Config{})
return newTX(db), err
}

View File

@ -0,0 +1,62 @@
package dbs
import (
"errors"
"nf-repo/internal/interfaces/enums"
"nf-repo/internal/sqlType"
"time"
)
type User struct {
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
DeletedAt int64 `json:"deleted_at" gorm:"index;column:deleted_at;default:0"`
Username string `json:"username" gorm:"column:username;type:varchar(64);unique"`
Password string `json:"-" gorm:"column:password;type:varchar(256)"`
Status enums.Status `json:"status" gorm:"column:status;default:0"`
Nickname string `json:"nickname" gorm:"column:nickname;type:varchar(64)"`
Comment string `json:"comment" gorm:"column:comment"`
Role enums.Role `json:"role" gorm:"column:role"`
Privileges sqlType.NumSlice[enums.Privilege] `json:"privileges" gorm:"column:privileges;type:bigint[]"`
CreatedById uint64 `json:"created_by_id" gorm:"column:created_by_id"`
CreatedByName string `json:"created_by_name" gorm:"column:created_by_name;type:varchar(64)"`
ActiveAt int64 `json:"active_at" gorm:"column:active_at"`
Deadline int64 `json:"deadline" gorm:"column:deadline"`
LoginAt int64 `json:"login_at" gorm:"-"`
}
func (u *User) IsValid(mustOk bool) error {
now := time.Now()
if now.UnixMilli() >= u.Deadline {
return errors.New("用户已过期")
}
if now.UnixMilli() < u.ActiveAt {
return errors.New("用户未启用")
}
if u.DeletedAt > 0 {
return errors.New("用户不存在")
}
switch u.Status {
case enums.StatusNormal:
case enums.StatusFrozen:
if mustOk {
return errors.New("用户被冻结")
}
default:
return errors.New("用户状态未知")
}
return nil
}