refactoring: now detecting file type using filename from form data. Also, only one accepted content type

This commit is contained in:
Julien Neuhart
2018-04-08 21:49:02 +02:00
parent cdeef33ff7
commit bca02f6198
14 changed files with 119 additions and 400 deletions

View File

@@ -42,18 +42,15 @@ func enforceContentLengthHandler(next http.Handler) http.Handler {
}
// enforceContentTypeHandler checks if the "Content-Type" entry
// from the request's header matches one of the allowed content types.
// from the request's header matches the allowed content type.
func enforceContentTypeHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ct, err := ghttp.FindAuthorizedContentType(r.Header)
if err != nil {
if err := ghttp.CheckAuthorizedContentType(r.Header); err != nil {
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
logger.Error(err)
return
}
r = context.WithContentType(r, ct)
next.ServeHTTP(w, r)
})
}
@@ -61,14 +58,7 @@ func enforceContentTypeHandler(next http.Handler) http.Handler {
// convertHandler is in charge of converting the file(s) from the request to PDF.
func convertHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ct, err := context.GetContentType(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error(err)
return
}
c, err := converter.NewConverter(r, ct)
c, err := converter.NewConverter(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error(err)