-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.go
116 lines (103 loc) · 2.87 KB
/
images.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
package api
import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/Blockitifluy/CoffeeCo/utility"
"github.com/blockloop/scan"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
// ImageData contains the URL, content, content-type of an image
type ImageData struct {
URL string `db:"URL"`
Content []byte `db:"content"`
ContentType string `db:"mimetype"`
}
// APIUploadImage is an api call. Doesn't work as expected when called outside an API context
//
// Uploads an image (png, jpeg and gif) with a limited size, compresses it and add to database
func (srv *Server) APIUploadImage(w http.ResponseWriter, r *http.Request) {
mimetype := r.Header.Get("Content-Type")
if Accepted := utility.CanImageBeAccepted(r, mimetype); !Accepted.Ok {
utility.Error(w, utility.HTTPError{
Public: Accepted.Msg,
Message: Accepted.Msg,
Code: Accepted.Code,
})
return
}
img, err := io.ReadAll(r.Body)
if err != nil {
utility.Error(w, utility.HTTPError{
Public: "Couldn't Read Image",
Message: err.Error(),
Code: 500,
})
return
}
compress, compErr := utility.AutoCompress(mimetype, img)
if compErr != nil {
utility.Error(w, utility.HTTPError{
Public: "Couldn't Compress Image",
Message: compErr.Error(),
Code: 500,
})
return
}
ID := uuid.New()
zipped, err := utility.GZipBytes(compress)
if err != nil {
utility.Error(w, utility.HTTPError{
Public: "Couldn't Zip Image",
Message: err.Error(),
Code: 500,
})
return
}
_, resultErr := srv.Exec("INSERT INTO Images (url, content, mimetype) VALUES (?, ?, ?)", ID.String(), zipped, mimetype)
if resultErr != nil {
utility.Error(w, utility.HTTPError{
Public: "Couldn't Add image to Server",
Message: resultErr.Error(),
Code: 500,
})
return
}
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(ID.String()))
}
// APIDownloadImage is an api call. Doesn't work as expected when called outside an API context
//
// Retrieves an image from the batabase, compress for network
func (srv *Server) APIDownloadImage(w http.ResponseWriter, r *http.Request) {
URLParams := mux.Vars(r)
imageURL, err := url.QueryUnescape(URLParams["url"])
if err != nil {
utility.Error(w, utility.HTTPError{
Public: "Couldn't Write Image",
Message: err.Error(),
Code: 400,
})
return
}
rows, err := srv.Query("SELECT content, mimetype FROM Images WHERE url = ?", imageURL)
if err != nil {
utility.Error(w, utility.HTTPError{
Public: "Couldn't Get Image",
Message: err.Error(),
Code: 500,
})
return
}
var Image ImageData
if err := scan.Row(&Image, rows); err != nil {
utility.SendScanErr(w, err, nil)
return
}
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", utility.ImageMaxAge))
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Type", Image.ContentType)
w.Write(Image.Content) // Faster Speed over network
}