forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
139 lines (122 loc) · 4.19 KB
/
plugins.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package web is a service for providing additional plugins to PHP frontend
package web
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/pydio/cells/assets"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/service"
"github.com/pydio/cells/common/service/context"
"github.com/pydio/cells/common/service/frontend"
)
func init() {
service.NewService(
service.Name(common.SERVICE_API_NAMESPACE_+common.SERVICE_FRONTPLUGS),
service.Tag(common.SERVICE_TAG_FRONTEND),
service.Description("REST service for providing additional plugins to PHP frontend"),
service.Migrations([]*service.Migration{
{
TargetVersion: service.FirstRun(),
Up: DeployAssets,
},
{
TargetVersion: service.Latest(), // Should be triggered whatever the version if there was a change
Up: OverrideAssets,
},
}),
service.WithGeneric(func(ctx context.Context, cancel context.CancelFunc) (service.Runner, service.Checker, service.Stopper, error) {
cfg := servicecontext.GetConfig(ctx)
port := cfg.Int("port", 9025)
return service.RunnerFunc(func() error {
boxes := frontend.GetRegisteredPluginBoxes()
httpFs := frontend.NewUnionHttpFs(boxes...)
http.Handle("/", http.FileServer(httpFs))
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
return nil
}), service.CheckerFunc(func() error {
return nil
}), service.StopperFunc(func() error {
return nil
}), nil
}),
)
}
// DeployAssets is called at the very first run
func DeployAssets(ctx context.Context) error {
// Assets are deployed at install time, but they could be deployed here?
return nil
}
// OverrideAssets is called whenever there is a version change to update the PHP resources
func OverrideAssets(ctx context.Context) error {
dir := filepath.Join(config.ApplicationDataDir(), "static", "pydio")
if _, e := os.Stat(dir); e != nil {
// Frontend is probably not deployed here, just ignore
return nil
}
// Restore assets inside a new directory
updateDir := filepath.Join(dir, "update")
os.Mkdir(updateDir, 0755)
log.Logger(ctx).Info("Deploying new PHP assets in " + updateDir + "...")
if err, _, _ := assets.RestoreAssets(updateDir, assets.PydioFrontBox, nil, "data"); err != nil {
return err
}
log.Logger(ctx).Info("Deploying new PHP assets in " + updateDir + ": done")
contents, _ := ioutil.ReadDir(updateDir)
for _, info := range contents {
base := info.Name()
orig := filepath.Join(dir, base)
if info.IsDir() {
// Folders : create a .bak version
bakPath := filepath.Join(dir, fmt.Sprintf("%s.bak", base))
if _, e := os.Stat(bakPath); e == nil {
os.RemoveAll(bakPath)
}
log.Logger(ctx).Info("Creating bak version of " + orig)
if err := os.Rename(orig, bakPath); err != nil {
return err
}
} else {
// root files : remove original version
if _, e := os.Stat(orig); e == nil {
os.Remove(orig)
}
}
log.Logger(ctx).Info("Now moving " + info.Name() + " to " + orig)
if err := os.Rename(filepath.Join(updateDir, info.Name()), orig); err != nil {
return err
}
}
// Now remove update folder
if err := os.RemoveAll(updateDir); err != nil {
return err
}
// clear cache (ignore errors)
os.RemoveAll(filepath.Join(dir, "data", "cache"))
// TODO : clear php FMP caches & Apc Cache ?
return nil
}