-
Notifications
You must be signed in to change notification settings - Fork 207
/
glick.go
120 lines (103 loc) · 3.07 KB
/
glick.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
// 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 plugins manages the plug-in componenets of the Documize system.
package plugins
import (
"bytes"
"io/ioutil"
"time"
"github.com/documize/community/core/api/convert/apidocumizecom"
"github.com/documize/community/core/api/convert/documizeapi"
"github.com/documize/community/core/api/convert/html"
"github.com/documize/community/core/api/convert/md"
api "github.com/documize/community/core/convapi"
"github.com/documize/community/domain"
"github.com/documize/glick"
)
// PluginFile is the path to the file containing the configuration information for the plugin system in JSON format.
var PluginFile = "DB" // this points to the database
var insecure = "false"
type infoLog struct{}
func (i infoLog) Write(b []byte) (int, error) {
// log.Info(string(b))
return len(b), nil
}
type errorLog struct{}
func (i errorLog) Write(b []byte) (int, error) {
// log.ErrorString(string(b))
return len(b), nil
}
// Lib holds a pointer to the global glick Library for the Documize app.
var Lib *glick.Library
// Setup configures the global library at Lib,
// largely based on the "config.json" file. It should be called only once.
func Setup(s *domain.Store) error {
if insecure == "true" {
glick.InsecureSkipVerifyTLS = true
}
var err error
Lib, err = glick.New(nil)
if err != nil {
return err
}
ppo := func() interface{} { return interface{}(&api.DocumentConversionResponse{}) }
err = Lib.RegAPI("Convert", &api.DocumentConversionRequest{}, ppo, 2*time.Minute)
if err != nil {
return err
}
for _, xtn := range []string{"htm", "html"} {
err = Lib.RegPlugin("Convert", xtn, html.Convert, nil)
if err != nil {
return err
}
}
for _, xtn := range []string{"md", "markdown"} {
err = Lib.RegPlugin("Convert", xtn, md.Convert, nil)
if err != nil {
return err
}
}
for _, xtn := range []string{"doc", "docx"} {
err = Lib.RegPlugin("Convert", xtn, apidocumizecom.MSwordConvert, nil)
if err != nil {
return err
}
}
err = Lib.RegPlugin("Convert", "documizeapi", documizeapi.Convert, nil)
if err != nil {
return err
}
ppoe := func() interface{} { return interface{}(&api.DocumentExport{}) }
err = Lib.RegAPI("Export", []byte{}, ppoe, 5*time.Minute)
if err != nil {
return err
}
var json = make([]byte, 0)
if PluginFile == "DB" {
c, _ := s.Setting.Get("FILEPLUGINS", "")
json = []byte(c)
if len(bytes.TrimSpace(json)) == 0 {
return nil // don't fail if the DB does not exist yet
}
} else {
json, err = ioutil.ReadFile(PluginFile)
if err != nil {
json = []byte(" [ ] \n")
err = nil
}
}
err = Lib.Configure(json)
if err != nil {
//fmt.Println("DEBUG plugin: "+string(json))
return err
}
return Lib.StartLocalRPCservers(infoLog{}, errorLog{})
}