-
Notifications
You must be signed in to change notification settings - Fork 0
/
filereader.go
171 lines (139 loc) · 4.96 KB
/
filereader.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
160
161
162
163
164
165
166
167
168
169
170
171
package fs
import (
"context"
"fmt"
"io"
"github.com/ungerik/go-fs/fsimpl"
)
type FileReader interface {
// String returns the name and meta information for the FileReader.
String() string
// Name returns the name of the file
Name() string
// Ext returns the extension of file name including the point, or an empty string.
Ext() string
// LocalPath returns the cleaned local file-system path of the file backing the FileReader,
// or an empty string if the FileReader is not backed by a local file.
LocalPath() string
// Size returns the size of the file
Size() int64
// Exists returns if file or data for the implementation exists
Exists() bool
// CheckExists return an ErrDoesNotExist error
// if the file does not exist.
CheckExists() error
// ContentHash returns the DefaultContentHash for the file.
ContentHash() (string, error)
// ContentHashContext returns the DefaultContentHash for the file.
ContentHashContext(ctx context.Context) (string, error)
// ReadAll reads and returns all bytes of the file
ReadAll() (data []byte, err error)
// ReadAllContext reads and returns all bytes of the file
ReadAllContext(context.Context) (data []byte, err error)
// ReadAllContentHash reads and returns all bytes of the file
// together with the DefaultContentHash.
ReadAllContentHash(context.Context) (data []byte, hash string, err error)
// ReadAllString reads the complete file and returns the content as string.
ReadAllString() (string, error)
// ReadAllStringContext reads the complete file and returns the content as string.
ReadAllStringContext(context.Context) (string, error)
// WriteTo implements the io.WriterTo interface
WriteTo(writer io.Writer) (n int64, err error)
// OpenReader opens the file and returns a ReadCloser that has to be closed after reading
OpenReader() (ReadCloser, error)
// OpenReadSeeker opens the file and returns a ReadSeekCloser.
// Use OpenReader if seeking is not necessary because implementations
// may need additional buffering to support seeking or not support it at all.
OpenReadSeeker() (ReadSeekCloser, error)
// ReadJSON reads and unmarshalles the JSON content of the file to output.
ReadJSON(ctx context.Context, output any) error
// ReadXML reads and unmarshalles the XML content of the file to output.
ReadXML(ctx context.Context, output any) error
// GobEncode reads and gob encodes the file name and content,
// implementing encoding/gob.GobEncoder.
GobEncode() ([]byte, error)
}
// AsFileReaders converts the passed files to []FileReader.
// If the passed files are already of type []FileReader
// then they are returned as is.
func AsFileReaders[F FileReader](files []F) []FileReader {
if len(files) == 0 {
return nil
}
fileReaders, ok := any(files).([]FileReader)
if ok {
return fileReaders
}
fileReaders = make([]FileReader, len(files))
for i, memFile := range files {
fileReaders[i] = memFile
}
return fileReaders
}
// FileReaderNameIndex returns the slice index of the first FileReader
// with the passed filename returned from its Name method
// or -1 in case of no match.
func FileReaderNameIndex(fileReades []FileReader, filename string) int {
for i, f := range fileReades {
if f.Name() == filename {
return i
}
}
return -1
}
// FileReaderLocalPathIndex returns the slice index of the first FileReader
// with the passed localPath returned from its LocalPath method
// or -1 in case of no match.
func FileReaderLocalPathIndex(fileReades []FileReader, localPath string) int {
for i, f := range fileReades {
if f.LocalPath() == localPath {
return i
}
}
return -1
}
// FileReaderContentHashIndex returns the slice index of the first FileReader
// with the passed hash returned from its ContentHashContext method
// or -1 in case of no match.
func FileReaderContentHashIndex(ctx context.Context, fileReades []FileReader, hash string) (int, error) {
for i, f := range fileReades {
fFash, err := f.ContentHashContext(ctx)
if err != nil {
return -1, err
}
if fFash == hash {
return i, nil
}
}
return -1, nil
}
// FileReaderNotExistsIndex returns the slice index of the first FileReader
// where the Exists method returned false
// or -1 in case of no match.
func FileReaderNotExistsIndex(fileReades []FileReader, filename string) int {
for i, f := range fileReades {
if !f.Exists() {
return i
}
}
return -1
}
// FileReaderWithName returns a new FileReader that wraps the passed fileReader,
// but the Name() method returns the passed name instead of name of the wrapped fileReader.
func FileReaderWithName(fileReader FileReader, name string) FileReader {
return &fileReaderWithName{FileReader: fileReader, name: name}
}
type fileReaderWithName struct {
FileReader
name string
}
// String implements the fmt.Stringer interface.
func (f *fileReaderWithName) String() string {
return fmt.Sprintf("%s -> %s", f.name, f.FileReader.String())
}
func (f *fileReaderWithName) Name() string {
return f.name
}
func (f *fileReaderWithName) Ext() string {
return fsimpl.Ext(f.name, "")
}