200 lines
6.3 KiB
Go
200 lines
6.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
|
|
pb "github.com/topfans/backend/pkg/proto/asset"
|
|
pbCommon "github.com/topfans/backend/pkg/proto/common"
|
|
"github.com/topfans/backend/pkg/models"
|
|
"github.com/topfans/backend/pkg/logger"
|
|
"github.com/topfans/backend/services/assetService/service"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// UploadMaterial 上传素材
|
|
func (p *AssetProvider) UploadMaterial(ctx context.Context, req *pb.UploadMaterialRequest) (*pb.UploadMaterialResponse, error) {
|
|
userID, starID, err := extractUserInfoFromDubboAttachments(ctx)
|
|
if err != nil {
|
|
return &pb.UploadMaterialResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_UNAUTHORIZED, Message: "unauthorized"},
|
|
}, err
|
|
}
|
|
|
|
material := &models.Material{
|
|
OssKey: req.OssKey,
|
|
OriginalName: req.OriginalName,
|
|
FileSize: req.FileSize,
|
|
MimeType: req.MimeType,
|
|
Hash: req.Hash,
|
|
CreatedBy: userID,
|
|
StarID: starID,
|
|
}
|
|
if req.Width > 0 {
|
|
w := int(req.Width)
|
|
material.Width = &w
|
|
}
|
|
if req.Height > 0 {
|
|
h := int(req.Height)
|
|
material.Height = &h
|
|
}
|
|
|
|
result, err := p.materialService.UploadMaterial(material)
|
|
if err != nil {
|
|
logger.Logger.Error("UploadMaterial failed", zap.Error(err))
|
|
return &pb.UploadMaterialResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_INTERNAL_ERROR, Message: err.Error()},
|
|
}, err
|
|
}
|
|
|
|
return &pb.UploadMaterialResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_OK, Message: "ok"},
|
|
Material: &pb.Material{
|
|
MaterialId: result.ID,
|
|
OssKey: result.OssKey,
|
|
OriginalName: result.OriginalName,
|
|
FileSize: result.FileSize,
|
|
MimeType: result.MimeType,
|
|
Hash: result.Hash,
|
|
CreatedBy: result.CreatedBy,
|
|
StarId: result.StarID,
|
|
CreatedAt: result.CreatedAt,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// BindAssetMaterials 绑定资产素材
|
|
func (p *AssetProvider) BindAssetMaterials(ctx context.Context, req *pb.BindAssetMaterialsRequest) (*pb.BindAssetMaterialsResponse, error) {
|
|
if _, _, err := extractUserInfoFromDubboAttachments(ctx); err != nil {
|
|
return &pb.BindAssetMaterialsResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_UNAUTHORIZED, Message: "unauthorized"},
|
|
}, err
|
|
}
|
|
|
|
items := make([]service.BindMaterialItem, 0, len(req.Materials))
|
|
for _, m := range req.Materials {
|
|
items = append(items, service.BindMaterialItem{
|
|
MaterialID: m.MaterialId,
|
|
MaterialType: m.MaterialType,
|
|
LayerOrder: m.LayerOrder,
|
|
PosX: doublePtr(m.PosX),
|
|
PosY: doublePtr(m.PosY),
|
|
Opacity: doublePtr(m.Opacity),
|
|
Rotation: doublePtr(m.Rotation),
|
|
ScaleX: doublePtr(m.ScaleX),
|
|
ScaleY: doublePtr(m.ScaleY),
|
|
})
|
|
}
|
|
|
|
if _, err := p.materialService.BindMaterials(req.AssetId, items); err != nil {
|
|
logger.Logger.Error("BindAssetMaterials failed", zap.Error(err))
|
|
return &pb.BindAssetMaterialsResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_INTERNAL_ERROR, Message: err.Error()},
|
|
}, err
|
|
}
|
|
|
|
return &pb.BindAssetMaterialsResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_OK, Message: "ok"},
|
|
}, nil
|
|
}
|
|
|
|
// GetAssetMaterials 获取资产素材列表
|
|
func (p *AssetProvider) GetAssetMaterials(ctx context.Context, req *pb.GetAssetMaterialsRequest) (*pb.GetAssetMaterialsResponse, error) {
|
|
if _, _, err := extractUserInfoFromDubboAttachments(ctx); err != nil {
|
|
return &pb.GetAssetMaterialsResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_UNAUTHORIZED, Message: "unauthorized"},
|
|
}, err
|
|
}
|
|
|
|
relations, err := p.materialService.GetAssetMaterials(req.AssetId)
|
|
if err != nil {
|
|
return &pb.GetAssetMaterialsResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_INTERNAL_ERROR, Message: err.Error()},
|
|
}, err
|
|
}
|
|
|
|
materials := make([]*pb.AssetMaterialRelation, 0, len(relations))
|
|
for _, rel := range relations {
|
|
pbRel := &pb.AssetMaterialRelation{
|
|
RelationId: rel.ID,
|
|
AssetId: rel.AssetID,
|
|
MaterialId: rel.MaterialID,
|
|
MaterialType: rel.MaterialType,
|
|
LayerOrder: int32(rel.LayerOrder),
|
|
}
|
|
if rel.PosX != nil {
|
|
pbRel.PosX = *rel.PosX
|
|
}
|
|
if rel.PosY != nil {
|
|
pbRel.PosY = *rel.PosY
|
|
}
|
|
if rel.Opacity != nil {
|
|
pbRel.Opacity = *rel.Opacity
|
|
}
|
|
if rel.Rotation != nil {
|
|
pbRel.Rotation = *rel.Rotation
|
|
}
|
|
if rel.ScaleX != nil {
|
|
pbRel.ScaleX = *rel.ScaleX
|
|
}
|
|
if rel.ScaleY != nil {
|
|
pbRel.ScaleY = *rel.ScaleY
|
|
}
|
|
materials = append(materials, pbRel)
|
|
}
|
|
|
|
return &pb.GetAssetMaterialsResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_OK, Message: "ok"},
|
|
Materials: materials,
|
|
}, nil
|
|
}
|
|
|
|
// UpdateMaterialLayerOrder 更新图层顺序
|
|
func (p *AssetProvider) UpdateMaterialLayerOrder(ctx context.Context, req *pb.UpdateMaterialLayerOrderRequest) (*pb.UpdateMaterialLayerOrderResponse, error) {
|
|
if _, _, err := extractUserInfoFromDubboAttachments(ctx); err != nil {
|
|
return &pb.UpdateMaterialLayerOrderResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_UNAUTHORIZED, Message: "unauthorized"},
|
|
}, err
|
|
}
|
|
|
|
orders := make(map[int64]int, len(req.Orders))
|
|
for _, o := range req.Orders {
|
|
orders[o.RelationId] = int(o.LayerOrder)
|
|
}
|
|
|
|
if err := p.materialService.UpdateLayerOrder(req.AssetId, orders); err != nil {
|
|
return &pb.UpdateMaterialLayerOrderResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_INTERNAL_ERROR, Message: err.Error()},
|
|
}, err
|
|
}
|
|
|
|
return &pb.UpdateMaterialLayerOrderResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_OK, Message: "ok"},
|
|
}, nil
|
|
}
|
|
|
|
// UnbindAssetMaterial 解绑素材
|
|
func (p *AssetProvider) UnbindAssetMaterial(ctx context.Context, req *pb.UnbindAssetMaterialRequest) (*pb.UnbindAssetMaterialResponse, error) {
|
|
if _, _, err := extractUserInfoFromDubboAttachments(ctx); err != nil {
|
|
return &pb.UnbindAssetMaterialResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_UNAUTHORIZED, Message: "unauthorized"},
|
|
}, err
|
|
}
|
|
|
|
if err := p.materialService.UnbindMaterial(req.RelationId); err != nil {
|
|
return &pb.UnbindAssetMaterialResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_INTERNAL_ERROR, Message: err.Error()},
|
|
}, err
|
|
}
|
|
|
|
return &pb.UnbindAssetMaterialResponse{
|
|
Base: &pbCommon.BaseResponse{Code: pbCommon.StatusCode_STATUS_OK, Message: "ok"},
|
|
}, nil
|
|
}
|
|
|
|
func doublePtr(v float64) *float64 {
|
|
if v == 0 {
|
|
return nil
|
|
}
|
|
return &v
|
|
}
|