This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
/
file.go
159 lines (138 loc) · 2.65 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Package file provides convenient helpers for files and abstractions of files
package file
import (
"io"
"io/fs"
"strings"
"github.com/spf13/afero"
)
// File abstracts an abstract file
type File struct {
Content io.ReadCloser
Path string
Size int64
// If the content type is not explicitly specified, ContenType will be an empty string.
ContentType string
}
// Iterator is an iterator of files
type Iterator interface {
// Next returns the next File. If there is no next File, returns nil file and nil error
Next() (*File, error)
}
// For debugging
type SimpleIterator struct {
c int
files []File
}
func NewSimpleIterator(files []File) *SimpleIterator {
files2 := make([]File, len(files))
copy(files2, files)
return &SimpleIterator{
files: files2,
}
}
func (s *SimpleIterator) Next() (*File, error) {
if len(s.files) <= s.c {
return nil, nil
}
n := s.files[s.c]
s.c++
return &n, nil
}
type PrefixIterator struct {
a Iterator
prefix string
}
func NewPrefixIterator(a Iterator, prefix string) *PrefixIterator {
return &PrefixIterator{
a: a,
prefix: prefix,
}
}
func (s *PrefixIterator) Next() (*File, error) {
for {
n, err := s.a.Next()
if err != nil {
return nil, err
}
if n == nil {
return nil, nil
}
if s.prefix == "" {
return n, nil
}
if strings.HasPrefix(n.Path, s.prefix+"/") {
n2 := *n
n2.Path = strings.TrimPrefix(n2.Path, s.prefix+"/")
return &n2, nil
}
}
}
type FilteredIterator struct {
a Iterator
skipper func(p string) bool
}
func NewFilteredIterator(a Iterator, skipper func(p string) bool) *FilteredIterator {
return &FilteredIterator{
a: a,
skipper: skipper,
}
}
func (s *FilteredIterator) Next() (*File, error) {
for {
n, err := s.a.Next()
if err != nil {
return nil, err
}
if n == nil {
return nil, nil
}
if !s.skipper(n.Path) {
return n, nil
}
}
}
type FsIterator struct {
fs afero.Fs
files []string
c int
}
func NewFsIterator(afs afero.Fs) (*FsIterator, error) {
var files []string
var size int64
if err := afero.Walk(afs, "", func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
files = append(files, path)
size += info.Size()
return nil
}); err != nil {
return nil, err
}
return &FsIterator{
fs: afs,
files: files,
c: 0,
}, nil
}
func (a *FsIterator) Next() (*File, error) {
if len(a.files) <= a.c {
return nil, nil
}
next := a.files[a.c]
a.c++
fi, err := a.fs.Open(next)
if err != nil {
return nil, err
}
stat, err := fi.Stat()
if err != nil {
return nil, err
}
return &File{
Content: fi,
Path: next,
Size: stat.Size(),
}, nil
}