Files
EdgeAPI/internal/rpc/services/service_file_chunk.go

82 lines
2.2 KiB
Go
Raw Permalink Normal View History

2020-11-04 15:51:32 +08:00
package services
import (
"context"
2024-07-27 14:15:25 +08:00
2020-11-04 15:51:32 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
2020-11-04 15:51:32 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
2021-07-11 18:05:57 +08:00
// FileChunkService 文件片段相关服务
2020-11-04 15:51:32 +08:00
type FileChunkService struct {
BaseService
2020-11-04 15:51:32 +08:00
}
2021-07-11 18:05:57 +08:00
// CreateFileChunk 创建文件片段
2020-11-04 15:51:32 +08:00
func (this *FileChunkService) CreateFileChunk(ctx context.Context, req *pb.CreateFileChunkRequest) (*pb.CreateFileChunkResponse, error) {
// 校验请求
2022-09-17 16:07:37 +08:00
_, userId, err := this.ValidateAdminAndUser(ctx, false)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2022-07-22 15:05:30 +08:00
var tx = this.NullTx()
// 检查权限
if userId > 0 {
err = models.SharedFileDAO.CheckUserFile(tx, userId, req.FileId)
if err != nil {
return nil, err
}
}
chunkId, err := models.SharedFileChunkDAO.CreateFileChunk(tx, req.FileId, req.Data)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
return &pb.CreateFileChunkResponse{FileChunkId: chunkId}, nil
}
2021-07-11 18:05:57 +08:00
// FindAllFileChunkIds 获取的一个文件的所有片段IDs
2020-11-04 15:51:32 +08:00
func (this *FileChunkService) FindAllFileChunkIds(ctx context.Context, req *pb.FindAllFileChunkIdsRequest) (*pb.FindAllFileChunkIdsResponse, error) {
// 校验请求
_, _, err := this.ValidateNodeId(ctx, rpcutils.UserTypeNode, rpcutils.UserTypeDNS, rpcutils.UserTypeAdmin, rpcutils.UserTypeUser)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2022-07-22 15:05:30 +08:00
var tx = this.NullTx()
// 校验用户
// TODO
chunkIds, err := models.SharedFileChunkDAO.FindAllFileChunkIds(tx, req.FileId)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
return &pb.FindAllFileChunkIdsResponse{FileChunkIds: chunkIds}, nil
}
2021-07-11 18:05:57 +08:00
// DownloadFileChunk 下载文件片段
2020-11-04 15:51:32 +08:00
func (this *FileChunkService) DownloadFileChunk(ctx context.Context, req *pb.DownloadFileChunkRequest) (*pb.DownloadFileChunkResponse, error) {
// 校验请求
_, _, err := this.ValidateNodeId(ctx, rpcutils.UserTypeNode, rpcutils.UserTypeDNS, rpcutils.UserTypeAdmin, rpcutils.UserTypeUser)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2021-02-25 21:07:48 +08:00
// TODO 校验用户
2022-07-22 15:05:30 +08:00
var tx = this.NullTx()
chunk, err := models.SharedFileChunkDAO.FindFileChunk(tx, req.FileChunkId)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
if chunk == nil {
return &pb.DownloadFileChunkResponse{FileChunk: nil}, nil
}
2022-03-22 19:30:30 +08:00
return &pb.DownloadFileChunkResponse{FileChunk: &pb.FileChunk{Data: chunk.Data}}, nil
2020-11-04 15:51:32 +08:00
}