2020-11-04 15:51:32 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 文件相关服务
|
|
|
|
|
type FileService struct {
|
2020-11-24 15:02:44 +08:00
|
|
|
BaseService
|
2020-11-04 15:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-25 21:07:48 +08:00
|
|
|
// 查找文件
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 15:51:32 +08:00
|
|
|
// 创建文件
|
|
|
|
|
func (this *FileService) CreateFile(ctx context.Context, req *pb.CreateFileRequest) (*pb.CreateFileResponse, error) {
|
2021-02-25 21:07:48 +08:00
|
|
|
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-11-04 15:51:32 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-02-25 21:07:48 +08:00
|
|
|
fileId, err := models.SharedFileDAO.CreateFile(tx, adminId, userId, "ipLibrary", "", req.Filename, req.Size, req.IsPublic)
|
2020-11-04 15:51:32 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &pb.CreateFileResponse{FileId: fileId}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将文件置为已完成
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *FileService) UpdateFileFinished(ctx context.Context, req *pb.UpdateFileFinishedRequest) (*pb.RPCSuccess, error) {
|
2021-02-25 21:07:48 +08:00
|
|
|
_, err := this.ValidateAdmin(ctx, 0)
|
2020-11-04 15:51:32 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
err = models.SharedFileDAO.UpdateFileIsFinished(tx, req.FileId)
|
2020-11-04 15:51:32 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-11-04 15:51:32 +08:00
|
|
|
}
|