-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileinfo.go
82 lines (70 loc) · 1.46 KB
/
fileinfo.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
package main
import (
"bytes"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
type fileInfo interface {
Name() string
Size() int64
Open() (io.ReadCloser, error)
}
type systemFileInfo struct {
path string
size int64
}
func (info *systemFileInfo) Name() string {
return filepath.Base(info.path)
}
func (info *systemFileInfo) Size() int64 {
return info.size
}
func (info *systemFileInfo) Open() (io.ReadCloser, error) {
return os.Open(info.path)
}
type bufferFileInfo struct {
name string
buffer bytes.Buffer
}
func (info *bufferFileInfo) Name() string {
return info.name
}
func (info *bufferFileInfo) Size() int64 {
return int64(info.buffer.Len())
}
func (info *bufferFileInfo) Open() (io.ReadCloser, error) {
return io.NopCloser(&info.buffer), nil
}
func readDirectory(dirname string) ([]fileInfo, error) {
fi1, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, err
}
var fi2 []fileInfo
for _, fi := range fi1 {
if !fi.IsDir() && strings.ToLower(fi.Name()) != lmdFilename {
fi2 = append(fi2, &systemFileInfo{
size: fi.Size(),
path: path.Join(dirname, fi.Name()),
})
}
}
return fi2, nil
}
func listFilesToPack(dirname string, database bool, gameID gameID) ([]fileInfo, error) {
if files, err := readDirectory(dirname); err != nil {
return nil, err
} else if database {
lmb, err := lmdWrite(gameID, files)
if err != nil {
return nil, err
}
return append(files, lmb), nil
} else {
return files, nil
}
}