-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
408 lines (363 loc) · 10.1 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"image/color"
"image/gif"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/eliukblau/pixterm/pkg/ansimage"
"github.com/mattLLVW/term-gif/models"
"golang.org/x/time/rate"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
lol "github.com/kris-nova/lolgopher"
"github.com/mssola/user_agent"
"github.com/spf13/viper"
)
type config struct {
Host string
Port int
ApiKey string
ApiUrl string
Limit int
DbUser string
DbPass string
DbHost string
DbPort int
DbName string
DbEnabled bool
}
var c config
// Create a custom visitor struct which holds the rate limiter for each
// visitor and the last time that the visitor was seen.
// alexedwards.net/blog/how-to-rate-limit-http-requests
type visitor struct {
limiter *rate.Limiter
lastSeen time.Time
}
// Change the map to hold values of the type visitor.
var visitors = make(map[string]*visitor)
var mu sync.Mutex
func getVisitor(ip string) *rate.Limiter {
mu.Lock()
defer mu.Unlock()
v, exists := visitors[ip]
if !exists {
limiter := rate.NewLimiter(1, 3)
// Include the current time when creating a new visitor.
visitors[ip] = &visitor{limiter, time.Now()}
return limiter
}
// Update the last seen time for the visitor.
v.lastSeen = time.Now()
return v.limiter
}
// Every minute check the map for visitors that haven't been seen for
// more than 3 minutes and delete the entries.
func cleanupVisitors() {
for {
time.Sleep(time.Minute)
mu.Lock()
for ip, v := range visitors {
if time.Since(v.lastSeen) > 3*time.Minute {
delete(visitors, ip)
}
}
mu.Unlock()
}
}
// Search gif on api and return download url and gif id
func searchApi(search string) (data models.Api, err error) {
url := fmt.Sprintf("%s/v1/search?q='%s'&key=%s&media_filter=minimal&limit=%d", c.ApiUrl, search, c.ApiKey, c.Limit)
// Search gif on api
res, err := http.Get(url)
if err != nil {
log.Printf("Error while searching gif %s", err)
return data, err
}
defer res.Body.Close()
// Parse json response
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Error while reading body %s", err)
return data, err
}
if err := json.Unmarshal(body, &data); err != nil {
log.Printf("Error while unmarshalling body %s", err)
return data, err
}
return data, nil
}
// If anything bad happen, be cute
func oopsGif() []models.RenderedImg {
g := models.AnsiGif{}
g.Oops()
g.Render()
return g.Rendered
}
// Send rendered gif as a response
func sendGif(w http.ResponseWriter, imgs []models.RenderedImg) {
// Clear terminal and position cursor
fmt.Fprintf(w, "\033[2J\033[1;1H")
for _, srcImg := range imgs {
time.Sleep(time.Duration(srcImg.Delay*10) * time.Millisecond)
fmt.Fprintf(w, srcImg.Output)
// Reposition cursor
fmt.Fprintf(w, "\033[1;1H")
}
// Clear terminal
fmt.Fprintf(w, "\033[2J")
}
// Check if url is valid
func isUrl(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}
// TODO: find a way to list natively supported extension.
func isValidExtension(ext string) bool {
switch ext {
case ".jpeg", ".jpg", ".png", ".gif", ".webp":
return true
}
return false
}
// If requested from Cli, search for gif, encode in ansi and return result
func wildcardHandler(w http.ResponseWriter, r *http.Request) {
// Extract search terms and url query from url and format them
qry := r.URL.Query()
preview := qry.Get("img")
url := qry.Get("url")
reverse := qry.Get("rev")
vars := mux.Vars(r)
search, ok := vars["search"]
// If there's no search terms and nothing to fetch, just return doc.
if !ok && url == "" {
landing := `
_ __
__ _(_)/ _|__ ___ _ _________ _ _ __ _ _ _ __
/ _ | | |_ \ \/ / | | |_ /_ / | | | | '__| | | | '_ \
| (_| | | _| > <| |_| |/ / / /| |_| |_| | | |_| | | | |
\__, |_|_|(_)_/\_\\__, /___/___|\__, (_)_| \__,_|_| |_|
|___/ |___/ |___/
Search and display gifs in your terminal
USAGE:
curl gif.xyzzy.run/<your_search_terms_separated_by_an_underscore>
curl gif.xyzzy.run?url=https://<the_greatest_gif_or_image_of_all_times.jpeg>
EXAMPLE:
curl gif.xyzzy.run/spongebob_magic
curl "gif.xyzzy.run?url=https://gif.xyzzy.run/static/img/mgc.gif" #inception :)
You can also reverse the gif if you want, i.e:
curl "gif.xyzzy.run/mind_blown?rev=true"
Or just display a preview image of the gif, i.e:
curl "gif.xyzzy.run/wow?img=true"
Powered By Tenor
if you like this project, please consider sponsoring it: https://github.com/sponsors/mattLLVW
`
lw := lol.Writer{
Output: w,
ColorMode: lol.ColorModeTrueColor,
}
lw.Write([]byte(landing))
return
}
// If user submit url, check that it's valid with a valid extension before downloading.
if url != "" {
ext := filepath.Ext(url)
if !isUrl(url) || !isValidExtension(ext) {
sendGif(w, oopsGif())
return
}
res, err := http.Get(url)
if err != nil {
sendGif(w, oopsGif())
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
// Use the net/http package's handy DetectContentType function.
contentType := http.DetectContentType(body)
if contentType == "image/gif" {
g := models.AnsiGif{}
g.Gif, err = gif.DecodeAll(bytes.NewBuffer(body))
if err != nil {
sendGif(w, oopsGif())
return
}
if preview != "" {
// Clear terminal and position cursor
fmt.Fprintf(w, "\033[2J\033[1;1H")
img := g.Preview()
fmt.Fprintf(w, img)
return
}
g.Render()
if reverse != "" {
g.Reverse()
}
sendGif(w, g.Rendered)
return
}
if strings.HasPrefix(contentType, "image") {
// Clear terminal and position cursor
fmt.Fprintf(w, "\033[2J\033[1;1H")
// set image scale factor for ANSIPixel grid, background color and scale mode
tx, ty := 30, 9
sfy, sfx := ansimage.BlockSizeY, ansimage.BlockSizeX
mc := color.RGBA{0x00, 0x00, 0x00, 0xff}
dm := ansimage.DitheringMode(0)
sm := ansimage.ScaleMode(2)
pix, _ := ansimage.NewScaledFromReader(bytes.NewBuffer(body), sfy*ty, sfx*tx, mc, sm, dm)
pix.SetMaxProcs(runtime.NumCPU())
res := pix.Render()
fmt.Fprintf(w, res)
return
}
// If the detected content type is not a valid image be cute.
sendGif(w, oopsGif())
return
}
// Search gif on api and return api data
strings.ReplaceAll(search, "_", " ")
apiData, err := searchApi(search)
if err != nil || len(apiData.Results) == 0 {
log.Println("api results len", len(apiData.Results), "err", err)
sendGif(w, oopsGif())
return
}
// If enabled, return a random gif
rand.Seed(time.Now().Unix())
randNb := rand.Intn(c.Limit)
if len(apiData.Results) < c.Limit {
// Must be a specific search so just return the only result
randNb = 0
}
gifUrl := apiData.Results[randNb].Media[0].Gif.Url
gifId := apiData.Results[randNb].Id
gifPreview := apiData.Results[randNb].Media[0].Gif.Preview
if preview != "" {
// Clear terminal and position cursor
fmt.Fprintf(w, "\033[2J\033[1;1H")
img := models.GetPreview(gifPreview)
fmt.Fprintf(w, img)
return
}
if c.DbEnabled && models.AlreadyExist(gifId) {
// If we already have gif rendered locally try returning it
log.Println("fetching", gifId, "from database")
g, err := models.GetGifFromDb(gifId, qry.Get("rev") != "")
if err != nil {
log.Println("error while fetching", gifId, "from database")
sendGif(w, oopsGif())
return
}
sendGif(w, g)
return
}
// Render fetched gif
g := models.AnsiGif{}
err = g.Get(gifUrl)
if err != nil {
log.Println("error getting", gifId)
g.Oops()
g.Render()
sendGif(w, g.Rendered)
return
}
g.Render()
if c.DbEnabled {
// Store rendered gif in db.
log.Println("inserting", gifId, "into database")
err = g.Insert(gifId)
if err != nil {
log.Println("error while inserting", gifId, "into database")
g.Oops()
g.Render()
sendGif(w, g.Rendered)
return
}
}
if reverse != "" {
g.Reverse()
}
sendGif(w, g.Rendered)
return
}
// Check if request is made from a CLI
func isValidBrowser(browser string) bool {
switch browser {
case
"curl",
"Wget",
"HTTPie":
return true
}
return false
}
// Return proper handler depending on cli or browser
func conditionalHandler(w http.ResponseWriter, r *http.Request) {
ua := user_agent.New(r.Header.Get("User-Agent"))
name, _ := ua.Browser()
// If static site, just return it
if !isValidBrowser(name) {
http.ServeFile(w, r, "static/index.html")
} else {
// Apply limiter if we are curling and have a real ip
ip := r.Header.Get("X-Real-Ip")
if ip != "" {
limiter := getVisitor(ip)
if limiter.Allow() == false {
http.Error(w, http.StatusText(429), http.StatusTooManyRequests)
return
}
}
wildcardHandler(w, r)
}
}
// Run a background goroutine to remove old entries from the visitors map.
func init() {
go cleanupVisitors()
}
func main() {
log.SetOutput(os.Stdout)
// Find and read the config file
viper.AddConfigPath(".")
viper.SetConfigName("env.sample")
viper.SetConfigType("env")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
} else {
log.Fatalf("Error while reading config file %s", err)
// Config file was found but another error was produced
}
}
viper.AutomaticEnv()
if err := viper.Unmarshal(&c); err != nil {
log.Fatalf("Unable to unmarshal config %s", err)
}
if c.DbEnabled {
// Init database using environ variables
models.InitDB(fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", c.DbUser, c.DbPass, c.DbHost, c.DbPort, c.DbName))
}
// Route to static site or ascii gif based on user agent
r := mux.NewRouter()
r.PathPrefix("/static").Handler(http.StripPrefix("/static", http.FileServer(http.Dir("./static")))).Methods("GET")
r.HandleFunc("/{search}", conditionalHandler).Methods("GET")
r.PathPrefix("/").HandlerFunc(conditionalHandler).Methods("GET")
log.Printf("starting server on %s:%d\n", c.Host, c.Port)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", c.Host, c.Port), handlers.RecoveryHandler()(handlers.LoggingHandler(os.Stdout, r)))
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}