mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-06 23:45:47 +08:00
增加ip2region库管理
This commit is contained in:
42
internal/rpc/services/service_file.go
Normal file
42
internal/rpc/services/service_file.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// 文件相关服务
|
||||
type FileService struct {
|
||||
}
|
||||
|
||||
// 创建文件
|
||||
func (this *FileService) CreateFile(ctx context.Context, req *pb.CreateFileRequest) (*pb.CreateFileResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileId, err := models.SharedFileDAO.CreateFile("ipLibrary", "", req.Filename, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateFileResponse{FileId: fileId}, nil
|
||||
}
|
||||
|
||||
// 将文件置为已完成
|
||||
func (this *FileService) UpdateFileFinished(ctx context.Context, req *pb.UpdateFileFinishedRequest) (*pb.RPCUpdateSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedFileDAO.UpdateFileIsFinished(req.FileId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rpcutils.RPCUpdateSuccess()
|
||||
}
|
||||
60
internal/rpc/services/service_file_chunk.go
Normal file
60
internal/rpc/services/service_file_chunk.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// 文件片段相关服务
|
||||
type FileChunkService struct {
|
||||
}
|
||||
|
||||
// 创建文件片段
|
||||
func (this *FileChunkService) CreateFileChunk(ctx context.Context, req *pb.CreateFileChunkRequest) (*pb.CreateFileChunkResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
chunkId, err := models.SharedFileChunkDAO.CreateFileChunk(req.FileId, req.Data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateFileChunkResponse{FileChunkId: chunkId}, nil
|
||||
}
|
||||
|
||||
// 获取的一个文件的所有片段IDs
|
||||
func (this *FileChunkService) FindAllFileChunkIds(ctx context.Context, req *pb.FindAllFileChunkIdsRequest) (*pb.FindAllFileChunkIdsResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
chunkIds, err := models.SharedFileChunkDAO.FindAllFileChunkIds(req.FileId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.FindAllFileChunkIdsResponse{FileChunkIds: chunkIds}, nil
|
||||
}
|
||||
|
||||
// 下载文件片段
|
||||
func (this *FileChunkService) DownloadFileChunk(ctx context.Context, req *pb.DownloadFileChunkRequest) (*pb.DownloadFileChunkResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
chunk, err := models.SharedFileChunkDAO.FindFileChunk(req.FileChunkId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if chunk == nil {
|
||||
return &pb.DownloadFileChunkResponse{FileChunk: nil}, nil
|
||||
}
|
||||
return &pb.DownloadFileChunkResponse{FileChunk: &pb.FileChunk{Data: []byte(chunk.Data)}}, nil
|
||||
}
|
||||
163
internal/rpc/services/service_ip_library.go
Normal file
163
internal/rpc/services/service_ip_library.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// IP库服务
|
||||
type IPLibraryService struct {
|
||||
}
|
||||
|
||||
// 创建IP库
|
||||
func (this *IPLibraryService) CreateIPLibrary(ctx context.Context, req *pb.CreateIPLibraryRequest) (*pb.CreateIPLibraryResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ipLibraryId, err := models.SharedIPLibraryDAO.CreateIPLibrary(req.Type, req.FileId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.CreateIPLibraryResponse{
|
||||
IpLibraryId: ipLibraryId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 查找单个IP库
|
||||
func (this *IPLibraryService) FindEnabledIPLibrary(ctx context.Context, req *pb.FindEnabledIPLibraryRequest) (*pb.FindEnabledIPLibraryResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ipLibrary, err := models.SharedIPLibraryDAO.FindEnabledIPLibrary(req.IpLibraryId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ipLibrary == nil {
|
||||
return &pb.FindEnabledIPLibraryResponse{IpLibrary: nil}, nil
|
||||
}
|
||||
|
||||
// 文件相关
|
||||
var pbFile *pb.File = nil
|
||||
file, err := models.SharedFileDAO.FindEnabledFile(int64(ipLibrary.FileId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if file != nil {
|
||||
pbFile = &pb.File{
|
||||
Id: int64(file.Id),
|
||||
Filename: file.Filename,
|
||||
Size: int64(file.Size),
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.FindEnabledIPLibraryResponse{
|
||||
IpLibrary: &pb.IPLibrary{
|
||||
Id: int64(ipLibrary.Id),
|
||||
Type: ipLibrary.Type,
|
||||
File: pbFile,
|
||||
CreatedAt: int64(ipLibrary.CreatedAt),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 查找最新的IP库
|
||||
func (this *IPLibraryService) FindLatestIPLibraryWithType(ctx context.Context, req *pb.FindLatestIPLibraryWithTypeRequest) (*pb.FindLatestIPLibraryWithTypeResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ipLibrary, err := models.SharedIPLibraryDAO.FindLatestIPLibraryWithType(req.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ipLibrary == nil {
|
||||
return &pb.FindLatestIPLibraryWithTypeResponse{IpLibrary: nil}, nil
|
||||
}
|
||||
|
||||
// 文件相关
|
||||
var pbFile *pb.File = nil
|
||||
file, err := models.SharedFileDAO.FindEnabledFile(int64(ipLibrary.FileId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if file != nil {
|
||||
pbFile = &pb.File{
|
||||
Id: int64(file.Id),
|
||||
Filename: file.Filename,
|
||||
Size: int64(file.Size),
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.FindLatestIPLibraryWithTypeResponse{
|
||||
IpLibrary: &pb.IPLibrary{
|
||||
Id: int64(ipLibrary.Id),
|
||||
Type: ipLibrary.Type,
|
||||
File: pbFile,
|
||||
CreatedAt: int64(ipLibrary.CreatedAt),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 列出某个类型的所有IP库
|
||||
func (this *IPLibraryService) FindAllEnabledIPLibrariesWithType(ctx context.Context, req *pb.FindAllEnabledIPLibrariesWithTypeRequest) (*pb.FindAllEnabledIPLibrariesWithTypeResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ipLibraries, err := models.SharedIPLibraryDAO.FindAllEnabledIPLibrariesWithType(req.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []*pb.IPLibrary{}
|
||||
for _, library := range ipLibraries {
|
||||
// 文件相关
|
||||
var pbFile *pb.File = nil
|
||||
file, err := models.SharedFileDAO.FindEnabledFile(int64(library.FileId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if file != nil {
|
||||
pbFile = &pb.File{
|
||||
Id: int64(file.Id),
|
||||
Filename: file.Filename,
|
||||
Size: int64(file.Size),
|
||||
}
|
||||
}
|
||||
|
||||
result = append(result, &pb.IPLibrary{
|
||||
Id: int64(library.Id),
|
||||
Type: library.Type,
|
||||
File: pbFile,
|
||||
CreatedAt: int64(library.CreatedAt),
|
||||
})
|
||||
}
|
||||
return &pb.FindAllEnabledIPLibrariesWithTypeResponse{IpLibraries: result}, nil
|
||||
}
|
||||
|
||||
// 删除IP库
|
||||
func (this *IPLibraryService) DeleteIPLibrary(ctx context.Context, req *pb.DeleteIPLibraryRequest) (*pb.RPCDeleteSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedIPLibraryDAO.DisableIPLibrary(req.IpLibraryId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rpcutils.RPCDeleteSuccess()
|
||||
}
|
||||
Reference in New Issue
Block a user