-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
53 lines (39 loc) · 866 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
52
53
package docstore
import (
"io"
"os"
)
var _ Storer = File{}
// File defines a document service backed by a local file system
type File struct {
dir string
}
// NewFileStore instantiates Storer backed by a local directory
func NewFileStore(dir string) File {
return File{
dir: dir,
}
}
func (f File) Put(obj CreateObj) (string, error) {
filePointer, err := os.Create(f.dir + obj.Identifier)
if err != nil {
return "", ConnectionError
}
defer filePointer.Close()
_, readerr := io.Copy(filePointer, obj)
if readerr != nil {
return "", IOError
}
return obj.Identifier, nil
}
func (f File) Get(fileName string) (RetrieveObj, error) {
filePointer, err := os.Open(f.dir + fileName)
if err != nil {
return RetrieveObj{}, EntryNotFoundError
}
doc := RetrieveObj{
ReadCloser: filePointer,
Identifier: fileName,
}
return doc, nil
}