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
|
package disk
import (
"context"
"errors"
"fmt"
"io"
"gitlab.com/gitlab-org/gitlab-pages/internal/vfs"
)
var (
// Make sure lazyFile is a vfs.File to support vfs.ServeCompressedFile
// if the file is compressed.
// This should always be satisfied because root.Open always returns
// a vfs.File.
_ vfs.File = &lazyFile{}
// Make sure lazyFile is a ReadSeeker to support http.ServeContent
// if the file is not compressed.
// Note: lazyFile.Seek only works if the underlying root.Open returns
// a vfs.SeekableFile which is the case if the file is not compressed.
_ io.ReadSeeker = &lazyFile{}
// ErrInvalidSeeker is returned if lazyFile.Seek is called and the
// underlying file is not seekable
ErrInvalidSeeker = errors.New("file is not seekable")
)
type lazyFile struct {
f vfs.File
err error
load func() (vfs.File, error)
}
func lazyOpen(ctx context.Context, root vfs.Root, fullPath string) lazyFile {
lf := lazyFile{
load: func() (vfs.File, error) {
return root.Open(ctx, fullPath)
},
}
return lf
}
func (lf lazyFile) Read(p []byte) (int, error) {
if lf.f == nil && lf.err == nil {
lf.f, lf.err = lf.load()
}
if lf.err != nil {
return 0, lf.err
}
return lf.f.Read(p)
}
func (lf lazyFile) Close() error {
if lf.f != nil {
return lf.f.Close()
}
return nil
}
func (lf lazyFile) Seek(offset int64, whence int) (int64, error) {
if lf.f == nil && lf.err == nil {
lf.f, lf.err = lf.load()
}
if lf.err != nil {
return 0, lf.err
}
if sf, ok := lf.f.(io.ReadSeeker); ok {
return sf.Seek(offset, whence)
}
return 0, fmt.Errorf("unable to seek from %T: %w", lf.f, ErrInvalidSeeker)
}
|