Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Commit

Permalink
More type-safety for multipartFile
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Dec 12, 2018
1 parent dfb4931 commit a6bcde3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 47 deletions.
16 changes: 8 additions & 8 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ anotherfile
if part == nil || err != nil {
t.Fatal("Expected non-nil part, nil error")
}
mpname, mpf, err := newFileFromPart("", part, mpReader)
mpname, mpf, err := newFileFromPart("", part, &peekReader{r: mpReader})
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
t.Fatal("Expected non-nil multipartFile, nil error")
}
mf, ok := mpf.(File)
if !ok {
Expand All @@ -118,9 +118,9 @@ anotherfile
if part == nil || err != nil {
t.Fatal("Expected non-nil part, nil error")
}
mpname, mpf, err = newFileFromPart("", part, mpReader)
mpname, mpf, err = newFileFromPart("", part, &peekReader{r: mpReader})
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
t.Fatal("Expected non-nil multipartFile, nil error")
}
md, ok := mpf.(Directory)
if !ok {
Expand All @@ -138,9 +138,9 @@ anotherfile
if part == nil || err != nil {
t.Fatal("Expected non-nil part, nil error")
}
mpname, mpf, err = newFileFromPart("dir/", part, mpReader)
mpname, mpf, err = newFileFromPart("dir/", part, &peekReader{r: mpReader})
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
t.Fatal("Expected non-nil multipartFile, nil error")
}
mf, ok = mpf.(File)
if !ok {
Expand All @@ -161,9 +161,9 @@ anotherfile
if part == nil || err != nil {
t.Fatal("Expected non-nil part, nil error")
}
mpname, mpf, err = newFileFromPart("dir/", part, mpReader)
mpname, mpf, err = newFileFromPart("dir/", part, &peekReader{r: mpReader})
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
t.Fatal("Expected non-nil multipartFile, nil error")
}
ms, ok := mpf.(*Symlink)
if !ok {
Expand Down
6 changes: 3 additions & 3 deletions multifilereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestOutput(t *testing.T) {
}
mpname, mpf, err := newFileFromPart("", part, mpReader)
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
t.Fatal("Expected non-nil multipartFile, nil error")
}
mpr, ok := mpf.(File)
if !ok {
Expand All @@ -136,7 +136,7 @@ func TestOutput(t *testing.T) {
}
mpname, mpf, err = newFileFromPart("", part, mpReader)
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
t.Fatal("Expected non-nil multipartFile, nil error")
}
mpd, ok := mpf.(Directory)
if !ok {
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestOutput(t *testing.T) {
}
mpname, mpf, err = newFileFromPart("", part, mpReader)
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
t.Fatal("Expected non-nil multipartFile, nil error")
}
if mpname != "file.txt" {
t.Fatal("Expected filename to be \"b.txt\"")
Expand Down
65 changes: 29 additions & 36 deletions multipartfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ const (
var ErrPartOutsideParent = errors.New("file outside parent dir")
var ErrPartInChildTree = errors.New("file in child tree")

// MultipartFile implements Node, and is created from a `multipart.Part`.
type MultipartFile struct {
// multipartFile implements Node, and is created from a `multipart.Part`.
type multipartFile struct {
Node

Part *multipart.Part
Reader PartReader
Mediatype string
part *multipart.Part
reader *peekReader
mediatype string
}

func NewFileFromPartReader(reader *multipart.Reader, mediatype string) (Directory, error) {
if !isDirectory(mediatype) {
return nil, ErrNotDirectory
}

f := &MultipartFile{
Reader: &peekReader{r: reader},
Mediatype: mediatype,
f := &multipartFile{
reader: &peekReader{r: reader},
mediatype: mediatype,
}

return f, nil
}

func newFileFromPart(parent string, part *multipart.Part, reader PartReader) (string, Node, error) {
f := &MultipartFile{
Part: part,
Reader: reader,
func newFileFromPart(parent string, part *multipart.Part, reader *peekReader) (string, Node, error) {
f := &multipartFile{
part: part,
reader: reader,
}

dir, base := path.Split(f.fileName())
Expand Down Expand Up @@ -89,12 +89,12 @@ func newFileFromPart(parent string, part *multipart.Part, reader PartReader) (st
}

var err error
f.Mediatype, _, err = mime.ParseMediaType(contentType)
f.mediatype, _, err = mime.ParseMediaType(contentType)
if err != nil {
return "", nil, err
}

if !isDirectory(f.Mediatype) {
if !isDirectory(f.mediatype) {
return base, &ReaderFile{
reader: part,
abspath: part.Header.Get("abspath"),
Expand All @@ -109,7 +109,7 @@ func isDirectory(mediatype string) bool {
}

type multipartIterator struct {
f *MultipartFile
f *multipartFile

curFile Node
curName string
Expand All @@ -125,13 +125,13 @@ func (it *multipartIterator) Node() Node {
}

func (it *multipartIterator) Next() bool {
if it.f.Reader == nil {
if it.f.reader == nil {
return false
}
var part *multipart.Part
for {
var err error
part, err = it.f.Reader.NextPart()
part, err = it.f.reader.NextPart()
if err != nil {
if err == io.EOF {
return false
Expand All @@ -140,7 +140,7 @@ func (it *multipartIterator) Next() bool {
return false
}

name, cf, err := newFileFromPart(it.f.fileName(), part, it.f.Reader)
name, cf, err := newFileFromPart(it.f.fileName(), part, it.f.reader)
if err == ErrPartOutsideParent {
break
}
Expand All @@ -152,46 +152,39 @@ func (it *multipartIterator) Next() bool {
}
}

// we read too much, try to fix this
pr, ok := it.f.Reader.(*peekReader)
if !ok {
it.err = errors.New("cannot undo NextPart")
return false
}

it.err = pr.put(part)
it.err = it.f.reader.put(part)
return false
}

func (it *multipartIterator) Err() error {
return it.err
}

func (f *MultipartFile) Entries() DirIterator {
func (f *multipartFile) Entries() DirIterator {
return &multipartIterator{f: f}
}

func (f *MultipartFile) fileName() string {
if f == nil || f.Part == nil {
func (f *multipartFile) fileName() string {
if f == nil || f.part == nil {
return ""
}

filename, err := url.QueryUnescape(f.Part.FileName())
filename, err := url.QueryUnescape(f.part.FileName())
if err != nil {
// if there is a unescape error, just treat the name as unescaped
return f.Part.FileName()
return f.part.FileName()
}
return filename
}

func (f *MultipartFile) Close() error {
if f.Part != nil {
return f.Part.Close()
func (f *multipartFile) Close() error {
if f.part != nil {
return f.part.Close()
}
return nil
}

func (f *MultipartFile) Size() (int64, error) {
func (f *multipartFile) Size() (int64, error) {
return 0, ErrNotSupported
}

Expand Down Expand Up @@ -230,4 +223,4 @@ func (pr *peekReader) put(p *multipart.Part) error {
return nil
}

var _ Directory = &MultipartFile{}
var _ Directory = &multipartFile{}

0 comments on commit a6bcde3

Please sign in to comment.