This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
497 lines (416 loc) · 13.2 KB
/
main.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package main
import (
"bytes"
"context"
"flag"
"fmt"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
"image/png"
"io"
"log"
"math"
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
_ "github.com/Soreil/svg"
"github.com/disintegration/imaging"
red "github.com/go-redis/redis/v8"
"github.com/go-redsync/redsync/v4"
"github.com/go-redsync/redsync/v4/redis/goredis/v8"
"github.com/minio/minio-go"
"github.com/pkg/errors"
_ "golang.org/x/image/webp"
)
// Cli Flags
var (
address = flag.String("a", "0.0.0.0:2222", "Server address")
bucketName = flag.String("b", "", "Bucket name")
endPoint = flag.String("e", "http://minio1.servers.platinbox.org:9000", "Server Endpoint")
region = flag.String("r", "fr-par", "Region")
gifsicleCmd string
)
// Define Locker
type Locker struct {
initialized bool
sync *redsync.Redsync
}
// Acquire a lock with the key.
func (l *Locker) lock(key string, w http.ResponseWriter, r *http.Request) (*redsync.Mutex, error) {
if l == nil || !l.initialized {
return nil, nil;
}
// Generate the mutex.
mutex := locker.sync.NewMutex(key)
// Try to acquire lock. If already locked, finalize the request with 503 and Retry-after.
if err := mutex.Lock(); err != nil {
w.Header().Add("Retry-After", "3")
http.Error(w, "Resource was busy, try again in 3 seconds.", http.StatusServiceUnavailable)
r.Body.Close()
return nil, err
}
// Return lock.
return mutex, nil;
}
func (l *Locker) unlock(m *redsync.Mutex) error {
if l != nil && l.initialized && m != nil {
if ok, err := m.Unlock(); (!ok || err != nil) {
return err
}
}
return nil
}
// Locker
var locker *Locker
// Thumbnail Handler
type thumbnailHandlers struct {
minioClient *minio.Client
}
func initRedis() {
if addr, ok := os.LookupEnv("REDIS_SERVICE"); ok {
locker = new(Locker)
client := red.NewClient(&red.Options{ Addr: addr })
pool := goredis.NewPool(client)
locker.sync = redsync.New(pool)
locker.initialized = false
log.Printf("[REDIS] Service address %s\n", addr)
if _, err := client.Ping(context.Background()).Result(); err == nil {
log.Printf("[REDIS] Connection established with the service, %s, mutex on.\n", addr)
locker.initialized = true
}
}
}
// Init
func init() {
var err error
gifsicleCmd, err = exec.LookPath("gifsicle")
if err != nil {
log.Fatalln("gifsicle not found")
}
initRedis()
}
// Run gifsicle command
func runGifsicle(r io.Reader, args []string, w io.Writer) error {
cmd := exec.Command(gifsicleCmd, args...)
var stderr bytes.Buffer
cmd.Stdin = r
cmd.Stdout = w
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return errors.New("Girsicle error! - " + err.Error() + " - " + strings.Trim(string(stderr.Bytes()), " "))
}
return nil
}
// Finds out whether the url is http(insecure) or https(secure).
func isSecure(urlStr string) bool {
u, err := url.Parse(urlStr)
if err != nil {
panic(err)
}
return u.Scheme == "https"
}
// Convert match result into one bool
func isMatched(matched bool, err error) bool {
if err != nil {
log.Fatalln(err)
return false
}
return matched
}
// Find the Host of the given url.
func findHost(urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
panic(err)
}
return u.Host
}
// Get keys
func mustGetAccessKeys() (accessKey, secretKey string) {
accessKey = os.Getenv("ACCESS_KEY")
if accessKey == "" {
log.Fatalln("Env variable 'ACCESS_KEY' not set")
}
secretKey = os.Getenv("SECRET_KEY")
if secretKey == "" {
log.Fatalln("Env variable 'SECRET_KEY' not set")
}
return accessKey, secretKey
}
func main() {
flag.Parse()
// Check bucket name
if *bucketName == "" {
log.Fatalln("Bucket name cannot be empty.")
}
// Get access keys
accessKey, secretKey := mustGetAccessKeys()
// Initialize minio client.
minioClient, err := minio.NewWithRegion(findHost(*endPoint), accessKey, secretKey, isSecure(*endPoint), *region)
if err != nil {
log.Fatalln(err)
}
// Check bucket exists
bucketExists, err := minioClient.BucketExists(*bucketName)
if err != nil {
log.Fatalln(err)
}
if !bucketExists {
log.Fatalln("Bucket " + *bucketName + " not exists on " + *endPoint)
}
// Initialize handler
thumbnailGenerator := thumbnailHandlers{
minioClient: minioClient,
}
// Handler for index page
http.HandleFunc("/", thumbnailGenerator.ProcessRequest)
http.HandleFunc("/health", thumbnailGenerator.healthcheck)
log.Println("Starting image resizer on " + *address)
// Start http server
log.Fatalln(http.ListenAndServe(*address, nil))
}
// Check the liveness of the service
func (api thumbnailHandlers) healthcheck(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}
func CreateBackground(width, height int, backgroundColor color.Color) image.Image {
img := image.NewNRGBA(image.Rect(0, 0, width, height))
draw.Draw(img, img.Bounds(), &image.Uniform{backgroundColor}, image.ZP, draw.Src)
return img
}
func GetContentType(r *minio.Object) (string, error) {
buffer := make([]byte, 512)
_, err := r.Read(buffer)
if err != nil {
return "", err
}
contentType := http.DetectContentType(buffer)
return contentType, nil
}
func calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight float64) (int, int) {
if srcWidth == 0 || srcHeight == 0 {
return 0, 0
}
ratio := []float64{ maxWidth / srcWidth, maxHeight / srcHeight }
minRatio := math.Min(ratio[0], ratio[1])
return int(math.Round(srcWidth * minRatio)), int(math.Round(srcHeight * minRatio))
}
// Request Process
func (api thumbnailHandlers) ProcessRequest(w http.ResponseWriter, r *http.Request) {
// Get Path
path := r.URL.Path
// Set variables
file := ""
ext := ""
width := 0
height := 0
id := 0
sourceFileName := ""
targetFileName := ""
// Check request
switch true {
case isMatched(regexp.MatchString("(?i)^/[0-9]{1,6}/pictures/thumb/[0-9]{2,3}X-[0-9]{2,3}X-.*\\.(jpg|jfif|jpeg|gif|png|webp|svg|bmp)$", path)):
// /123/pictures/thumb/000X-000X-file.ext
parts := regexp.MustCompile("(?i)^/([0-9]{1,6})/pictures/thumb/([0-9]{2,3})X-([0-9]{2,3})X-(.*)\\.(jpg|jfif|jpeg|gif|png|webp|svg|bmp)$").FindStringSubmatch(path)
// Set variables
file = parts[4]
ext = parts[5]
id, _ = strconv.Atoi(parts[1])
width, _ = strconv.Atoi(parts[2])
height, _ = strconv.Atoi(parts[3])
// Set files
sourceFileName = fmt.Sprintf("%d/pictures/%s.%s", id, file, ext)
targetFileName = fmt.Sprintf("%d/pictures/thumb/%dX-%dX-%s.%s", id, width, height, file, ext)
case isMatched(regexp.MatchString("(?i)^/[0-9]{1,6}/pictures/thumb/[0-9]{2,3}X-.*\\.(jpg|jfif|jpeg|gif|png|webp|svg|bmp)$", path)):
// /123/pictures/thumb/000X-file.ext
parts := regexp.MustCompile("(?i)^/([0-9]{1,6})/pictures/thumb/([0-9]{2,3})X-(.*)\\.(jpg|jfif|jpeg|gif|png|webp|svg|bmp)$").FindStringSubmatch(path)
// Set variables
file = parts[3]
ext = parts[4]
id, _ = strconv.Atoi(parts[1])
width, _ = strconv.Atoi(parts[2])
height = 0
// Set files
sourceFileName = fmt.Sprintf("%d/pictures/%s.%s", id, file, ext)
targetFileName = fmt.Sprintf("%d/pictures/thumb/%dX-%s.%s", id, width, file, ext)
case isMatched(regexp.MatchString("(?i)^/[0-9]{1,6}/dosyalar/_thumbs/.*\\.(jpg|jfif|jpeg|gif|png|webp|svg|bmp)$", path)):
// /123/dosyalar/_thumbs/file.ext
parts := regexp.MustCompile("(?i)^/([0-9]{1,6})/dosyalar/_thumbs/(.*)\\.(jpg|jfif|jpeg|gif|png|webp|svg|bmp)$").FindStringSubmatch(path)
// Set variables
file = parts[2]
ext = parts[3]
id, _ = strconv.Atoi(parts[1])
width = 100
height = 100
// Set files
sourceFileName = fmt.Sprintf("%d/dosyalar/%s.%s", id, file, ext)
targetFileName = fmt.Sprintf("%d/dosyalar/_thumbs/%s.%s", id, file, ext)
default:
// Not found
http.NotFound(w, r)
return
}
// Equals height to width
if height == 0 {
height = width
}
// Check source file is alive
sourceFileInfo, err := api.minioClient.StatObject(*bucketName, sourceFileName, minio.StatObjectOptions{})
if err != nil {
http.NotFound(w, r)
return
}
// Check target file is alive so return
_, err = api.minioClient.StatObject(*bucketName, targetFileName, minio.StatObjectOptions{})
if err == nil {
targetFile, err := api.minioClient.GetObject(*bucketName, targetFileName, minio.GetObjectOptions{})
if err != nil {
http.Error(w, err.Error(), 500)
log.Println("File "+targetFileName+" read error!", err)
return
}
defer targetFile.Close()
typeName, err := GetContentType(targetFile)
if err != nil {
typeName = "application/octet-stream"
}
targetFile.Seek(0, 0)
w.Header().Add("Content-Type", typeName)
host, _ := os.Hostname()
w.Header().Add("X-Serve-From", host)
w.Header().Add("X-Resized", "false")
io.Copy(w, targetFile)
return
}
// Lock the first request.
mutex, err := locker.lock(targetFileName, w, r)
defer locker.unlock(mutex)
if err != nil {
return;
}
// Test delay for local. Make concurrent requests to same resource in 10 second.
// The first one should handle the real request while others waiting to resolve with 503 Retry-After
// time.Sleep(10 * time.Second)
// log.Println("PASS")
// Open source file
sourceFile, err := api.minioClient.GetObject(*bucketName, sourceFileName, minio.GetObjectOptions{})
if err != nil {
http.Error(w, err.Error(), 500)
log.Println("File "+sourceFileName+" read error!", err)
return
}
defer sourceFile.Close()
// Get image data
imageData, inputFormat, err := image.Decode(sourceFile)
if err != nil {
http.Error(w, err.Error(), 500)
log.Println(err, sourceFileName)
return
}
// Create pipes
imageReader, imageWriter := io.Pipe()
// Check image is small for given width height
resizeRequire := false; targetWidthHeight := map[string]int{ "width": 0, "height": 0 }
if imageData.Bounds().Size().X < width && imageData.Bounds().Size().Y < height {
resizeRequire = true
if imageData.Bounds().Size().X >= imageData.Bounds().Size().Y {
targetWidthHeight["width"] = width
targetWidthHeight["height"] = 0
} else {
targetWidthHeight["width"] = 0
targetWidthHeight["height"] = height
}
}
// Content Type
targetContentType := sourceFileInfo.ContentType
// Save image
targetData := imageData
switch inputFormat {
case "jpeg":
if resizeRequire {
targetData = imaging.Resize(targetData, targetWidthHeight["width"], targetWidthHeight["height"], imaging.Lanczos)
}
targetData = imaging.Fit(targetData, width, height, imaging.Linear)
targetData = imaging.Sharpen(targetData, .7)
targetData = imaging.OverlayCenter(CreateBackground(width, height, color.White), targetData, 100)
go func() {
defer imageWriter.Close()
imaging.Encode(imageWriter, targetData, imaging.JPEG, imaging.JPEGQuality(80))
}()
case "gif":
sourceFile.Seek(0, 0)
actualWidth := float64(targetData.Bounds().Size().X)
actualHeight := float64(targetData.Bounds().Size().Y)
lastWidth, lastHeight := calculateAspectRatioFit(actualWidth, actualHeight, float64(width), float64(height))
offsetX := (lastWidth - width) / 2
offsetY := (lastHeight - height) / 2
tR, tW := io.Pipe()
defer tR.Close()
go func() {
defer tW.Close()
var err error = nil
if resizeRequire {
targetWidth := strconv.Itoa(targetWidthHeight["width"])
if targetWidth == "0" {
targetWidth = "_"
}
targetHeight := strconv.Itoa(targetWidthHeight["height"])
if targetHeight == "0" {
targetHeight = "_"
}
err = runGifsicle(sourceFile, []string{ "--resize", fmt.Sprintf("%sx%s", targetWidth, targetHeight) }, tW)
} else {
err = runGifsicle(sourceFile, []string{ "--resize-fit", fmt.Sprintf("%dx%d", width, height) }, tW)
}
if err != nil {
log.Println(err)
}
}()
go func() {
defer imageWriter.Close()
runGifsicle(tR, []string{ "--crop", fmt.Sprintf("%d,%d+%dx%d", offsetX, offsetY, width, height), "--no-warnings", "--no-ignore-errors" }, imageWriter)
}()
case "png":
if resizeRequire {
targetData = imaging.Resize(targetData, targetWidthHeight["width"], targetWidthHeight["height"], imaging.Lanczos)
}
targetData = imaging.Fit(targetData, width, height, imaging.Lanczos)
targetData = imaging.OverlayCenter(CreateBackground(width, height, color.Transparent), targetData, 100)
go func() {
defer imageWriter.Close()
imaging.Encode(imageWriter, targetData, imaging.PNG, imaging.PNGCompressionLevel(png.BestCompression))
}()
default:
if resizeRequire {
targetData = imaging.Resize(targetData, targetWidthHeight["width"], targetWidthHeight["height"], imaging.Lanczos)
}
targetData = imaging.Fit(targetData, width, height, imaging.Lanczos)
targetData = imaging.Sharpen(targetData, 3.5)
targetData = imaging.OverlayCenter(CreateBackground(width, height, color.White), targetData, 100)
targetContentType = "image/jpeg"
go func() {
defer imageWriter.Close()
imaging.Encode(imageWriter, targetData, imaging.JPEG)
}()
}
// Create reader clone
var buf bytes.Buffer
tee := io.TeeReader(imageReader, &buf)
w.Header().Add("Content-type", targetContentType)
w.Header().Add("X-Resized", "true")
host, _ := os.Hostname()
w.Header().Add("X-Serve-From", host)
size, _ := io.Copy(w, tee)
log.Printf("[%s] %s created.", host, targetFileName)
_, a := api.minioClient.PutObject(*bucketName, targetFileName, io.Reader(&buf), size, minio.PutObjectOptions{ContentType: sourceFileInfo.ContentType})
if a != nil {
log.Println(a, targetFileName)
}
}