mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-05 00:43:05 +08:00
增加文件相关API
This commit is contained in:
@@ -64,13 +64,16 @@ func (this *FileDAO) FindEnabledFile(tx *dbs.Tx, id int64) (*File, error) {
|
||||
}
|
||||
|
||||
// 创建文件
|
||||
func (this *FileDAO) CreateFile(tx *dbs.Tx, businessType, description string, filename string, size int64) (int64, error) {
|
||||
func (this *FileDAO) CreateFile(tx *dbs.Tx, adminId int64, userId int64, businessType, description string, filename string, size int64, isPublic bool) (int64, error) {
|
||||
op := NewFileOperator()
|
||||
op.AdminId = adminId
|
||||
op.UserId = userId
|
||||
op.Type = businessType
|
||||
op.Description = description
|
||||
op.State = FileStateEnabled
|
||||
op.Size = size
|
||||
op.Filename = filename
|
||||
op.IsPublic = isPublic
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
||||
@@ -13,6 +13,7 @@ type File struct {
|
||||
Type string `field:"type"` // 类型
|
||||
State uint8 `field:"state"` // 状态
|
||||
IsFinished uint8 `field:"isFinished"` // 是否已完成上传
|
||||
IsPublic uint8 `field:"isPublic"` // 是否可以公开访问
|
||||
}
|
||||
|
||||
type FileOperator struct {
|
||||
@@ -27,6 +28,7 @@ type FileOperator struct {
|
||||
Type interface{} // 类型
|
||||
State interface{} // 状态
|
||||
IsFinished interface{} // 是否已完成上传
|
||||
IsPublic interface{} // 是否可以公开访问
|
||||
}
|
||||
|
||||
func NewFileOperator() *FileOperator {
|
||||
|
||||
@@ -55,7 +55,7 @@ func (this *BaseService) ValidateAdminAndUser(ctx context.Context, requireAdminI
|
||||
}
|
||||
case rpcutils.UserTypeUser:
|
||||
userId = reqUserId
|
||||
if userId <= 0 {
|
||||
if requireUserId >= 0 && userId <= 0 {
|
||||
err = errors.New("invalid 'userId'")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ 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"
|
||||
)
|
||||
|
||||
@@ -12,17 +11,50 @@ type FileService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 查找文件
|
||||
func (this *FileService) FindEnabledFile(ctx context.Context, req *pb.FindEnabledFileRequest) (*pb.FindEnabledFileResponse, error) {
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, -1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
file, err := models.SharedFileDAO.FindEnabledFile(tx, req.FileId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if file == nil {
|
||||
return &pb.FindEnabledFileResponse{File: nil}, nil
|
||||
}
|
||||
|
||||
if file.IsPublic != 1 {
|
||||
// 校验权限
|
||||
if userId > 0 && int64(file.UserId) != userId {
|
||||
return nil, this.PermissionError()
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.FindEnabledFileResponse{
|
||||
File: &pb.File{
|
||||
Id: int64(file.Id),
|
||||
Filename: file.Filename,
|
||||
Size: int64(file.Size),
|
||||
CreatedAt: int64(file.CreatedAt),
|
||||
IsPublic: file.IsPublic == 1,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 创建文件
|
||||
func (this *FileService) CreateFile(ctx context.Context, req *pb.CreateFileRequest) (*pb.CreateFileResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
fileId, err := models.SharedFileDAO.CreateFile(tx, "ipLibrary", "", req.Filename, req.Size)
|
||||
fileId, err := models.SharedFileDAO.CreateFile(tx, adminId, userId, "ipLibrary", "", req.Filename, req.Size, req.IsPublic)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -31,8 +63,7 @@ func (this *FileService) CreateFile(ctx context.Context, req *pb.CreateFileReque
|
||||
|
||||
// 将文件置为已完成
|
||||
func (this *FileService) UpdateFileFinished(ctx context.Context, req *pb.UpdateFileFinishedRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -32,11 +32,13 @@ func (this *FileChunkService) CreateFileChunk(ctx context.Context, req *pb.Creat
|
||||
// 获取的一个文件的所有片段IDs
|
||||
func (this *FileChunkService) FindAllFileChunkIds(ctx context.Context, req *pb.FindAllFileChunkIdsRequest) (*pb.FindAllFileChunkIdsResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode, rpcutils.UserTypeAdmin)
|
||||
_, _, err := this.ValidateAdminAndUser(ctx, 0, -1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 校验用户
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
chunkIds, err := models.SharedFileChunkDAO.FindAllFileChunkIds(tx, req.FileId)
|
||||
@@ -49,11 +51,13 @@ func (this *FileChunkService) FindAllFileChunkIds(ctx context.Context, req *pb.F
|
||||
// 下载文件片段
|
||||
func (this *FileChunkService) DownloadFileChunk(ctx context.Context, req *pb.DownloadFileChunkRequest) (*pb.DownloadFileChunkResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode, rpcutils.UserTypeAdmin)
|
||||
_, _, err := this.ValidateAdminAndUser(ctx, 0, -1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO 校验用户
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
chunk, err := models.SharedFileChunkDAO.FindFileChunk(tx, req.FileChunkId)
|
||||
|
||||
Reference in New Issue
Block a user