forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.libreoffice.go
134 lines (114 loc) · 3.25 KB
/
editor.libreoffice.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
package modifiers
import (
"bytes"
"context"
"fmt"
"html/template"
"net/url"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/caddy"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/plugins"
)
var (
editorLibreOfficeTemplate *template.Template
editorLibreOfficeTemplateStr = `
pydioproxy /wopi/ {{.WOPI}} {
transparent
header_upstream Host {{if .Site.ExternalHost}}{{.Site.ExternalHost}}{{else}}{host}{{end}}
header_upstream X-Real-IP {remote}
header_upstream X-Forwarded-Proto {scheme}
}
{{if .Enabled}}
proxy /{{.LeafletURI}}/ {{.Collabora.Scheme}}://{{.Collabora.Host}}/{{.LeafletURI}} {
transparent
insecure_skip_verify
without /{{.LeafletURI}}/
}
proxy /hosting/discovery {{.Collabora.Scheme}}://{{.Collabora.Host}}/hosting/discovery {
transparent
insecure_skip_verify
without /hosting/discovery
}
proxy /{{.WebsocketURI}}/ {{.Collabora.Scheme}}://{{.Collabora.Host}}/{{.WebsocketURI}}/ {
transparent
insecure_skip_verify
websocket
without /{{.WebsocketURI}}/
}
{{end}}
`
)
type EditorLibreOffice struct {
Enabled bool
WOPI string
Collabora *url.URL
Site caddy.SiteConf
LeafletURI string
WebsocketURI string
}
func init() {
plugins.Register("main", func(ctx context.Context) {
caddy.RegisterPluginTemplate(
play,
[]string{"frontend", "plugin", "editor.libreoffice"},
"/wopi/",
"/loleaflet/",
"/browser/",
"/hosting/discovery",
"/lool/",
"/cool/",
)
tmpl, err := template.New("caddyfile").Funcs(caddy.FuncMap).Parse(editorLibreOfficeTemplateStr)
if err != nil {
log.Fatal("Could not read template ", zap.Error(err))
}
editorLibreOfficeTemplate = tmpl
})
}
func play(site ...caddy.SiteConf) (*bytes.Buffer, error) {
data := &EditorLibreOffice{
WOPI: common.ServiceGatewayWopi,
}
if len(site) > 0 {
data.Site = site[0]
}
if enabled, u, version, err := getCollaboraConfig(); err != nil {
log.Error("could not retrieve collabora config", zap.Any("error ", err))
return nil, err
} else {
data.Enabled = enabled
data.Collabora = u
if version == "v6" {
data.LeafletURI = "leaflet"
data.WebsocketURI = "lool"
} else {
data.LeafletURI = "browser"
data.WebsocketURI = "cool"
}
}
buf := bytes.NewBuffer([]byte{})
if err := editorLibreOfficeTemplate.Execute(buf, data); err != nil {
return nil, err
}
return buf, nil
}
func getCollaboraConfig() (bool, *url.URL, string, error) {
pconf := config.Get("frontend", "plugin", "editor.libreoffice")
enabled := pconf.Val(config.KeyFrontPluginEnabled).Default(false).Bool()
tls := pconf.Val("LIBREOFFICE_SSL").Default(true).Bool()
host := pconf.Val("LIBREOFFICE_HOST").Default("localhost").String()
port := pconf.Val("LIBREOFFICE_PORT").Default("9980").String()
version := pconf.Val("LIBREOFFICE_CODE_VERSION").Default("v6").String()
scheme := "http"
if tls {
scheme = "https"
}
u, err := url.Parse(fmt.Sprintf("%s://%s:%s", scheme, host, port))
if err != nil {
return false, nil, "", err
}
return enabled, u, version, nil
}