-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
124 lines (96 loc) · 3.02 KB
/
handlers.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
package main
import (
"fmt"
"github.com/disintegration/imaging"
"github.com/gorilla/mux"
"github.com/rlmcpherson/s3gof3r"
_ "image/jpeg"
_ "image/png"
"log"
"net/http"
//"net/url"
//"image/color"
"path"
)
func registerHandlers(r *mux.Router) {
//r.HandleFunc("/passthrough/{object:[0-9a-z/_.-]+}", PassthroughHandler)
r.HandleFunc("/{object:[0-9A-Za-z/_.-]+}", ThumbnailHandler)
//r.HandleFunc("/{object:[0-9a-z/-_.]+}/{previewType:[a-zA-Z0-9_-]+}", ThumbnailHandler)
r.HandleFunc("/", HelloHandler)
}
func HelloHandler(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "s3preview - See https://github.com/andrioid/s3preview for more information.")
}
func ThumbnailHandler(rw http.ResponseWriter, r *http.Request) {
object := mux.Vars(r)["object"]
previewType := r.FormValue("t")
if previewType == "" {
http.Error(rw, "Preview Type empty. E.g. Add ?t=small to your URL", 400)
//fmt.Fprintf(rw, "previewType empty. Add \"/passthrough/\" in front of your URL to see the original")
return
}
typeOptions, ok := configuration.Previews[previewType]
if ok != true {
http.Error(rw, "previewType not configured", 400)
return
}
k, err := s3gof3r.EnvKeys() // get S3 keys from environment
if err != nil {
fmt.Fprint(rw, err.Error())
return
}
// Create s3 path for thumbnail
s3path := path.Join(configuration.Preview_Prefix, previewType, object)
s3url := fmt.Sprintf("http://%s.%s/%s", configuration.Preview_Bucket, configuration.StorageDomain, path.Join(configuration.Preview_Prefix, previewType, object))
// Ask Mr. Bloom
exists := previewBloom.TestString(s3path)
if exists == true {
// Thumbnail exists, redirect and return
//fmt.Fprintf(rw, "Redirecting to: %s", s3url)
http.Redirect(rw, r, s3url, 301)
return
}
// Fetch image and generate stuff
// - TODO: Check if we're too busy to create the thumbnail now. Return a temporary error 502 if we are.
// - TODO: Add the object into preview queue for later processing
// Open bucket to put file into
s3 := s3gof3r.New("", k)
b := s3.Bucket(configuration.Asset_Bucket)
rb, _, err := b.GetReader(path.Join(configuration.Asset_Prefix, object), nil)
if err != nil {
fmt.Fprint(rw, err.Error())
return
}
orgImg, err := imaging.Decode(rb)
log.Printf("GET %s", path.Join(configuration.Asset_Prefix, object))
if err != nil {
http.Error(rw, err.Error(), 400)
return
}
dstImg, err := Preview(&orgImg, typeOptions)
if err != nil {
http.Error(rw, err.Error(), 400)
return
}
// Put into Preview Bucket
pb := s3.Bucket(configuration.Preview_Bucket)
hdr := make(http.Header)
hdr.Add("Content-Type", "image/jpg")
prw, err := pb.PutWriter(s3path, hdr, nil)
if err = imaging.Encode(prw, dstImg, imaging.JPEG); err != nil {
http.Error(rw, err.Error(), 400)
return
}
if err = prw.Close(); err != nil {
fmt.Fprintf(rw, err.Error())
return
}
log.Printf("PUT %s", s3path)
previewBloom.AddString(s3path)
// Output to browser
err = imaging.Encode(rw, dstImg, imaging.JPEG)
if err != nil {
fmt.Fprintf(rw, err.Error())
return
}
}