/
uploadimage.go
46 lines (36 loc) · 1014 Bytes
/
uploadimage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package usecases
import (
"context"
"errors"
"fmt"
"strings"
"github.com/daochanio/backend/api/entities"
"github.com/daochanio/backend/common"
"github.com/google/uuid"
)
type UploadImage struct {
logger common.Logger
storage Storage
}
func NewUploadImageUsecase(logger common.Logger, storage Storage) *UploadImage {
return &UploadImage{
logger,
storage,
}
}
type UploadImageInput struct {
ContentType string `validate:"oneof=image/jpeg image/png image/gif image/webp"`
Bytes *[]byte `validate:"required"`
}
func (u *UploadImage) Execute(ctx context.Context, input UploadImageInput) (entities.Image, error) {
if err := common.ValidateStruct(input); err != nil {
return entities.Image{}, err
}
id := uuid.New().String()
ext := strings.Split(input.ContentType, "/")
if len(ext) != 2 {
return entities.Image{}, errors.New("invalid content type")
}
fileName := fmt.Sprintf("%s.%s", id, ext[1])
return u.storage.UploadImage(ctx, fileName, input.ContentType, input.Bytes)
}