-
Notifications
You must be signed in to change notification settings - Fork 4
/
local.go
157 lines (139 loc) · 3.38 KB
/
local.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
package local
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
)
type Local struct {
dir string
}
type metadata struct {
Filename string `json:"filename"`
Mimetype string `json:"mimetype"`
Timestamp time.Time `json:"timestamp"`
}
func NewLocal(dir string) (*Local, error) {
st, err := os.Stat(dir)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
if err := os.MkdirAll(dir, 0777); err != nil {
return nil, err
}
st, err = os.Stat(dir)
if err != nil {
return nil, err
}
}
if !st.IsDir() {
return nil, errors.New("local: defined storage directory is not a directory")
}
return &Local{dir: dir}, nil
}
func (l *Local) Name() string {
return "Local"
}
func (l *Local) List() ([]string, error) {
files, err := ioutil.ReadDir(l.dir)
if err != nil {
return nil, err
}
rv := []string{}
for _, file := range files {
if file.IsDir() {
continue
}
if fn := file.Name(); filepath.Ext(fn) == ".json" {
rv = append(rv, fn[:len(fn)-5])
}
}
return rv, nil
}
func (l *Local) Read(id string) (io.ReadCloser, error) {
return os.Open(filepath.Join(l.dir, id))
}
func (l *Local) ReadMetadata(id string) (string, string, int64, time.Time, error) {
fn := filepath.Join(l.dir, id+".json")
fp, err := os.Open(fn)
if err != nil {
return "", "", 0, time.Time{}, err
}
defer fp.Close()
v := &metadata{}
if err := json.NewDecoder(fp).Decode(v); err != nil {
return "", "", 0, time.Time{}, err
}
st, err := os.Stat(filepath.Join(l.dir, id))
if err != nil {
return "", "", 0, time.Time{}, err
}
return v.Filename, v.Mimetype, st.Size(), v.Timestamp, nil
}
func (l *Local) writeJSON(id string, v *metadata) error {
fn := filepath.Join(l.dir, id+".json")
fp, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return err
}
defer fp.Close()
return json.NewEncoder(fp).Encode(v)
}
func (l *Local) deleteJSON(id string) error {
return os.Remove(filepath.Join(l.dir, id+".json"))
}
func (l *Local) Write(id string, r io.ReadSeeker, filename string, mimetype string) (int64, error) {
v := &metadata{
Filename: filename,
Mimetype: mimetype,
Timestamp: time.Now().UTC(),
}
if err := l.writeJSON(id, v); err != nil {
return 0, err
}
fn := filepath.Join(l.dir, id)
fp, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
l.deleteJSON(id)
return 0, err
}
defer fp.Close()
return io.Copy(fp, r)
}
func (l *Local) Delete(id string) error {
err1 := l.deleteJSON(id)
err2 := os.Remove(filepath.Join(l.dir, id))
if err1 != nil && err2 != nil {
return errors.New(err1.Error() + " | " + err2.Error())
}
if err1 != nil {
return err1
}
if err2 != nil {
return err2
}
return nil
}
func (l *Local) Serve(w http.ResponseWriter, r *http.Request, id string, filename string, mimetype string, timestamp time.Time, attachment bool) error {
fn := filepath.Join(l.dir, id)
w.Header().Set("Content-Type", mimetype)
w.Header().Set("X-Content-Type-Options", "nosniff")
if filename != "" {
if attachment {
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
} else {
w.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, filename))
}
}
if !timestamp.IsZero() {
w.Header().Set("Last-Modified", timestamp.UTC().Format(http.TimeFormat))
}
http.ServeFile(w, r, fn)
return nil
}