mirror of
https://github.com/lyx0/yaf.git
synced 2024-11-13 19:49:53 +01:00
set max upload size through the config file instead
This commit is contained in:
parent
4b3adfe1dd
commit
8ed6dbf55c
4 changed files with 16 additions and 5 deletions
|
@ -24,6 +24,7 @@ type Config struct {
|
||||||
ExifAllowedPaths []string
|
ExifAllowedPaths []string
|
||||||
ExifAbortOnError bool
|
ExifAbortOnError bool
|
||||||
FileExpiration bool
|
FileExpiration bool
|
||||||
|
MaxFileSizeMB int
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigFromFile(filePath string) (*Config, error) {
|
func ConfigFromFile(filePath string) (*Config, error) {
|
||||||
|
@ -48,6 +49,7 @@ func ConfigFromFile(filePath string) (*Config, error) {
|
||||||
ExifAllowedPaths: []string{},
|
ExifAllowedPaths: []string{},
|
||||||
ExifAbortOnError: true,
|
ExifAbortOnError: true,
|
||||||
FileExpiration: false,
|
FileExpiration: false,
|
||||||
|
MaxFileSizeMB: 50,
|
||||||
}
|
}
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
@ -152,6 +154,13 @@ func ConfigFromFile(filePath string) (*Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
retval.FileExpiration = parsed
|
retval.FileExpiration = parsed
|
||||||
|
case "MaxFileSizeMB":
|
||||||
|
parsed, err := strconv.Atoi(val)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
retval.MaxFileSizeMB = parsed
|
||||||
default:
|
default:
|
||||||
return nil, errors.Errorf("unexpected config key: \"%s\"", key)
|
return nil, errors.Errorf("unexpected config key: \"%s\"", key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,3 +9,4 @@ ExifAllowedIds: 0x0112 274
|
||||||
ExifAllowedPaths: IFD/Orientation
|
ExifAllowedPaths: IFD/Orientation
|
||||||
ExifAbortOnError: true
|
ExifAbortOnError: true
|
||||||
FileExpiration: true
|
FileExpiration: true
|
||||||
|
MaxFileSizeMB: 50
|
||||||
|
|
|
@ -20,8 +20,8 @@ type uploadHandler struct {
|
||||||
func (handler *uploadHandler) PostUploadRedirect(w http.ResponseWriter, r *http.Request) {
|
func (handler *uploadHandler) PostUploadRedirect(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
// Limit size to 50Mb
|
// Limit size to set value in config
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, 50*1024*1024)
|
r.Body = http.MaxBytesReader(w, r.Body, int64(handler.config.MaxFileSizeMB)*1024*1024)
|
||||||
uploadFile, header, err := r.FormFile("file")
|
uploadFile, header, err := r.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "could not read uploaded file: "+err.Error(), http.StatusBadRequest)
|
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) {
|
func (handler *uploadHandler) PostUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
// Limit size to 50Mb
|
// Limit size to set value in config
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, 50*1024*1024)
|
r.Body = http.MaxBytesReader(w, r.Body, int64(handler.config.MaxFileSizeMB)*1024*1024)
|
||||||
uploadFile, header, err := r.FormFile("file")
|
uploadFile, header, err := r.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "could not read uploaded file: "+err.Error(), http.StatusBadRequest)
|
http.Error(w, "could not read uploaded file: "+err.Error(), http.StatusBadRequest)
|
||||||
|
|
3
yaf.go
3
yaf.go
|
@ -61,7 +61,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
router := httprouter.New()
|
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, "/upload", handler.PostUpload)
|
||||||
router.HandlerFunc(http.MethodPost, "/uploadweb", handler.PostUploadRedirect)
|
router.HandlerFunc(http.MethodPost, "/uploadweb", handler.PostUploadRedirect)
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", config.Port), router))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", config.Port), router))
|
||||||
|
|
Loading…
Reference in a new issue