-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
buffered_file.go
67 lines (53 loc) · 1.23 KB
/
buffered_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
package uiserver
import (
"bytes"
"errors"
"os"
"time"
)
// bufferedFile implements http.File and allows us to modify a file from disk by
// writing out the new version into a buffer and then serving file reads from
// that.
type bufferedFile struct {
buf *bytes.Reader
info os.FileInfo
}
func newBufferedFile(buf []byte, info os.FileInfo) *bufferedFile {
return &bufferedFile{
buf: bytes.NewReader(buf),
info: info,
}
}
func (t *bufferedFile) Read(p []byte) (n int, err error) {
return t.buf.Read(p)
}
func (t *bufferedFile) Seek(offset int64, whence int) (int64, error) {
return t.buf.Seek(offset, whence)
}
func (t *bufferedFile) Close() error {
return nil
}
func (t *bufferedFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, errors.New("not a directory")
}
func (t *bufferedFile) Stat() (os.FileInfo, error) {
return t, nil
}
func (t *bufferedFile) Name() string {
return t.info.Name()
}
func (t *bufferedFile) Size() int64 {
return int64(t.buf.Len())
}
func (t *bufferedFile) Mode() os.FileMode {
return t.info.Mode()
}
func (t *bufferedFile) ModTime() time.Time {
return t.info.ModTime()
}
func (t *bufferedFile) IsDir() bool {
return false
}
func (t *bufferedFile) Sys() interface{} {
return nil
}