@@ -38,11 +38,12 @@ type Image interface {
38
38
}
39
39
40
40
type image struct {
41
- minioClient * minio.Client
42
- awsService service.AwsService
43
- workerPool * worker.Pool
44
- batchProc * batch.BatchProcessor
45
- cache service.CacheService
41
+ minioClient * minio.Client
42
+ awsService service.AwsService
43
+ imageService * service.ImageService
44
+ workerPool * worker.Pool
45
+ batchProc * batch.BatchProcessor
46
+ cache service.CacheService
46
47
}
47
48
48
49
// ImageProcessRequest represents an image processing request
@@ -62,56 +63,37 @@ type UploadUrlRequest struct {
62
63
AWSUpload bool `json:"aws_upload"`
63
64
}
64
65
65
- func NewImage (minioClient * minio.Client , awsService service.AwsService ) Image {
66
+ func NewImage (minioClient * minio.Client , awsService service.AwsService , imageService * service. ImageService ) Image {
66
67
// Initialize worker pool with 5 workers
67
68
workerConfig := worker .DefaultConfig ()
68
69
workerConfig .Workers = 5
69
70
wp := worker .NewPool (workerConfig )
70
71
wp .Start ()
71
72
73
+ img := & image {
74
+ minioClient : minioClient ,
75
+ awsService : awsService ,
76
+ imageService : imageService ,
77
+ workerPool : wp ,
78
+ cache : nil ,
79
+ }
80
+
72
81
// Initialize batch processor with default config
73
82
batchConfig := batch .DefaultConfig ()
74
83
batchConfig .BatchSize = 10
75
84
batchConfig .FlushTimeout = 5 * time .Second
76
- bp := batch .NewBatchProcessor (batchConfig , processBatch )
85
+ bp := batch .NewBatchProcessor (batchConfig , img . processBatch )
77
86
bp .Start ()
78
87
79
88
// Initialize cache service
80
89
cacheService , err := service .NewCacheService ("redis://localhost:6379" )
81
90
if err != nil {
82
91
log .Printf ("Failed to initialize cache service: %v" , err )
83
92
}
93
+ img .cache = cacheService
94
+ img .batchProc = bp
84
95
85
- return & image {
86
- minioClient : minioClient ,
87
- awsService : awsService ,
88
- workerPool : wp ,
89
- batchProc : bp ,
90
- cache : cacheService ,
91
- }
92
- }
93
-
94
- // processBatch handles batch processing of items
95
- func processBatch (items []batch.BatchItem ) []batch.BatchItem {
96
- // Process items in parallel using goroutines
97
- var wg sync.WaitGroup
98
- for i := range items {
99
- wg .Add (1 )
100
- go func (item * batch.BatchItem ) {
101
- defer wg .Done ()
102
-
103
- // Process the item based on its type
104
- switch data := item .Data .(type ) {
105
- case * ImageProcessRequest :
106
- // Process image
107
- err := processImage (data )
108
- item .Error = err
109
- item .Success = err == nil
110
- }
111
- }(& items [i ])
112
- }
113
- wg .Wait ()
114
- return items
96
+ return img
115
97
}
116
98
117
99
func (i image ) GetImage (c * fiber.Ctx ) error {
@@ -150,15 +132,15 @@ func (i image) GetImage(c *fiber.Ctx) error {
150
132
return c .SendFile ("./public/notfound.png" )
151
133
}
152
134
153
- if err , orjWidth , orjHeight := service .ImagickGetWidthHeight (getByte ); err == nil {
135
+ if err , orjWidth , orjHeight := i . imageService .ImagickGetWidthHeight (getByte ); err == nil {
154
136
c .Set ("Width" , strconv .Itoa (int (orjWidth )))
155
137
c .Set ("Height" , strconv .Itoa (int (orjHeight )))
156
138
}
157
139
158
140
c .Set ("Content-Type" , http .DetectContentType (getByte ))
159
141
160
142
if resize {
161
- resizedImage := service .ImagickResize (getByte , width , height )
143
+ resizedImage := i . imageService .ImagickResize (getByte , width , height )
162
144
// Cache the resized image
163
145
if err := i .cache .SetResizedImage (bucket , objectName , width , height , resizedImage ); err != nil {
164
146
log .Printf ("Failed to cache resized image: %v" , err )
@@ -254,7 +236,7 @@ func (i image) UploadImage(c *fiber.Ctx) error {
254
236
orjWidth uint
255
237
orjHeight uint
256
238
)
257
- if err , orjWidth , orjHeight = service .ImagickGetWidthHeight (fileContent ); err == nil {
239
+ if err , orjWidth , orjHeight = i . imageService .ImagickGetWidthHeight (fileContent ); err == nil {
258
240
c .Set ("Width" , strconv .Itoa (int (orjWidth )))
259
241
c .Set ("Height" , strconv .Itoa (int (orjHeight )))
260
242
}
@@ -263,7 +245,7 @@ func (i image) UploadImage(c *fiber.Ctx) error {
263
245
resize , width , height := service .GetWidthAndHeight (c , service .FormsType )
264
246
if resize && orjWidth > 0 && orjHeight > 0 {
265
247
width , height = service .RatioWidthHeight (orjWidth , orjHeight , width , height )
266
- fileContent = service .ImagickResize (fileContent , width , height )
248
+ fileContent = i . imageService .ImagickResize (fileContent , width , height )
267
249
if tempFile , err := service .CreateFile (fileContent ); err == nil {
268
250
defer func () {
269
251
_ = tempFile .Close ()
@@ -497,7 +479,7 @@ func (i *image) ResizeImage(c *fiber.Ctx) error {
497
479
ContentType : file .Header .Get ("Content-Type" ),
498
480
Filename : file .Filename ,
499
481
}
500
- return processImage (req )
482
+ return processImage (req , i )
501
483
},
502
484
Response : respChan ,
503
485
}
@@ -512,10 +494,33 @@ func (i *image) ResizeImage(c *fiber.Ctx) error {
512
494
return service .Response (c , fiber .StatusOK , true , "Image processed successfully" , nil )
513
495
}
514
496
497
+ // processBatch handles batch processing of items
498
+ func (i * image ) processBatch (items []batch.BatchItem ) []batch.BatchItem {
499
+ // Process items in parallel using goroutines
500
+ var wg sync.WaitGroup
501
+ for idx := range items {
502
+ wg .Add (1 )
503
+ go func (item * batch.BatchItem ) {
504
+ defer wg .Done ()
505
+
506
+ // Process the item based on its type
507
+ switch data := item .Data .(type ) {
508
+ case * ImageProcessRequest :
509
+ // Process image
510
+ err := processImage (data , i )
511
+ item .Error = err
512
+ item .Success = err == nil
513
+ }
514
+ }(& items [idx ])
515
+ }
516
+ wg .Wait ()
517
+ return items
518
+ }
519
+
515
520
// processImage handles the actual image processing
516
- func processImage (req * ImageProcessRequest ) error {
521
+ func processImage (req * ImageProcessRequest , i * image ) error {
517
522
if service .IsImageFile (req .Filename ) {
518
- resized := service .ImagickResize (req .File , req .Width , req .Height )
523
+ resized := i . imageService .ImagickResize (req .File , req .Width , req .Height )
519
524
if resized == nil {
520
525
return fmt .Errorf ("image processing failed" )
521
526
}
0 commit comments