-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (115 loc) · 3 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
package main
import (
"fmt"
"github.com/juruen/rmapi/annotations"
"github.com/juruen/rmapi/api"
"github.com/juruen/rmapi/filetree"
"github.com/juruen/rmapi/log"
"github.com/juruen/rmapi/model"
"github.com/juruen/rmapi/util"
"html/template"
"io/ioutil"
"net/http"
"os"
"path"
"sort"
)
type ViewHandler struct {
Api *api.ApiCtx
}
type DownloadHandler struct {
Api *api.ApiCtx
}
type Error struct {
Text string
}
type ViewItem struct {
Name string
Path string
Directory bool
}
type ViewData struct {
Path string
Items []ViewItem
}
func ShowErrorPage(w http.ResponseWriter, err Error) {
t, _ := template.ParseFiles("templates/error.html")
t.Execute(w, err)
}
func (h *DownloadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
docpath := r.URL.Path[len("/download/"):]
node, _ := h.Api.Filetree.NodeByPath(docpath, h.Api.Filetree.Root())
if node.IsFile() {
err := preparePdf(h.Api, node)
if err != nil {
ShowErrorPage(w, Error{Text: fmt.Sprintf("Couldn't create PDF file for %s. %s",
node.Name(), err.Error())})
return
}
http.ServeFile(w, r, fmt.Sprintf("%s.pdf", node.Name()))
defer os.Remove(fmt.Sprintf("%s.pdf", node.Name()))
}
}
func (h *ViewHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/view.html")
root := h.Api.Filetree.Root()
docpath := r.URL.Path[len("/view/"):]
if len(docpath) > 0 {
node, _ := h.Api.Filetree.NodeByPath(docpath, root)
root = node
}
var items []ViewItem
for _, node := range root.Children {
item := ViewItem{Name: node.Name(), Path: path.Clean(docpath), Directory: node.IsDirectory()}
items = append(items, item)
}
if docpath == "" {
docpath = "/"
} else {
item := ViewItem{Name: "Previous level", Path: "..", Directory: false}
items = append(items, item)
}
sort.Slice(items, func(x, y int) bool {
if items[x].Path == ".." {
return true
}
return items[x].Name < items[y].Name
})
data := ViewData{
Path: docpath,
Items: items,
}
t.Execute(w, data)
}
func preparePdf(api *api.ApiCtx, node *model.Node) error {
api.FetchDocument(node.Document.ID, "tmp.zip")
tmp, _ := ioutil.TempDir("", "rmapizip")
err := util.Unzip("tmp.zip", tmp)
if err != nil {
return err
}
defer os.RemoveAll(tmp)
defer os.Remove("tmp.zip")
unzipDir := path.Join(tmp, node.Id())
pdfName := fmt.Sprintf("%s.pdf", node.Name())
generator := annotations.CreatePdfGenerator(unzipDir, pdfName)
err = generator.Generate()
if err != nil {
return err
}
return nil
}
func main() {
log.InitLog()
ctx := api.CreateApiCtx(api.AuthHttpCtx())
filetree.CreateFileTreeCtx()
viewHandler := ViewHandler{Api: ctx}
http.HandleFunc("/view/", viewHandler.ServeHTTP)
downloadHandler := DownloadHandler{Api: ctx}
http.HandleFunc("/download/", downloadHandler.ServeHTTP)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/view/", 301)
})
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css/"))))
http.ListenAndServe(":8080", nil)
}