forked from blakesmith/skeeter-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeeter.go
67 lines (56 loc) · 1.32 KB
/
skeeter.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
package main
import (
"fmt"
"net/http"
"os/exec"
)
// Borrowed from: https://gist.github.com/1477401
func pipe_commands(commands ...*exec.Cmd) ([]byte, error) {
for i, command := range commands[:len(commands)-1] {
out, err := command.StdoutPipe()
if err != nil {
return nil, err
}
command.Start()
commands[i+1].Stdin = out
}
final, err := commands[len(commands)-1].Output()
if err != nil {
return nil, err
}
return final, nil
}
func makeImage(url string, width string) string {
curl := exec.Command("curl", "-s", url)
convert := exec.Command("convert", "-", "jpg:-")
jp2a := exec.Command("jp2a", "-", fmt.Sprintf("--width=%s", width))
out, err := pipe_commands(curl, convert, jp2a)
if err != nil {
fmt.Println(err)
// Panic
} else {
fmt.Println(out)
}
return string(out)
}
func imageHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var url, width string
if val, ok := r.Form["image_url"]; ok {
url = val[0]
} else {
// 400 Bad Request: [:error, "Image_url query param is required"]
}
if val, ok := r.Form["width"]; ok {
width = val[0]
} else {
width = "80"
}
fmt.Printf("Processing request with url %s, and width %s\n", url, width)
out := makeImage(url, width)
fmt.Fprintf(w, out)
}
func main() {
http.HandleFunc("/", imageHandler)
http.ListenAndServe(":9000", nil)
}