-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
213 lines (174 loc) · 4.39 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
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/PuerkitoBio/goquery"
"github.com/google/uuid"
"github.com/juruen/rmapi/auth"
"github.com/juruen/rmapi/cloud"
)
const codeEnv string = "RMUPLOADER_CODE"
type server struct {
cli *cloud.Client
addr string
}
// newServer creates a server with an api client with correct authentication
// initiated using the code environment variable.
func newServer(addr string) (server, error) {
s := server{addr: addr}
code, ok := os.LookupEnv(codeEnv)
if !ok {
return s, fmt.Errorf("%s variable is not defined", codeEnv)
}
log.Println("Setting up authentication with the device...")
auth := auth.New()
auth.RegisterDevice(code)
// The default auth uses ~/.rmapi to store credentials
s.cli = cloud.NewClient(auth.Client())
return s, nil
}
func main() {
s, err := newServer(":8080")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
http.HandleFunc("/", s.index)
http.HandleFunc("/upload", s.upload)
http.HandleFunc("/delete", s.delete)
// Statics
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("web/img/"))))
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("web/css/"))))
log.Println("Starting web server...")
if err := http.ListenAndServe(s.addr, logRequest(http.DefaultServeMux)); err != nil {
panic(err)
}
}
func (s server) index(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("web/index.html"))
msg := ""
switch r.Method {
case "GET":
case "POST":
url := r.FormValue("url")
if url == "" {
msg = "url not provided"
break
}
file, err := webpageAsPDF(url)
if err != nil {
log.Fatal(err)
msg = err.Error()
break
}
name, err := titleFromURL(url)
if err != nil {
log.Fatal(err)
msg = err.Error()
break
}
id := uuid.New().String()
if err := s.uploadToRm(id, file, name); err != nil {
log.Fatal(err)
msg = err.Error()
break
}
w.WriteHeader(http.StatusOK)
msg = "webpage has been sent"
default:
w.WriteHeader(http.StatusMethodNotAllowed)
msg = http.StatusText(http.StatusMethodNotAllowed)
}
tmpl.Execute(w, msg)
}
func (s server) upload(w http.ResponseWriter, r *http.Request) {
switch r.Method {
// called using ajax
case "POST":
if r.FormValue("file") == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "missing file parameter")
return
}
f, header, err := r.FormFile("file")
if err != nil {
log.Fatal(err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, err.Error())
return
}
defer f.Close()
file, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, err.Error())
return
}
id := uuid.New().String()
if err := s.uploadToRm(id, file, header.Filename); err != nil {
log.Fatal(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, err.Error())
return
}
// send id as result
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, id)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, http.StatusText(http.StatusMethodNotAllowed))
return
}
}
func (s server) delete(w http.ResponseWriter, r *http.Request) {
switch r.Method {
// called using ajax
case "DELETE":
id, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, err.Error())
return
}
if err := s.deleteFromRm(string(id)); err != nil {
log.Fatal(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, err.Error())
return
}
// send id as result
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(id))
default:
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, http.StatusText(http.StatusMethodNotAllowed))
return
}
}
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
// titleFromURL parses an URL to return a
// more friendly name.
// The extension .pdf is added at the end.
func titleFromURL(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
return "", err
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", err
}
title := doc.Find("title").Text()
return fmt.Sprintf("%s.pdf", title), nil
}