-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_service.go
More file actions
108 lines (94 loc) · 2.64 KB
/
upload_service.go
File metadata and controls
108 lines (94 loc) · 2.64 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package services
import (
"appointment-api/internal/config"
"context"
"fmt"
"mime/multipart"
"os"
"path/filepath"
"strings"
"time"
"github.com/cloudinary/cloudinary-go/v2"
"github.com/cloudinary/cloudinary-go/v2/api"
"github.com/cloudinary/cloudinary-go/v2/api/uploader"
)
type UploadService interface {
UploadImage(file multipart.File, filename string, folder string) (*UploadResult, error)
DeleteImage(publicID string) error
}
type UploadResult struct {
PublicID string `json:"public_id"`
URL string `json:"url"`
SecureURL string `json:"secure_url"`
Format string `json:"format"`
Width int `json:"width"`
Height int `json:"height"`
Bytes int `json:"bytes"`
}
type uploadService struct {
cloudinary *cloudinary.Cloudinary
}
func NewUploadService(cfg *config.Config) (UploadService, error) {
// 2.x: Bağlantı için CLOUDINARY_URL kullan
cld, err := cloudinary.NewFromURL(os.Getenv("CLOUDINARY_URL"))
if err != nil {
return nil, fmt.Errorf("failed to initialize Cloudinary: %v", err)
}
return &uploadService{
cloudinary: cld,
}, nil
}
func (s *uploadService) UploadImage(file multipart.File, filename string, folder string) (*UploadResult, error) {
ctx := context.Background()
// Validate file extension
ext := strings.ToLower(filepath.Ext(filename))
allowedExts := map[string]bool{
".jpg": true,
".jpeg": true,
".png": true,
".gif": true,
".webp": true,
}
if !allowedExts[ext] {
return nil, fmt.Errorf("unsupported file format: %s", ext)
}
// Generate unique public ID
publicID := fmt.Sprintf("%s/%s_%d", folder,
strings.TrimSuffix(filename, filepath.Ext(filename)),
time.Now().Unix())
uploadParams := uploader.UploadParams{
PublicID: publicID,
Folder: folder,
ResourceType: "image",
Overwrite: api.Bool(true),
}
uploadResult, err := s.cloudinary.Upload.Upload(ctx, file, uploadParams)
if err != nil {
return nil, fmt.Errorf("failed to upload to Cloudinary: %v", err)
}
result := &UploadResult{
PublicID: uploadResult.PublicID,
URL: uploadResult.URL,
SecureURL: uploadResult.SecureURL,
Format: uploadResult.Format,
Width: uploadResult.Width,
Height: uploadResult.Height,
Bytes: uploadResult.Bytes,
}
return result, nil
}
func (s *uploadService) DeleteImage(publicID string) error {
ctx := context.Background()
if publicID == "" {
return fmt.Errorf("public ID is required")
}
deleteParams := uploader.DestroyParams{
PublicID: publicID,
ResourceType: "image",
}
_, err := s.cloudinary.Upload.Destroy(ctx, deleteParams)
if err != nil {
return fmt.Errorf("failed to delete from Cloudinary: %v", err)
}
return nil
}