-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
51 lines (36 loc) · 760 Bytes
/
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
package sources
import (
"io/ioutil"
"os"
"time"
)
type FileSource struct {
Source
prefix string
}
type FileSourceOptions struct {
Prefix string `name:"file.prefix" desc:"The basepath for images."`
}
func NewFileSource(prefix string) (*FileSource, error) {
c := FileSource{
prefix: prefix,
}
return &c, nil
}
func (c *FileSource) Read(key string) (*SourceInfo, error) {
filename := c.prefix + "/" + key
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
sourceInfo := SourceInfo{}
if info, err := f.Stat(); err == nil {
sourceInfo.LastModified = info.ModTime().Format(time.RFC1123Z)
}
sourceInfo.Payload, err = ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return &sourceInfo, nil
}