forked from rbrick/services
/
main.go
77 lines (61 loc) · 1.69 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
package main
import (
"errors"
"github.com/go-martini/martini"
"math/rand"
"net/http"
"os"
"path/filepath"
"time"
)
var (
CHARS = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
CHARS_LENGTH = len(CHARS)
)
const BASE_URL = "http://rbrickis.me/"
func main() {
rand.Seed(time.Now().UnixNano())
m := martini.Classic()
// Handles uploading of images
m.Post("/upload_image", func(response http.ResponseWriter, request *http.Request) {
imgName := saveImage(request)
response.Write([]byte(BASE_URL + "i/" + imgName))
})
m.Get("/i/:id", func(response http.ResponseWriter, request *http.Request, params martini.Params) {
id := params["id"]
fileName := "image/" + id + ".png"
if _, err := os.Open(fileName); os.IsNotExist(err) {
response.WriteHeader(404)
http.ServeFile(response, request, "404.html")
return
}
response.Header().Set("Content-Type", "image/png")
http.ServeFile(response, request, fileName)
})
m.NotFound(func(response http.ResponseWriter, request *http.Request) {
http.ServeFile(response, request, "404.html")
})
m.RunOnAddr(":80")
}
func findFile(dir, fileName string) (*os.File, error) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return nil, errors.New("Provided directory does not exist!")
}
var found *os.File
visit := func(path string, info os.FileInfo, err error) error {
if info.Name() == fileName {
found, _ = os.Open(path)
}
return nil
}
filepath.Walk(dir, visit)
return found, nil
}
// Generate a pseudo-random string of x length
func generateRandomString(x int) string {
result := make([]rune, x)
for i := range result {
result[i] = CHARS[rand.Intn(CHARS_LENGTH)]
}
return string(result)
}