增加ip2region库管理

This commit is contained in:
GoEdgeLab
2020-11-04 15:51:32 +08:00
parent b41624df21
commit 70eaf45aba
11 changed files with 454 additions and 73 deletions

View File

@@ -5,6 +5,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
type FileChunkDAO dbs.DAO
@@ -29,16 +30,19 @@ func init() {
}
// 创建文件Chunk
func (this *FileChunkDAO) CreateFileChunk(fileId int, data []byte) error {
func (this *FileChunkDAO) CreateFileChunk(fileId int64, data []byte) (int64, error) {
op := NewFileChunkOperator()
op.FileId = fileId
op.Data = data
_, err := this.Save(op)
return err
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
// 列出所有的文件Chunk
func (this *FileChunkDAO) FindAllFileChunks(fileId int) (result []*FileChunk, err error) {
func (this *FileChunkDAO) FindAllFileChunks(fileId int64) (result []*FileChunk, err error) {
_, err = this.Query().
Attr("fileId", fileId).
AscPk().
@@ -47,8 +51,25 @@ func (this *FileChunkDAO) FindAllFileChunks(fileId int) (result []*FileChunk, er
return
}
// 读取文件的所有片段ID
func (this *FileChunkDAO) FindAllFileChunkIds(fileId int64) ([]int64, error) {
ones, err := this.Query().
Attr("fileId", fileId).
AscPk().
ResultPk().
FindAll()
if err != nil {
return nil, err
}
result := []int64{}
for _, one := range ones {
result = append(result, int64(one.(*FileChunk).Id))
}
return result, nil
}
// 删除以前的文件
func (this *FileChunkDAO) DeleteFileChunks(fileId int) error {
func (this *FileChunkDAO) DeleteFileChunks(fileId int64) error {
if fileId <= 0 {
return errors.New("invalid fileId")
}
@@ -57,3 +78,17 @@ func (this *FileChunkDAO) DeleteFileChunks(fileId int) error {
Delete()
return err
}
// 根据ID查找片段
func (this *FileChunkDAO) FindFileChunk(chunkId int64) (*FileChunk, error) {
one, err := this.Query().
Pk(chunkId).
Find()
if err != nil {
return nil, err
}
if one == nil {
return nil, nil
}
return one.(*FileChunk), nil
}

View File

@@ -5,8 +5,6 @@ import (
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
"mime/multipart"
"os"
)
const (
@@ -66,82 +64,26 @@ func (this *FileDAO) FindEnabledFile(id int64) (*File, error) {
}
// 创建文件
func (this *FileDAO) CreateFileFromReader(businessType, description string, filename string, body *multipart.FileHeader, order int) (int, error) {
file, err := body.Open()
if err != nil {
return 0, err
}
func (this *FileDAO) CreateFile(businessType, description string, filename string, size int64) (int64, error) {
op := NewFileOperator()
op.Type = businessType
op.Description = description
op.State = FileStateEnabled
op.Size = body.Size
op.Order = order
op.Size = size
op.Filename = filename
_, err = this.Save(op)
_, err := this.Save(op)
if err != nil {
return 0, err
}
fileId := types.Int(op.Id)
// 保存chunk
buf := make([]byte, 512*1024)
for {
n, err := file.Read(buf)
if n > 0 {
err1 := SharedFileChunkDAO.CreateFileChunk(fileId, buf[:n])
if err1 != nil {
return 0, err1
}
}
if err != nil {
break
}
}
return fileId, nil
return types.Int64(op.Id), nil
}
// 创建一个空文件
func (this *FileDAO) UploadLocalFile(businessType string, localFile string, filename string) (fileId int, err error) {
reader, err := os.Open(localFile)
if err != nil {
return 0, err
}
defer func() {
_ = reader.Close()
}()
stat, err := reader.Stat()
if err != nil {
return 0, err
}
op := NewFileOperator()
op.Type = businessType
op.Filename = filename
op.Size = stat.Size()
op.State = FileStateEnabled
_, err = this.Save(op)
if err != nil {
return
}
fileId = types.Int(op.Id)
buf := make([]byte, 512*1024)
for {
n, err := reader.Read(buf)
if n > 0 {
err1 := SharedFileChunkDAO.CreateFileChunk(fileId, buf[:n])
if err1 != nil {
return 0, err1
}
}
if err != nil {
break
}
}
return fileId, nil
// 将文件置为已完成
func (this *FileDAO) UpdateFileIsFinished(fileId int64) error {
_, err := this.Query().
Pk(fileId).
Set("isFinished", true).
Update()
return err
}

View File

@@ -12,6 +12,7 @@ type File struct {
Order uint32 `field:"order"` // 排序
Type string `field:"type"` // 类型
State uint8 `field:"state"` // 状态
IsFinished uint8 `field:"isFinished"` // 是否已完成上传
}
type FileOperator struct {
@@ -25,6 +26,7 @@ type FileOperator struct {
Order interface{} // 排序
Type interface{} // 类型
State interface{} // 状态
IsFinished interface{} // 是否已完成上传
}
func NewFileOperator() *FileOperator {

View File

@@ -0,0 +1,104 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
IPLibraryStateEnabled = 1 // 已启用
IPLibraryStateDisabled = 0 // 已禁用
)
type IPLibraryDAO dbs.DAO
func NewIPLibraryDAO() *IPLibraryDAO {
return dbs.NewDAO(&IPLibraryDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeIPLibraries",
Model: new(IPLibrary),
PkName: "id",
},
}).(*IPLibraryDAO)
}
var SharedIPLibraryDAO *IPLibraryDAO
func init() {
dbs.OnReady(func() {
SharedIPLibraryDAO = NewIPLibraryDAO()
})
}
// 启用条目
func (this *IPLibraryDAO) EnableIPLibrary(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", IPLibraryStateEnabled).
Update()
return err
}
// 禁用条目
func (this *IPLibraryDAO) DisableIPLibrary(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", IPLibraryStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *IPLibraryDAO) FindEnabledIPLibrary(id int64) (*IPLibrary, error) {
result, err := this.Query().
Pk(id).
Attr("state", IPLibraryStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*IPLibrary), err
}
// 查找某个类型的IP库列表
func (this *IPLibraryDAO) FindAllEnabledIPLibrariesWithType(libraryType string) (result []*IPLibrary, err error) {
_, err = this.Query().
State(IPLibraryStateEnabled).
Attr("type", libraryType).
DescPk().
Slice(&result).
FindAll()
return
}
// 查找某个类型的最新的IP库
func (this *IPLibraryDAO) FindLatestIPLibraryWithType(libraryType string) (*IPLibrary, error) {
one, err := this.Query().
State(IPLibraryStateEnabled).
Attr("type", libraryType).
DescPk().
Find()
if err != nil {
return nil, err
}
if one == nil {
return nil, nil
}
return one.(*IPLibrary), nil
}
// 创建新的IP库
func (this *IPLibraryDAO) CreateIPLibrary(libraryType string, fileId int64) (int64, error) {
op := NewIPLibraryOperator()
op.Type = libraryType
op.FileId = fileId
op.State = IPLibraryStateEnabled
_, err := this.Save(op)
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}

View File

@@ -0,0 +1,5 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
)

View File

@@ -0,0 +1,24 @@
package models
// IP库
type IPLibrary struct {
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
FileId uint32 `field:"fileId"` // 文件ID
Type string `field:"type"` // 类型
State uint8 `field:"state"` // 状态
CreatedAt uint64 `field:"createdAt"` // 创建时间
}
type IPLibraryOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
FileId interface{} // 文件ID
Type interface{} // 类型
State interface{} // 状态
CreatedAt interface{} // 创建时间
}
func NewIPLibraryOperator() *IPLibraryOperator {
return &IPLibraryOperator{}
}

View File

@@ -0,0 +1 @@
package models