Skip to content

Commit

Permalink
Use Go 1.9 multipart.FileHeader.Size attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur White committed Aug 25, 2017
1 parent 23653f1 commit ccf3198
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Package [check](https://godoc.org/github.com/gowww/check) provides request form
2. Check data:

- From a values map, with [Checker.CheckValues](https://godoc.org/github.com/gowww/check#Checker.CheckValues):

```Go
errs := userChecker.CheckValues(map[string][]string{
"name": {"foobar"},
Expand All @@ -48,7 +48,7 @@ Package [check](https://godoc.org/github.com/gowww/check) provides request form
```

- From an [http.Request](https://golang.org/pkg/net/http/#Request), with [Checker.CheckRequest](https://godoc.org/github.com/gowww/check#Checker.CheckRequest):

```Go
errs := checker.CheckRequest(r)
```
Expand Down
22 changes: 0 additions & 22 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"mime/multipart"
"net/http"
"os"
"strings"
)

Expand Down Expand Up @@ -65,27 +64,6 @@ func (c Checker) CheckRequest(r *http.Request) Errors {
return c.Check(form)
}

func fileSize(file *multipart.FileHeader) (int64, error) {
if file == nil {
return 0, errNoFileProvided
}
// TODO: In next Go versions, use new Size attribute (https://go-review.googlesource.com/c/39223).
f, err := file.Open()
if err != nil {
return 0, err
}
var size int64
switch ft := f.(type) {
case *os.File:
fi, _ := ft.Stat()
size = fi.Size()
default:
size, _ = ft.Seek(0, io.SeekEnd)
f.Seek(0, io.SeekStart) // Reset reader.
}
return size, nil
}

func fileType(file *multipart.FileHeader) (string, error) {
if file == nil {
return "", errNoFileProvided
Expand Down
19 changes: 5 additions & 14 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ func MaxFileSize(max int64) Rule {
return
}
for _, file := range form.File[key] {
size, err := fileSize(file)
if err != nil {
continue
}
if size > max {
if file != nil && file.Size > max {
errs.Add(key, &Error{Error: ErrMaxFileSize, Args: []interface{}{i18n.TransFileSize(max)}})
return
}
Expand Down Expand Up @@ -234,11 +230,7 @@ func MinFileSize(min int64) Rule {
return
}
for _, file := range form.File[key] {
size, err := fileSize(file)
if err != nil {
continue
}
if size < min {
if file != nil && file.Size < min {
errs.Add(key, &Error{Error: ErrMinFileSize, Args: []interface{}{i18n.TransFileSize(min)}})
return
}
Expand Down Expand Up @@ -319,15 +311,14 @@ func RangeFileSize(min, max int64) Rule {
return
}
for _, file := range form.File[key] {
size, err := fileSize(file)
if err != nil {
if file == nil {
continue
}
if size > max {
if file.Size > max {
errs.Add(key, &Error{Error: ErrMaxFileSize, Args: []interface{}{i18n.TransFileSize(max)}})
return
}
if size < min {
if file.Size < min {
errs.Add(key, &Error{Error: ErrMinFileSize, Args: []interface{}{i18n.TransFileSize(min)}})
return
}
Expand Down

0 comments on commit ccf3198

Please sign in to comment.