v0.0.5 Добавлена логика создания поста и загрузки изображения
This commit is contained in:
parent
95bdb56e70
commit
1fd284afaf
@ -2,9 +2,15 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"tailly_back_v2/internal/domain"
|
"tailly_back_v2/internal/domain"
|
||||||
"tailly_back_v2/internal/repository"
|
"tailly_back_v2/internal/repository"
|
||||||
|
"tailly_back_v2/internal/utils"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,6 +36,9 @@ func NewPostService(postRepo repository.PostRepository) PostService {
|
|||||||
|
|
||||||
// Создание нового поста
|
// Создание нового поста
|
||||||
func (s *postService) Create(ctx context.Context, authorID int, title, content string) (*domain.Post, error) {
|
func (s *postService) Create(ctx context.Context, authorID int, title, content string) (*domain.Post, error) {
|
||||||
|
|
||||||
|
const op = "service/postService.Create"
|
||||||
|
|
||||||
// Валидация данных
|
// Валидация данных
|
||||||
if title == "" {
|
if title == "" {
|
||||||
return nil, errors.New("post title cannot be empty")
|
return nil, errors.New("post title cannot be empty")
|
||||||
@ -38,9 +47,36 @@ func (s *postService) Create(ctx context.Context, authorID int, title, content s
|
|||||||
return nil, errors.New("post content cannot be empty")
|
return nil, errors.New("post content cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(content, "data:image/jpeg;base64,") {
|
||||||
|
content = strings.TrimPrefix(content, "data:image/jpeg;base64,")
|
||||||
|
}
|
||||||
|
|
||||||
|
imgBase64, err := base64.StdEncoding.DecodeString(content)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to decode image base64", op, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
randName := utils.GenerateId()
|
||||||
|
|
||||||
|
upPath := fmt.Sprintf("./uploads/posts/images/%d", authorID)
|
||||||
|
|
||||||
|
err = os.MkdirAll(upPath, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to create directory", op, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fileName := fmt.Sprintf("%s/%s.jpg", upPath, randName)
|
||||||
|
|
||||||
|
err = os.WriteFile(fileName, imgBase64, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("failed to write file", op, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
post := &domain.Post{
|
post := &domain.Post{
|
||||||
Title: title,
|
Title: title,
|
||||||
Content: content,
|
Content: fileName,
|
||||||
AuthorID: authorID,
|
AuthorID: authorID,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
UpdatedAt: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
|
|||||||
12
internal/utils/rand_name.go
Normal file
12
internal/utils/rand_name.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateId() string {
|
||||||
|
b := make([]byte, 16)
|
||||||
|
rand.Read(b)
|
||||||
|
return fmt.Sprintf("%x", b)
|
||||||
|
}
|
||||||
BIN
uploads/posts/images/1/8eec541e6474071aa848b3ecdd992d6d.jpg
Executable file
BIN
uploads/posts/images/1/8eec541e6474071aa848b3ecdd992d6d.jpg
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 430 KiB |
BIN
uploads/posts/images/1/d59b870db2a935ca6970181e46fbdbef.jpg
Executable file
BIN
uploads/posts/images/1/d59b870db2a935ca6970181e46fbdbef.jpg
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 496 KiB |
Loading…
x
Reference in New Issue
Block a user