-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
executable file
·85 lines (67 loc) · 2.11 KB
/
handlers.go
File metadata and controls
executable file
·85 lines (67 loc) · 2.11 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
package main
import (
"io"
"fmt"
"errors"
"github.com/labstack/echo"
"github.com/AidHamza/optimizers-api/storage"
"github.com/AidHamza/optimizers-api/messaging"
"github.com/AidHamza/optimizers-api/pkg/helpers"
op "github.com/AidHamza/optimizers-api/pkg/operation"
)
func cruncher(c echo.Context) error {
//Process #1: Welcome the file
file, err := c.FormFile("file")
if err != nil {
return throwHTTPError(c, badImageRequest, "FILE_UPLOAD", err)
}
//Process #2: Store the bytes into src
src, err := file.Open()
if err != nil {
return throwHTTPError(c, inputFileOpen, "FILE_OPEN", err)
}
defer src.Close()
//Process #3: Try to guess the type and reject if not acceptable
imageType := guessImageMimeTypes(src)
allowedImages := []string{"image/jpeg", "image/png"}
isImageAllowed, _ := helpers.InArray(imageType, allowedImages)
if isImageAllowed == false {
return throwHTTPError(c, invalidImageType, "FILE_TYPE", errors.New("FILE_TYPE: invalid file type"))
}
var imageBucket string = "jpeg"
if imageType == "image/png" {
imageBucket = "png"
}
fileSize, err := src.Seek(0, io.SeekEnd) //2 = from end
if err != nil {
return throwHTTPError(c, invalidImageType, "FILE_GET_SIZE", err)
}
//Return to the head of the file
src.Seek(0, io.SeekStart)
// Generate an Unique Operation ID
opId := helpers.RandomID()
fileName := fmt.Sprintf("%d/%s", opId, file.Filename)
//Process #4: Queue for processing
err = storage.PutObject(src, imageBucket, fileName, imageType)
if err != nil {
return throwHTTPError(c, failedStoreFile, "FILE_STORAGE_FAILED", err)
}
producer, err := messaging.NewProducer()
if err != nil {
return throwHTTPError(c, failedQueueFile, "OP_QUEUE_FAILED", err)
}
operation, err := op.NewOperation(opId, file.Filename, imageType)
if err != nil {
return throwHTTPError(c, failedQueueFile, "OP_QUEUE_FAILED", err)
}
err = producer.PublishMessage(operation)
if err != nil {
return throwHTTPError(c, failedQueueFile, "OP_QUEUE_PUBLISH_FAILED", err)
}
result := &compressSuccess{
Filename: file.Filename,
Size: fileSize,
Id: opId,
}
return c.JSON(200, result)
}