diff --git a/config.go b/config.go index 6efc075..e1b0538 100644 --- a/config.go +++ b/config.go @@ -24,6 +24,7 @@ type Config struct { ExifAllowedPaths []string ExifAbortOnError bool FileExpiration bool + MaxFileSizeMB int } func ConfigFromFile(filePath string) (*Config, error) { @@ -48,6 +49,7 @@ func ConfigFromFile(filePath string) (*Config, error) { ExifAllowedPaths: []string{}, ExifAbortOnError: true, FileExpiration: false, + MaxFileSizeMB: 50, } scanner := bufio.NewScanner(file) @@ -152,6 +154,13 @@ func ConfigFromFile(filePath string) (*Config, error) { } retval.FileExpiration = parsed + case "MaxFileSizeMB": + parsed, err := strconv.Atoi(val) + if err != nil { + return nil, err + } + + retval.MaxFileSizeMB = parsed default: return nil, errors.Errorf("unexpected config key: \"%s\"", key) } diff --git a/example.conf b/example.conf index 0f5e6ab..755d5a9 100644 --- a/example.conf +++ b/example.conf @@ -9,3 +9,4 @@ ExifAllowedIds: 0x0112 274 ExifAllowedPaths: IFD/Orientation ExifAbortOnError: true FileExpiration: true +MaxFileSizeMB: 50 diff --git a/uploadhandler.go b/uploadhandler.go index d2c0be2..e8ccc34 100644 --- a/uploadhandler.go +++ b/uploadhandler.go @@ -20,8 +20,8 @@ type uploadHandler struct { func (handler *uploadHandler) PostUploadRedirect(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() - // Limit size to 50Mb - r.Body = http.MaxBytesReader(w, r.Body, 50*1024*1024) + // Limit size to set value in config + r.Body = http.MaxBytesReader(w, r.Body, int64(handler.config.MaxFileSizeMB)*1024*1024) uploadFile, header, err := r.FormFile("file") if err != nil { http.Error(w, "could not read uploaded file: "+err.Error(), http.StatusBadRequest) @@ -81,8 +81,8 @@ func (handler *uploadHandler) PostUploadRedirect(w http.ResponseWriter, r *http. func (handler *uploadHandler) PostUpload(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() - // Limit size to 50Mb - r.Body = http.MaxBytesReader(w, r.Body, 50*1024*1024) + // Limit size to set value in config + r.Body = http.MaxBytesReader(w, r.Body, int64(handler.config.MaxFileSizeMB)*1024*1024) uploadFile, header, err := r.FormFile("file") if err != nil { http.Error(w, "could not read uploaded file: "+err.Error(), http.StatusBadRequest) diff --git a/yaf.go b/yaf.go index 66baa6e..19273cf 100644 --- a/yaf.go +++ b/yaf.go @@ -61,7 +61,8 @@ func main() { } router := httprouter.New() - log.Printf("starting yaf on port %d\n", config.Port) + log.Printf("starting yaf on port: \t%d\n", config.Port) + log.Printf("Maximum File Size: \t%dMB\n", config.MaxFileSizeMB) router.HandlerFunc(http.MethodPost, "/upload", handler.PostUpload) router.HandlerFunc(http.MethodPost, "/uploadweb", handler.PostUploadRedirect) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", config.Port), router))