-
Notifications
You must be signed in to change notification settings - Fork 8
/
api_text.go
114 lines (103 loc) · 2.7 KB
/
api_text.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
package shortpaste
import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"text/template"
)
func (app *App) handleText(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
app.handleGetText(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func (app *App) handleGetTexts(w http.ResponseWriter, r *http.Request) {
var texts []Text
app.db.Find(&texts)
json.NewEncoder(w).Encode(map[string][]Text{"texts": texts})
}
func (app *App) handleGetText(w http.ResponseWriter, r *http.Request) {
id := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/t/"), "/")
if id == "" {
onNotFound(w, "No ID found in request")
return
}
var text Text
if err := app.db.First(&text, "id = ?", id).Error; err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Text for `%s` not found!\n", id)
return
}
filePath := path.Join(app.storagePath, "data", "texts", text.ID+"."+text.Type)
if _, ok := r.URL.Query()["download"]; ok {
w.Header().Set("Content-Disposition", "attachment; filename="+text.ID+"."+text.Type)
http.ServeFile(w, r, filePath)
return
}
t, err := template.ParseFS(templateFS, "templates/text.html")
if err != nil {
onServerError(w, err, "failed to parse template")
return
}
textContent, err := ioutil.ReadFile(filePath)
if err != nil {
onServerError(w, err, "failed to read text")
return
}
var highlight string
if text.NoHighlight {
highlight = "language-plaintext"
}
data := struct {
ID string
Class string
Text string
}{
ID: text.ID,
Class: highlight,
Text: html.EscapeString(string(textContent)),
}
t.Execute(w, data)
text.HitCount += 1
app.db.Save(&text)
}
func (app *App) handleCreateText(w http.ResponseWriter, r *http.Request) {
text := Text{}
if err := json.NewDecoder(r.Body).Decode(&text); err != nil {
onClientError(w, err, "check the input and try again")
return
}
id := strings.TrimPrefix(r.URL.Path, "/l/")
if id != "" && text.ID == "" {
text.ID = id
}
if err := text.validate(); err != nil {
onClientError(w, err, "check the input and try again")
return
}
if text.Type == "" {
text.Type = "txt"
}
filePath := path.Join(app.storagePath, "data", "texts", text.ID+"."+text.Type)
if err := os.MkdirAll(path.Dir(filePath), 0700); err != nil {
onServerError(w, err, "failed to create folder")
return
}
if err := ioutil.WriteFile(filePath, []byte(text.Text), 0600); err != nil {
onServerError(w, err, "failed to copy contents to disk")
return
}
if err := app.db.Create(&text).Error; err != nil {
onServerError(w, err, "failed to create DB entry")
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{"message": "created"})
}