-
Notifications
You must be signed in to change notification settings - Fork 1
/
filereceiver.go
235 lines (223 loc) · 5.9 KB
/
filereceiver.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"database/sql"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/cenkalti/log"
"github.com/getsentry/raven-go"
)
// FileReceiver implements http.Handler for receiving files from clients in chunks.
type FileReceiver struct {
dir string
log log.Logger
db *sql.DB
}
func newFileReceiver(dir string, logger log.Logger, db *sql.DB) *FileReceiver {
return &FileReceiver{
dir: dir,
log: logger,
db: db,
}
}
func (f *FileReceiver) internalServerError(message string, err error, r *http.Request, w http.ResponseWriter) {
raven.CaptureError(err, nil, &raven.Message{Message: message})
message = message + ": " + err.Error()
f.log.Error(message + "; " + r.URL.Path)
http.Error(w, message, http.StatusInternalServerError)
}
func (f *FileReceiver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(f.dir, r.URL.Path)
switch r.Method {
case http.MethodPost:
err := createFile(path)
if err != nil {
f.internalServerError("cannot create file", err, r, w)
return
}
case http.MethodHead:
fi, err := ReadFileInfo(path)
if err != nil {
f.internalServerError("cannot get offset", err, r, w)
return
}
w.Header().Set("efes-file-offset", strconv.FormatInt(fi.Offset, 10))
case http.MethodPatch:
offset, err := strconv.ParseInt(r.Header.Get("efes-file-offset"), 10, 64)
if err != nil {
http.Error(w, "invalid header: efes-file-offset", http.StatusBadRequest)
return
}
var length int64 = -1
lengthHeader := r.Header.Get("efes-file-length")
if lengthHeader != "" {
length, err = strconv.ParseInt(lengthHeader, 10, 64)
if err != nil {
http.Error(w, "invalid header: efes-file-length", http.StatusBadRequest)
return
}
}
if r.Header.Get("efes-drain") == "" {
ok, err := f.tempfileExists(path)
if err != nil {
f.internalServerError("cannot check tempfile", err, r, w)
return
}
if !ok {
http.Error(w, "tempfile does not exist", http.StatusNotFound)
return
}
}
newOffset, digest, err := saveFile(path, offset, length, r.Body, f.log)
if oerr, ok := err.(*OffsetMismatchError); ok {
// Cannot use http.Error() because we want to set "efes-file-offset" header.
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("efes-file-offset", strconv.FormatInt(oerr.Required, 10))
w.WriteHeader(http.StatusConflict)
_, _ = fmt.Fprint(w, oerr.Error()) // nolint: gas
return
}
if err != nil {
f.internalServerError("cannot save file", err, r, w)
return
}
if digest != nil {
w.Header().Set("efes-file-sha1", hex.EncodeToString(digest.Sha1.Sum(nil)))
w.Header().Set("efes-file-crc32", hex.EncodeToString(digest.CRC32.Sum(nil)))
}
w.Header().Set("efes-file-offset", strconv.FormatInt(newOffset, 10))
case http.MethodDelete:
_, err := ReadExistingFileInfo(path)
if os.IsNotExist(err) {
http.Error(w, "offset file does not exist", http.StatusNotFound)
return
}
if err != nil {
f.internalServerError("cannot read offset file", err, r, w)
return
}
err = DeleteFileInfo(path)
if err != nil {
f.internalServerError("cannot delete offset file", err, r, w)
return
}
err = deleteFile(path)
if err != nil {
f.internalServerError("cannot delete file", err, r, w)
return
}
default:
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
}
func (f *FileReceiver) tempfileExists(fpath string) (bool, error) {
if f.db == nil {
return true, nil
}
fid, err := strconv.ParseInt(strings.SplitN(path.Base(fpath), ".", 2)[0], 10, 64)
if err != nil {
return false, err
}
row := f.db.QueryRow("select fid from tempfile where fid=?", fid)
err = row.Scan(&fid)
if err == sql.ErrNoRows {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func createFile(path string) error {
f, err := os.Create(path)
if os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(path), 0700)
if err != nil {
return err
}
f, err = os.Create(path)
}
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return SaveFileInfo(path, newFileInfo())
}
func deleteFile(path string) error {
return os.Remove(path)
}
func saveFile(path string, offset int64, length int64, r io.Reader, log log.Logger) (int64, *Digest, error) {
var fi *FileInfo
var err error
if offset == 0 {
// File can be saved without a prior POST for creating offset file.
err = createFile(path)
if err != nil {
return 0, nil, err
}
fi = newFileInfo()
} else {
fi, err = ReadFileInfo(path)
if err != nil {
return 0, nil, err
}
if offset != fi.Offset {
return 0, nil, &OffsetMismatchError{offset, fi.Offset}
}
}
f, err := os.OpenFile(path, os.O_WRONLY, 0600)
if os.IsNotExist(err) {
_ = DeleteFileInfo(path)
return 0, nil, &OffsetMismatchError{offset, 0}
}
if err != nil {
return 0, nil, err
}
closed := false
defer func() {
if !closed {
logCloseFile(log, f)
}
}()
_, err = f.Seek(offset, io.SeekStart)
if err != nil {
return 0, nil, err
}
w := io.MultiWriter(f, fi.Digest.CRC32, fi.Digest.Sha1)
n, _ := io.Copy(w, r)
err = f.Sync()
if err != nil {
return 0, nil, err
}
err = f.Close()
closed = true
if err != nil {
return 0, nil, err
}
fi.Offset = offset + n
if fi.Offset == length {
// If we know the length of the file, we can delete the ".offset"
// file without the need of a separate DELETE from the client.
err = DeleteFileInfo(path)
return fi.Offset, &fi.Digest, err
}
return fi.Offset, nil, SaveFileInfo(path, fi)
}
// OffsetMismatchError is returned when the offset specified in request does not match the actual offset on the disk.
type OffsetMismatchError struct {
Given, Required int64
}
func (e *OffsetMismatchError) Error() string {
return fmt.Sprintf("given offset (%d) does not match required offset (%d)", e.Given, e.Required)
}