forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-union.go
141 lines (122 loc) · 2.93 KB
/
http-union.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
140
141
/*
* 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 frontend
import (
"io"
"net/http"
"os"
"strings"
"time"
"github.com/gin-gonic/gin/json"
"github.com/gobuffalo/packr"
)
type UnionHttpFs struct {
boxes []packr.Box
indexFile http.File
}
func NewUnionHttpFs(boxes ...PluginBox) *UnionHttpFs {
var packrs []packr.Box
var allRoots []string
// Build index.json
for _, b := range boxes {
packrs = append(packrs, b.Box)
allRoots = append(allRoots, b.Exposes...)
}
return &UnionHttpFs{
boxes: packrs,
indexFile: NewIndexFile(allRoots),
}
}
func (p *UnionHttpFs) Open(name string) (http.File, error) {
safeName := strings.TrimLeft(name, "/")
if safeName == "index.json" {
return p.indexFile, nil
}
for _, b := range p.boxes {
if o, e := b.Open(safeName); e == nil {
return o, e
}
}
return nil, os.ErrNotExist
}
func NewIndexFile(rootList []string) http.File {
jsonData, _ := json.Marshal(rootList)
return &IndexFile{
data: string(jsonData),
info: fileInfo{
Path: "index.json",
Contents: jsonData,
isDir: false,
modTime: time.Now(),
size: int64(len(string(jsonData))),
},
}
}
type IndexFile struct {
data string
info fileInfo
cursor int64
}
func (i *IndexFile) Close() error {
i.cursor = 0
return nil
}
func (i *IndexFile) Read(p []byte) (n int, err error) {
if i.cursor > 0 {
return 0, io.EOF
}
b, e := strings.NewReader(i.data).Read(p)
i.cursor += int64(len(i.data))
return b, e
}
func (i *IndexFile) Seek(offset int64, whence int) (int64, error) {
return strings.NewReader(i.data).Seek(offset, whence)
}
func (i *IndexFile) Readdir(count int) ([]os.FileInfo, error) {
return []os.FileInfo{i.info}, nil
}
func (i *IndexFile) Stat() (os.FileInfo, error) {
return i.info, nil
}
type fileInfo struct {
Path string
Contents []byte
size int64
modTime time.Time
isDir bool
}
func (f fileInfo) Name() string {
return f.Path
}
func (f fileInfo) Size() int64 {
return f.size
}
func (f fileInfo) Mode() os.FileMode {
return 0444
}
func (f fileInfo) ModTime() time.Time {
return f.modTime
}
func (f fileInfo) IsDir() bool {
return f.isDir
}
func (f fileInfo) Sys() interface{} {
return nil
}