v0.0.11 Реализован функционал удаления поста и изображения из S3
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
f52b782fe3
commit
3fb0d5dacd
@ -139,6 +139,7 @@ type Mutation {
|
||||
|
||||
# Повторная отправка подтверждения email
|
||||
resendEmailConfirmation: Boolean!
|
||||
deletePost(id: Int!): Boolean!
|
||||
}
|
||||
|
||||
type Subscription {
|
||||
|
||||
@ -13,6 +13,7 @@ import (
|
||||
"tailly_back_v2/internal/domain"
|
||||
"tailly_back_v2/internal/repository"
|
||||
"tailly_back_v2/internal/utils"
|
||||
"tailly_back_v2/pkg/S3"
|
||||
"tailly_back_v2/pkg/moderation"
|
||||
"time"
|
||||
|
||||
@ -212,5 +213,18 @@ func (s *postService) Update(ctx context.Context, id int, title, content string)
|
||||
|
||||
// Удаление поста
|
||||
func (s *postService) Delete(ctx context.Context, id int) error {
|
||||
|
||||
post, err := s.postRepo.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrPostNotFound) {
|
||||
return errors.New("post not found")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = S3.DeleteFromS3(post.Content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.postRepo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
52
pkg/S3/s3.go
Normal file
52
pkg/S3/s3.go
Normal file
@ -0,0 +1,52 @@
|
||||
package S3
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func DeleteFromS3(imageURL string) error {
|
||||
const op = "s3.DeleteFromS3"
|
||||
|
||||
parts := strings.SplitN(strings.TrimPrefix(imageURL, "https://s3.regru.cloud/"), "/", 2)
|
||||
if len(parts) != 2 {
|
||||
return fmt.Errorf("%s: invalid S3 URL format", op)
|
||||
}
|
||||
bucket := parts[0]
|
||||
key := parts[1]
|
||||
|
||||
sess := session.Must(session.NewSession(&aws.Config{
|
||||
Region: aws.String("ru-central1"),
|
||||
Endpoint: aws.String("https://s3.regru.cloud"),
|
||||
S3ForcePathStyle: aws.Bool(true),
|
||||
Credentials: credentials.NewStaticCredentials(
|
||||
"TJ946G2S1Z5FEI3I7DQQ",
|
||||
"C2H2aITHRDpek8H921yhnrINZwDoADsjW3F6HURl",
|
||||
"",
|
||||
),
|
||||
}))
|
||||
|
||||
svc := s3.New(sess)
|
||||
|
||||
_, err := svc.DeleteObject(&s3.DeleteObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: failed to delete object from S3: %w", op, err)
|
||||
}
|
||||
|
||||
err = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: failed to wait for object deletion: %w", op, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user