-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfilesystem.go
90 lines (69 loc) · 1.63 KB
/
filesystem.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
package assets
import (
"bytes"
"net/http"
"os"
"path"
"time"
)
// An in-memory asset file system. The file system implements the
// http.FileSystem interface.
type FileSystem struct {
// A map of directory paths to the files in those directories.
Dirs map[string][]string
// A map of file/directory paths to assets.File types.
Files map[string]*File
// Override loading assets from local path. Useful for development.
LocalPath string
}
func NewFileSystem(dirs map[string][]string, files map[string]*File, localPath string) *FileSystem {
fs := &FileSystem{
Dirs: dirs,
Files: files,
LocalPath: localPath,
}
for _, f := range fs.Files {
f.fs = fs
}
return fs
}
func (f *FileSystem) NewFile(path string, filemode os.FileMode, mtime time.Time, data []byte) *File {
return &File{
Path: path,
FileMode: filemode,
Mtime: mtime,
Data: data,
fs: f,
}
}
// Implementation of http.FileSystem
func (f *FileSystem) Open(p string) (http.File, error) {
p = path.Clean(p)
if len(f.LocalPath) != 0 {
return http.Dir(f.LocalPath).Open(p)
}
if fi, ok := f.Files[p]; ok {
if !fi.IsDir() {
// Make a copy for reading
ret := fi
ret.buf = bytes.NewReader(ret.Data)
return ret, nil
}
return fi, nil
}
return nil, os.ErrNotExist
}
func (f *FileSystem) readDir(p string, index int, count int) ([]os.FileInfo, error) {
if d, ok := f.Dirs[p]; ok {
maxl := index + count
if maxl > len(d) {
maxl = len(d)
}
ret := make([]os.FileInfo, 0, maxl-index)
for i := index; i < maxl; i++ {
ret = append(ret, f.Files[path.Join(p, d[i])])
}
return ret, nil
}
return nil, os.ErrNotExist
}