-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
120 lines (96 loc) · 2.69 KB
/
server.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
package main
import (
"bytes"
"errors"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/unrolled/render"
"log"
"net/http"
"path"
"strconv"
"strings"
"github.com/google/gops/agent"
"github.com/jinzhu/configor"
)
var format = render.New()
type Response struct {
Status bool `json:"status"`
Error string `json:"error"`
}
type AppConfig struct {
Port string `default:"3201"`
Key string
Text TextConfig
UploadLimit int64 `default:"33000000"`
}
type TextConfig struct {
FontDPI float64 `default:"72"`
FontSize float64 `default:"12"`
FontFile string `default:"./fonts/DroidSansMono.ttf"`
}
var Config AppConfig
func main() {
configor.New(&configor.Config{ENVPrefix: "APP"}).Load(&Config, "config.yml")
if err := agent.Listen(agent.Options{}); err != nil {
log.Fatal(err)
}
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Post("/preview", func(w http.ResponseWriter, r *http.Request) {
if Config.Key != "" {
apiKey := r.URL.Query().Get("key")
if Config.Key != apiKey {
format.Text(w, 500, "Access denied")
return
}
}
r.Body = http.MaxBytesReader(w, r.Body, Config.UploadLimit)
r.ParseMultipartForm(Config.UploadLimit)
file, handler, err := r.FormFile("file")
if err != nil {
format.Text(w, 500, "Can't accept the file")
return
}
defer file.Close()
q := r.Form
width, _ := strconv.Atoi(q.Get("width"))
height, _ := strconv.Atoi(q.Get("height"))
name := handler.Filename
ext := strings.ToLower(path.Ext(handler.Filename))
if handler.Filename == "Dockerfile" {
ext = ".docker"
}
var writer = bytes.Buffer{}
var outType = "png"
switch ext {
case ".txt":
err = genTxtPreview(file, &writer, width, height)
case ".json", ".js", ".css", ".html", ".htm", ".yaml", ".yml", ".docker", ".xml", ".ts", ".md", ".ini", ".java", ".go", ".sql", ".sh":
err = genCodePreview(file, &writer, name, width, height)
case ".xls", ".xlsx", ".ods":
err = genOfficeOtherPreview(file, &writer, name, width, height)
case ".doc", ".docx", ".odt", ".rtf":
err = genOfficeDocPreview(file, &writer, name, width, height)
case ".jpg", ".png", ".jpeg", ".tiff", ".svg", ".pdf", ".gif", ".webp":
err = genImagePreview(file, &writer, width, height)
outType = "jpg"
default:
err = errors.New("unsupported file type")
}
if err == nil {
if outType == "png" {
w.Header().Add("Content-type", "image/png")
} else {
w.Header().Add("Content-type", "image/jpg")
}
writer.WriteTo(w)
} else {
log.Println(err.Error())
format.Text(w, 500, err.Error())
}
})
log.Printf("starting service ad port %s", Config.Port)
http.ListenAndServe(":"+Config.Port, r)
}