-
Notifications
You must be signed in to change notification settings - Fork 2
/
folderbundle.go
164 lines (147 loc) · 3.5 KB
/
folderbundle.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package brbundle
import (
"errors"
"fmt"
"github.com/gabriel-vasile/mimetype"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
type folderBundle struct {
baseBundle
rootFolder string
}
func newFolderBundle(rootFolder string, encrypted bool, option Option) *folderBundle {
decryptorType := NotToEncrypto
if encrypted {
decryptorType = UseAES
}
mountPoint := option.MountPoint
if mountPoint != "" && !strings.HasSuffix(mountPoint, "/") {
mountPoint = mountPoint + "/"
}
return &folderBundle{
baseBundle: baseBundle{
mountPoint: mountPoint,
name: option.Name,
decryptorType: decryptorType,
},
rootFolder: rootFolder,
}
}
func (f folderBundle) find(searchPath string) (FileEntry, error) {
filePath := filepath.Join(f.rootFolder, searchPath)
s, err := os.Stat(filePath)
if os.IsNotExist(err) {
return nil, nil
}
decryptor, err := f.baseBundle.getDecryptor()
if err != nil {
return nil, err
}
return &folderFileEntry{
filePath: filePath,
logicalPath: path.Clean("/" + path.Join(f.baseBundle.mountPoint, searchPath)),
info: s,
decryptor: decryptor,
}, nil
}
func (f folderBundle) close() {
// do nothing
}
func (f folderBundle) dirs() []string {
var dirs []string
filepath.Walk(f.rootFolder, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
rel, err := filepath.Rel(f.rootFolder, path)
if err != nil {
return err
}
if rel == "." {
dirs = append(dirs, strings.TrimSuffix(f.mountPoint, "/"))
} else if f.mountPoint == "" {
dirs = append(dirs, strings.ReplaceAll(rel, `\`, "/"))
} else {
dirs = append(dirs, f.mountPoint+strings.ReplaceAll(rel, `\`, "/"))
}
}
return nil
})
return dirs
}
func (f folderBundle) filesInDir(dirName string) []string {
if !strings.HasPrefix(dirName, f.mountPoint) {
return nil
}
dirName = dirName[len(f.mountPoint):]
dirs, err := ioutil.ReadDir(filepath.Join(f.rootFolder, dirName))
if err != nil {
return nil
}
var result []string
for _, dir := range dirs {
if dir.IsDir() {
continue
}
result = append(result, dir.Name())
}
return result
}
type folderFileEntry struct {
filePath string
info os.FileInfo
logicalPath string
contentType string
decryptor Decryptor
}
func (f folderFileEntry) Reader() (io.ReadCloser, error) {
file, err := os.Open(f.filePath)
if err != nil {
return nil, err
}
decryptoReader, err := f.decryptor.Decrypto(file)
if err != nil {
return nil, err
}
return NewReadCloser(decryptoReader, file), nil
}
func (f folderFileEntry) ReadAll() ([]byte, error) {
reader, err := f.Reader()
if err != nil {
return nil, err
}
defer reader.Close()
return ioutil.ReadAll(reader)
}
func (f folderFileEntry) BrotliReader() (io.ReadCloser, error) {
return nil, errors.New("Source data is not compressed by brotli")
}
func (f folderFileEntry) CompressedSize() int64 {
return -1
}
func (f folderFileEntry) Stat() os.FileInfo {
return f.info
}
func (f folderFileEntry) Name() string {
return path.Base(f.filePath)
}
func (f folderFileEntry) Path() string {
return f.logicalPath
}
func (f folderFileEntry) EtagAndContentType() (string, string) {
size := int(f.info.Size())
if f.contentType == "" {
if m, err := mimetype.DetectFile(f.filePath); err != nil {
f.contentType = m.String()
} else {
f.contentType = "application/octet-stream"
}
}
return fmt.Sprintf("%x-%x", size, f.info.ModTime().Unix()), f.contentType
}
func (f folderFileEntry) GetLocalPath() (string, error) {
return f.filePath, nil
}