This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
postArchive.go
160 lines (141 loc) · 3.1 KB
/
postArchive.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
package api
import (
"archive/zip"
"compress/flate"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"time"
"github.com/romshark/zipapi/store"
"github.com/pkg/errors"
)
func (srv *server) postArchive(
out http.ResponseWriter,
in *http.Request,
) error {
// Make sure the content-type header is set
contentTypeHeader := in.Header.Get("Content-Type")
if contentTypeHeader == "" {
http.Error(
out,
http.StatusText(http.StatusBadRequest),
http.StatusBadRequest,
)
return nil
}
// Validate content-type
contentType, _, err := mime.ParseMediaType(contentTypeHeader)
if err != nil {
return errors.Wrap(err, "parsing content-type header")
}
if contentType != "multipart/form-data" {
http.Error(
out,
http.StatusText(http.StatusBadRequest),
http.StatusBadRequest,
)
return nil
}
startTime := time.Now()
userAgent := in.Header.Get("User-Agent")
// Limit total form size
in.Body = http.MaxBytesReader(
out,
in.Body,
int64(srv.conf.App.MaxReqSize),
)
// Parse inputs
if err := in.ParseMultipartForm(
int64(srv.conf.App.MaxMultipartMembuf),
); err != nil {
// This is damn ugly, but there seems to be no way around
// comparing the error string
if err.Error() == "http: request body too large" {
http.Error(
out,
"request body too large",
http.StatusBadRequest,
)
return nil
}
return errors.Wrap(err, "parsing multipart/form-data")
}
// Init zip archive writer
arch := zip.NewWriter(out)
defer arch.Close()
arch.RegisterCompressor(
//TODO: make this compression optional
zip.Deflate,
func(out io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(out, flate.BestCompression)
},
)
defer arch.Close()
files := make([]store.File, 0, len(in.MultipartForm.File))
if len(in.MultipartForm.File) < 1 {
// Missing files
http.Error(
out,
http.StatusText(http.StatusBadRequest),
http.StatusBadRequest,
)
return nil
}
for flName, fl := range in.MultipartForm.File {
// Check file size
if uint64(fl[0].Size) > srv.conf.App.MaxFileSize {
http.Error(
out,
fmt.Sprintf(
"file '%s' exceeds max file size (%d)",
flName,
srv.conf.App.MaxFileSize,
),
http.StatusBadRequest,
)
return nil
}
file, err := fl[0].Open()
if err != nil {
return errors.Wrapf(
err,
"opening file '%s' from multipart/form-data",
flName,
)
}
// Read file
contents, err := ioutil.ReadAll(file)
if err != nil {
return errors.Wrapf(
err,
"reading file '%s' multipart/form-data",
flName,
)
}
files = append(files, store.File{
Upload: store.UploadInfo{
Time: startTime,
ClientAgent: userAgent,
},
Name: flName,
Contents: contents,
})
// Add file to archive
fout, err := arch.Create(flName)
if err != nil {
return errors.Wrap(err, "creating archive file")
}
// Write file
_, err = fout.Write(contents)
if err != nil {
return errors.Wrap(err, "copying multipart file to archive")
}
}
// Save file to store
if err := srv.store.SaveFiles(files...); err != nil {
return errors.Wrap(err, "saving files to store")
}
return nil
}