-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatic-files.go
94 lines (76 loc) · 1.71 KB
/
static-files.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
package steamscreenshots
import (
"os"
//"fmt"
"embed"
"io/fs"
//"io"
"bytes"
"time"
)
//go:embed static templates banners/unknown.jpg
var embeddedContent embed.FS
type staticFiles struct {
files map[string]fs.File
}
func (sf *staticFiles) Open(name string) (fs.File, error) {
file, found := sf.files[name]
if found {
if f, ok := file.(*staticFile); ok {
return f.Open(), nil
}
return file, nil
}
return embeddedContent.Open(name)
//return nil, fs.ErrNotExist
}
func (sf *staticFiles) LoadFromDisk(source, destination string) error {
info, err := os.Stat(source)
if err != nil {
return err
}
raw, err := os.ReadFile(source)
if err != nil {
return err
}
sf.files[destination] = newStaticFile(info, raw)
return nil
}
func (sf *staticFiles) LoadFromFS(source, destination string, filesystem fs.FS) {
}
type staticFile struct {
data []byte
info fs.FileInfo
reader *bytes.Reader
}
func newStaticFile(info fs.FileInfo, data []byte) fs.File {
return &staticFile{
data: data,
info: info,
reader: bytes.NewReader(data),
}
}
func (sf *staticFile) Open() fs.File {
sf.reader.Reset(sf.data)
return sf
}
func (sf *staticFile) Stat() (fs.FileInfo, error) {
return sf.info, nil
}
func (sf *staticFile) Read(buf []byte) (int, error) {
return sf.reader.Read(buf)
}
func (sf *staticFile) Close() error {
return nil
}
type staticInfo struct {
name string
size int64
mode fs.FileMode
modTime time.Time
}
func (si *staticInfo) Name() string { return si.name }
func (si *staticInfo) Size() int64 { return si.size }
func (si *staticInfo) Mode() fs.FileMode { return si.mode }
func (si *staticInfo) IsDir() bool { return false }
func (si *staticInfo) Sys() any { return nil }