-
Notifications
You must be signed in to change notification settings - Fork 25
/
file.go
93 lines (70 loc) · 1.41 KB
/
file.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
package assets
import (
"bytes"
"os"
"path"
"time"
)
// An asset file.
type File struct {
// The full asset file path
Path string
// The asset file mode
FileMode os.FileMode
// The asset modification time
Mtime time.Time
// The asset data. Note that this data might be in gzip compressed form.
Data []byte
fs *FileSystem
buf *bytes.Reader
dirIndex int
}
// Implementation of os.FileInfo
func (f *File) Name() string {
return path.Base(f.Path)
}
func (f *File) Mode() os.FileMode {
return f.FileMode
}
func (f *File) ModTime() time.Time {
return f.Mtime
}
func (f *File) IsDir() bool {
return f.FileMode.IsDir()
}
func (f *File) Size() int64 {
return int64(len(f.Data))
}
func (f *File) Sys() interface{} {
return nil
}
// Implementation of http.File
func (f *File) Close() error {
f.buf = nil
f.dirIndex = 0
return nil
}
func (f *File) Stat() (os.FileInfo, error) {
return f, nil
}
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
if f.IsDir() {
ret, err := f.fs.readDir(f.Path, f.dirIndex, count)
f.dirIndex += len(ret)
return ret, err
} else {
return nil, os.ErrInvalid
}
}
func (f *File) Read(data []byte) (int, error) {
if f.buf == nil {
f.buf = bytes.NewReader(f.Data)
}
return f.buf.Read(data)
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
if f.buf == nil {
f.buf = bytes.NewReader(f.Data)
}
return f.buf.Seek(offset, whence)
}