-
Notifications
You must be signed in to change notification settings - Fork 361
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
API upload object without using tmp file #4848
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"fmt" | ||
"io" | ||
"mime" | ||
"mime/multipart" | ||
"net/http" | ||
"net/mail" | ||
"net/url" | ||
|
@@ -2134,21 +2135,50 @@ func (c *Controller) UploadObject(w http.ResponseWriter, r *http.Request, reposi | |
allowOverwrite = false | ||
} | ||
|
||
// write the content | ||
file, handler, err := r.FormFile("content") | ||
if errors.Is(err, http.ErrMissingFile) { | ||
writeError(w, r, http.StatusInternalServerError, fmt.Errorf("multipart uploads missing key 'content': %w", err)) | ||
return | ||
} | ||
// read request body parse multipart for "content" and upload the data | ||
mt, p, err := mime.ParseMediaType(r.Header.Get("Content-Type")) | ||
if err != nil { | ||
writeError(w, r, http.StatusInternalServerError, err) | ||
return | ||
} | ||
defer func() { _ = file.Close() }() | ||
contentType := handler.Header.Get("Content-Type") | ||
address := c.PathProvider.NewPath() | ||
blob, err := upload.WriteBlob(ctx, c.BlockAdapter, repo.StorageNamespace, address, file, handler.Size, block.PutOpts{StorageClass: params.StorageClass}) | ||
if err != nil { | ||
if !strings.HasPrefix(mt, "multipart/") { | ||
writeError(w, r, http.StatusInternalServerError, http.ErrNotMultipart) | ||
return | ||
} | ||
ortz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
boundary, ok := p["boundary"] | ||
if !ok { | ||
writeError(w, r, http.StatusInternalServerError, http.ErrMissingBoundary) | ||
return | ||
} | ||
reader := multipart.NewReader(r.Body, boundary) | ||
var ( | ||
contentUploaded bool | ||
contentType string | ||
blob *upload.Blob | ||
) | ||
for { | ||
part, err := reader.NextPart() | ||
if err == io.EOF { | ||
break | ||
} | ||
ortz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
contentType = part.Header.Get("Content-Type") | ||
partName := part.FormName() | ||
// part is an io.Reader, deal with it | ||
if !contentUploaded && partName == "content" { | ||
// upload the first "content" | ||
address := c.PathProvider.NewPath() | ||
blob, err = upload.WriteBlob(ctx, c.BlockAdapter, repo.StorageNamespace, address, part, -1, block.PutOpts{StorageClass: params.StorageClass}) | ||
if err != nil { | ||
_ = part.Close() | ||
writeError(w, r, http.StatusInternalServerError, err) | ||
return | ||
} | ||
contentUploaded = true | ||
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. Why not 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. Will fix - wanted to complete the body processing - but I see that Close will do it for me! |
||
} | ||
_ = part.Close() | ||
} | ||
if !contentUploaded { | ||
err := fmt.Errorf("multipart upload missing key 'content': %w", http.ErrMissingFile) | ||
writeError(w, r, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
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.
Not sure this works for any multipart Content-Type, perhaps we should limit ourselves to a list of known types? (Package http probably limits itself in some way, so if we do the same would we be safe?)
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.
Good point - I'll limit it to "multipart/form-data". The Go's request implementation support
form-data
and optionalmixed
.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.
Currently we added the information in the swagger description:
Only a single file per upload which must be named "content".