v0.0.28 Добавлена модерация загруженного в S3 изображения по url перед сохранением в бд
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f86590570c
commit
f77b03bee1
@ -11,6 +11,7 @@ import (
|
|||||||
"tailly_back_v2/internal/repository"
|
"tailly_back_v2/internal/repository"
|
||||||
"tailly_back_v2/internal/utils"
|
"tailly_back_v2/internal/utils"
|
||||||
"tailly_back_v2/pkg/S3"
|
"tailly_back_v2/pkg/S3"
|
||||||
|
"tailly_back_v2/pkg/moderation"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/99designs/gqlgen/graphql"
|
"github.com/99designs/gqlgen/graphql"
|
||||||
@ -109,6 +110,23 @@ func (s *postService) Create(ctx context.Context, authorID int, title string, co
|
|||||||
|
|
||||||
imageURL := fmt.Sprintf("https://s3.regru.cloud/tailly/%s", s3Key)
|
imageURL := fmt.Sprintf("https://s3.regru.cloud/tailly/%s", s3Key)
|
||||||
|
|
||||||
|
modClient, err := moderation.NewModerationClient("tailly_censor:50051")
|
||||||
|
if err != nil {
|
||||||
|
S3.DeleteFromS3(imageURL) // удаляем если ошибка модерации
|
||||||
|
return nil, fmt.Errorf("%s: failed to create moderation client: %w", op, err)
|
||||||
|
}
|
||||||
|
defer modClient.Close()
|
||||||
|
|
||||||
|
allowed, err := modClient.CheckImageURL(ctx, imageURL)
|
||||||
|
if err != nil {
|
||||||
|
S3.DeleteFromS3(imageURL)
|
||||||
|
return nil, fmt.Errorf("%s: image moderation failed: %w", op, err)
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
S3.DeleteFromS3(imageURL)
|
||||||
|
return nil, errors.New("image rejected by moderation service")
|
||||||
|
}
|
||||||
|
|
||||||
post := &domain.Post{
|
post := &domain.Post{
|
||||||
Title: title,
|
Title: title,
|
||||||
Content: imageURL,
|
Content: imageURL,
|
||||||
|
|||||||
@ -3,8 +3,9 @@ package moderation
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
pb "tailly_back_v2/pkg/moderation/proto"
|
pb "tailly_back_v2/pkg/moderation/proto"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ModerationClient struct {
|
type ModerationClient struct {
|
||||||
@ -38,3 +39,12 @@ func (c *ModerationClient) CheckImage(ctx context.Context, imageData []byte) (bo
|
|||||||
func (c *ModerationClient) Close() {
|
func (c *ModerationClient) Close() {
|
||||||
c.conn.Close()
|
c.conn.Close()
|
||||||
}
|
}
|
||||||
|
func (c *ModerationClient) CheckImageURL(ctx context.Context, imageURL string) (bool, error) {
|
||||||
|
resp, err := c.client.CheckImage(ctx, &pb.ImageRequest{
|
||||||
|
ImageUrl: imageURL, // ← Передаем URL вместо данных!
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return resp.OverallDecision == "allowed", nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user