forked from steve-perkins/logofy
/
logofy.go
147 lines (135 loc) · 4.77 KB
/
logofy.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
package logofy
import (
"appengine"
"appengine/memcache"
"fmt"
"giphyutils"
"imageutils"
"math/rand"
"net/http"
"strings"
"net/url"
)
const MEMCACHE_ENABLED = false
func init() {
http.Handle("/", http.FileServer(http.Dir("static")))
http.HandleFunc("/logo", defaultHandler)
http.HandleFunc("/slack", slackHandler)
}
func defaultHandler(w http.ResponseWriter, r *http.Request) {
abstractHandler(w, r)
}
func slackHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
titles := []string{
"Another paradigm shift from Logofy",
"This game-changer brought to you by Logofy",
"Building synergy with Logofy",
"Thinking outside the box with Logofy",
"Pivoting this image's strategy with Logofy",
"Adding business value with Logofy",
"Moving the needle with Logofy",
"Empowering innovative transformation with Logofy",
"Gamifying the tipping point with Logofy",
"This market disruption brought to you by Logofy",
}
randomIndex := rand.Intn(len(titles))
textParam := r.URL.Query().Get("text")
paramStrings := strings.SplitAfter(textParam, " ")
// TODO: Determine whether "paramStrings[0]" is the URL to an image. If not, then treat it as a Giphy search string and get an image URL from "giphyutils.FetchGiphyUrl()"
if len(paramStrings) > 0 {
giphyImage, err := giphyutils.FetchGiphyUrl(ctx, paramStrings[0])
if err != nil {
ctx.Errorf("An error was encountered: %s\n", err)
} else {
ctx.Infof("Giphy returned this image URL: %s\n", giphyImage)
}
}
imgUrl := ""
if len(paramStrings) > 2 {
originalImage, logoImage, pos := strings.TrimSpace(paramStrings[0]), strings.TrimSpace(paramStrings[1]), strings.TrimSpace(paramStrings[2])
imgUrl = `http://logofy-web.appspot.com/logo?img=` + originalImage + `&logo=` + logoImage + `&pos=` + pos
ctx.Infof("originalImage: %s, logoImage: %s, pos:%s\n, ", originalImage, logoImage, pos)
} else if len(paramStrings) > 1 {
originalImage, logoOrPos := strings.TrimSpace(paramStrings[0]), strings.TrimSpace(paramStrings[1])
if(logoOrPos == "tl" || logoOrPos == "tc" || logoOrPos == "tr" || logoOrPos == "bl" || logoOrPos == "bc" || logoOrPos == "br"){
imgUrl = `http://logofy-web.appspot.com/logo?img=` + originalImage + `&pos=` + logoOrPos
ctx.Infof("originalImage: %s, pos:%s\n, ", originalImage, logoOrPos)
}else{
imgUrl = `http://logofy-web.appspot.com/logo?img=` + originalImage + `&logo=` + logoOrPos
ctx.Infof("originalImage: %s, logo:%s\n, ", originalImage, logoOrPos)
}
} else {
imgUrl = url.QueryEscape(textParam)
}
jsonString :=
`{
"response_type" : "in_channel",
"attachments" : [{
"title" : "` + titles[randomIndex] + `",
"image_url" : "` + imgUrl + `"
}]
}`
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, jsonString)
}
func abstractHandler(w http.ResponseWriter, r *http.Request) {
// If the requested pic with the requested logo has already been generated, then use the cached version
ctx := appengine.NewContext(r)
originalImageUrl := r.URL.Query().Get("img")
logoImageUrl := r.URL.Query().Get("logo")
if logoImageUrl == "" {
logoImageUrl = "brazz"
}
if MEMCACHE_ENABLED {
item, err := memcache.Get(ctx, logoImageUrl+":"+originalImageUrl)
if err != nil {
ctx.Infof("Retrieved [%s:%s] from memcache\n", logoImageUrl, originalImageUrl)
w.Header().Set("Content-Type", "image/png")
w.Write(item.Value)
return
}
}
// Load the logo image
logoImage, err := imageutils.FetchLogoImage(logoImageUrl)
if err != nil {
logoImage, err = imageutils.FetchImage(ctx, logoImageUrl, imageutils.TARGET_LOGO_WIDTH)
}
if err != nil {
message := fmt.Sprintf("Unable to load logo image file: %s\n", err)
ctx.Errorf(message)
fmt.Fprintf(w, message)
return
}
// Fetch the source image
originalImage, err := imageutils.FetchImage(ctx, originalImageUrl, imageutils.TARGET_IMAGE_WIDTH)
if err != nil {
message := fmt.Sprintf("An error occurred: %s\n", err)
ctx.Errorf(message)
fmt.Fprintf(w, message)
return
}
// Set the logo position
pos := r.URL.Query().Get("pos")
// Generate and return an image with logo over the source
generatedImage := imageutils.GenerateImageWithLogo(originalImage, logoImage, pos)
generatedImageBytes, err := imageutils.ImageToBytes(generatedImage)
if err != nil {
message := fmt.Sprintf("An error occured: %s\n", err)
ctx.Errorf(message)
fmt.Fprintf(w, message)
return
}
// Cache the generated image bytes before sending them in the HTTP response
if MEMCACHE_ENABLED {
item := &memcache.Item{
Key: logoImageUrl + ":" + originalImageUrl,
Value: generatedImageBytes,
}
memcache.Add(ctx, item)
ctx.Infof("Caching [%s:%s]\n", logoImageUrl, originalImageUrl)
}
ctx.Infof("Serving up [%s:%s]\n", logoImageUrl, originalImageUrl)
w.Header().Set("Content-Type", "image/png")
w.Write(generatedImageBytes)
}