-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
102 lines (85 loc) · 1.82 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
94
95
96
97
98
99
100
101
102
package goblet
import (
"bytes"
"os"
"path"
"time"
)
// File file system model for each files
type File struct {
Path string
Data []byte
FileMode os.FileMode
ModifiedAt time.Time
// implementation for io.Reader and io.Seeker
buf *bytes.Reader
}
// NewFromFileInfo return new file
func NewFromFileInfo(fi os.FileInfo, p string, d []byte) *File {
return &File{
Path: p,
Data: d,
FileMode: fi.Mode(),
ModifiedAt: fi.ModTime(),
}
}
// NewFile return new file
func NewFile(p string, d []byte, m os.FileMode, mat time.Time) *File {
return &File{
Path: p,
Data: d,
FileMode: m,
ModifiedAt: mat,
}
}
// Name implement for os.FileInfo
func (f *File) Name() string {
return path.Base(f.Path)
}
// Size implement for os.FileInfo
func (f *File) Size() int64 {
return int64(len(f.Data))
}
// Mode implement for os.FileInfo
func (f *File) Mode() os.FileMode {
return f.FileMode
}
// ModTime implement for os.FileInfo
func (f *File) ModTime() time.Time {
return f.ModifiedAt
}
// IsDir implement for os.FileInfo
func (f *File) IsDir() bool {
return f.FileMode.IsDir()
}
// Sys implement for os.FileInfo
func (f *File) Sys() interface{} {
return nil
}
// Read for io.Reader
func (f *File) Read(data []byte) (int, error) {
if f.buf == nil {
f.buf = bytes.NewReader(f.Data)
}
return f.buf.Read(data)
}
// Seek for io.Seeker
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)
}
// Close implement for io.Closer
func (f *File) Close() error {
f.buf = nil
return nil
}
// Readdir implement for http.File
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
}
// Stat implement for http.File
func (f *File) Stat() (os.FileInfo, error) {
return f, nil
}