-
Notifications
You must be signed in to change notification settings - Fork 3
/
fs.go
69 lines (54 loc) · 1.17 KB
/
fs.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
package main
import (
"net/http"
"os"
"strings"
)
type dotFileHidingFile struct {
http.File
}
func (f dotFileHidingFile) Readdir(n int) ([]os.FileInfo, error) {
files, err := f.File.Readdir(n)
if err != nil {
return nil, err
}
var filteredFiles []os.FileInfo
for _, file := range files {
if !strings.HasPrefix(file.Name(), ".") {
filteredFiles = append(filteredFiles, file)
}
}
return filteredFiles, nil
}
type dotFileHidingFileSystem struct {
http.FileSystem
}
func (fs dotFileHidingFileSystem) Open(name string) (http.File, error) {
if containsDotFile(name) {
return nil, os.ErrNotExist
}
file, err := fs.FileSystem.Open(name)
if err != nil {
return nil, err
}
return dotFileHidingFile{file}, err
}
func (fs dotFileHidingFileSystem) OpenWithStat(name string) (http.File, os.FileInfo, error) {
file, err := fs.Open(name)
if err != nil {
return nil, nil, err
}
fileInfo, err := file.Stat()
if err != nil {
return nil, nil, err
}
return dotFileHidingFile{file}, fileInfo, nil
}
func containsDotFile(name string) bool {
for _, file := range strings.Split(name, "/") {
if strings.HasPrefix(file, ".") {
return true
}
}
return false
}