-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_stream.go
51 lines (45 loc) · 1.06 KB
/
file_stream.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
package utils
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
// The FileStream implements the stream interface for files on disk.
//
// It provides the stream length by reading the file stats.
// The file name is used for the stream name.
type FileStream struct {
name string
path string
}
func (s FileStream) Name() string {
return s.name
}
func (s FileStream) Size() (int64, error) {
fileStat, err := os.Stat(s.path)
if err != nil && errors.Is(err, os.ErrNotExist) {
return -1, fmt.Errorf("File '%s' not found", s.path)
}
if err != nil {
return -1, fmt.Errorf("Error reading file size '%s': %w", s.path, err)
}
return fileStat.Size(), nil
}
func (s FileStream) Data() (io.ReadCloser, error) {
file, err := os.Open(s.path)
if err != nil && errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("File '%s' not found", s.path)
}
if err != nil {
return nil, fmt.Errorf("Error reading file '%s': %w", s.path, err)
}
return file, nil
}
func NewFileStream(path string) *FileStream {
return &FileStream{
name: filepath.Base(path),
path: path,
}
}