forked from documize/community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plantuml.go
121 lines (99 loc) · 3.06 KB
/
plantuml.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
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package plantuml
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
"github.com/documize/community/domain/store"
)
// Provider represents PlantUML Text Diagram
type Provider struct {
Runtime *env.Runtime
Store *store.Store
}
// Meta describes us
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}
section.ID = "f1067a60-45e5-40b5-89f6-aa3b03dd7f35"
section.Title = "PlantUML Diagram"
section.Description = "Diagrams generated from text"
section.ContentType = "plantuml"
section.PageType = "tab"
section.Order = 9990
return section
}
// Command stub.
func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
provider.WriteMessage(w, "plantuml", "missing method name")
return
}
switch method {
case "preview":
var payload struct {
Data string `json:"data"`
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
provider.WriteMessage(w, "plantuml", "Bad payload")
return
}
err = json.Unmarshal(body, &payload)
if err != nil {
provider.WriteMessage(w, "plantuml", "Cannot unmarshal")
return
}
// Generate diagram if we have data.
var diagram string
if len(payload.Data) > 0 {
diagram = p.generateDiagram(ctx, payload.Data)
}
payload.Data = diagram
provider.WriteJSON(w, payload)
return
}
provider.WriteEmpty(w)
}
// Render returns data as-is (HTML).
func (p *Provider) Render(ctx *provider.Context, config, data string) string {
return p.generateDiagram(ctx, data)
}
// Refresh just sends back data as-is.
func (*Provider) Refresh(ctx *provider.Context, config, data string) string {
return data
}
func (p *Provider) generateDiagram(ctx *provider.Context, data string) string {
org, _ := p.Store.Organization.GetOrganization(ctx.Request, ctx.OrgID)
var transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // TODO should be glick.InsecureSkipVerifyTLS (from -insecure flag) but get error: x509: certificate signed by unknown authority
}}
client := &http.Client{Transport: transport}
resp, _ := client.Post(org.ConversionEndpoint+"/api/plantuml", "application/text; charset=utf-8", bytes.NewReader([]byte(data)))
defer func() {
if e := resp.Body.Close(); e != nil {
fmt.Println("resp.Body.Close error: " + e.Error())
}
}()
png, _ := ioutil.ReadAll(resp.Body)
pngEncoded := base64.StdEncoding.EncodeToString(png)
return string(fmt.Sprintf("data:image/png;base64,%s", pngEncoded))
}