-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
flatten multipart transfers #2046
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b4dd57
Flatten multipart file transfers
whyrusleeping 0a09c4f
fix tests
whyrusleeping 38f86f0
cleanup multipart
whyrusleeping a9f0c12
PutNode creates intermediary nodes
whyrusleeping 90d7c6e
add more tests for multipart parsing
whyrusleeping 8c4900b
rename hidden field
whyrusleeping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
package files | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"mime" | ||
"mime/multipart" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
const ( | ||
multipartFormdataType = "multipart/form-data" | ||
multipartMixedType = "multipart/mixed" | ||
|
||
applicationSymlink = "application/symlink" | ||
applicationDirectory = "application/x-directory" | ||
applicationSymlink = "application/symlink" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this tested and documented anywhere (symlink)? |
||
|
||
contentTypeHeader = "Content-Type" | ||
) | ||
|
@@ -45,40 +46,33 @@ func NewFileFromPart(part *multipart.Part) (File, error) { | |
}, nil | ||
} | ||
|
||
var params map[string]string | ||
var err error | ||
f.Mediatype, params, err = mime.ParseMediaType(contentType) | ||
f.Mediatype, _, err = mime.ParseMediaType(contentType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if f.IsDirectory() { | ||
boundary, found := params["boundary"] | ||
if !found { | ||
return nil, http.ErrMissingBoundary | ||
} | ||
|
||
f.Reader = multipart.NewReader(part, boundary) | ||
} | ||
|
||
return f, nil | ||
} | ||
|
||
func (f *MultipartFile) IsDirectory() bool { | ||
return f.Mediatype == multipartFormdataType || f.Mediatype == multipartMixedType | ||
return f.Mediatype == multipartFormdataType || f.Mediatype == applicationDirectory | ||
} | ||
|
||
func (f *MultipartFile) NextFile() (File, error) { | ||
if !f.IsDirectory() { | ||
return nil, ErrNotDirectory | ||
} | ||
if f.Reader != nil { | ||
part, err := f.Reader.NextPart() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
part, err := f.Reader.NextPart() | ||
if err != nil { | ||
return nil, err | ||
return NewFileFromPart(part) | ||
} | ||
|
||
return NewFileFromPart(part) | ||
return nil, io.EOF | ||
} | ||
|
||
func (f *MultipartFile) FileName() string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
|
@@ -18,9 +19,10 @@ type serialFile struct { | |
files []os.FileInfo | ||
stat os.FileInfo | ||
current *File | ||
hidden bool | ||
} | ||
|
||
func NewSerialFile(name, path string, stat os.FileInfo) (File, error) { | ||
func NewSerialFile(name, path string, hidden bool, stat os.FileInfo) (File, error) { | ||
switch mode := stat.Mode(); { | ||
case mode.IsRegular(): | ||
file, err := os.Open(path) | ||
|
@@ -35,7 +37,7 @@ func NewSerialFile(name, path string, stat os.FileInfo) (File, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
return &serialFile{name, path, contents, stat, nil}, nil | ||
return &serialFile{name, path, contents, stat, nil, hidden}, nil | ||
case mode&os.ModeSymlink != 0: | ||
target, err := os.Readlink(path) | ||
if err != nil { | ||
|
@@ -68,14 +70,23 @@ func (f *serialFile) NextFile() (File, error) { | |
stat := f.files[0] | ||
f.files = f.files[1:] | ||
|
||
for !f.hidden && strings.HasPrefix(stat.Name(), ".") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably best to rename |
||
if len(f.files) == 0 { | ||
return nil, io.EOF | ||
} | ||
|
||
stat = f.files[0] | ||
f.files = f.files[1:] | ||
} | ||
|
||
// open the next file | ||
fileName := filepath.ToSlash(filepath.Join(f.name, stat.Name())) | ||
filePath := filepath.ToSlash(filepath.Join(f.path, stat.Name())) | ||
|
||
// recursively call the constructor on the next file | ||
// if it's a regular file, we will open it as a ReaderFile | ||
// if it's a directory, files in it will be opened serially | ||
sf, err := NewSerialFile(fileName, filePath, stat) | ||
sf, err := NewSerialFile(fileName, filePath, f.hidden, stat) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -94,7 +105,7 @@ func (f *serialFile) FullPath() string { | |
} | ||
|
||
func (f *serialFile) Read(p []byte) (int, error) { | ||
return 0, ErrNotReader | ||
return 0, io.EOF | ||
} | ||
|
||
func (f *serialFile) Close() error { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should test file inside the directory as well