-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
72 lines (56 loc) · 1.32 KB
/
dir.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
package storage
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
)
var _ Storage = (*dirStorage)(nil)
type dirStorage struct {
dir string
}
func NewDirStorage(dir string) (*dirStorage, error) {
if dir[len(dir)-1] != os.PathSeparator {
dir += string(os.PathSeparator)
}
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
return &dirStorage{dir: dir}, nil
}
func (s *dirStorage) Delete(ctx context.Context, path string) error {
return os.Remove(s.absPath(path))
}
func (s *dirStorage) Upload(ctx context.Context, path string, reader io.ReadSeeker) error {
p := s.absPath(path)
_, err := os.Stat(p)
switch {
case err == nil:
return &ErrObjectAlreadyExists{Key: path}
case err != nil && !os.IsNotExist(err):
return err
}
if err = os.MkdirAll(filepath.Dir(p), 0755); err != nil {
return err
}
file, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR|os.O_SYNC, 0644)
defer file.Close()
if err != nil {
return err
}
reader.Seek(0, io.SeekStart)
if _, err = io.Copy(file, reader); err != nil {
return err
}
return nil
}
func (s *dirStorage) Download(ctx context.Context, path string) (io.ReadCloser, error) {
return os.Open(s.absPath(path))
}
func (s *dirStorage) absPath(path string) string {
return fmt.Sprintf("%s%s", s.dir, path)
}
func (s *dirStorage) Close() error {
return nil
}