-
Notifications
You must be signed in to change notification settings - Fork 381
API upload object without using tmp file #4848
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
Changes from all 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,54 @@ 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 mt != "multipart/form-data" { | ||
writeError(w, r, http.StatusInternalServerError, http.ErrNotMultipart) | ||
return | ||
} | ||
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 !contentUploaded { | ||
part, err := reader.NextPart() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
writeError(w, r, http.StatusInternalServerError, err) | ||
return | ||
} | ||
contentType = part.Header.Get("Content-Type") | ||
partName := part.FormName() | ||
if partName == "content" { | ||
// upload the first "content" and exit the loop | ||
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 | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.