feat(upload): auto-detect file content-type if not sent#5361
feat(upload): auto-detect file content-type if not sent#5361akrem-chabchoub wants to merge 11 commits intomasterfrom
Conversation
|
what is the status of this PR? |
|
Still not fully tested yet. |
| r.Header.Set(ContentTypeHeader, contentTypeHdr) | ||
| mt, _, errParseCT := mime.ParseMediaType(contentTypeHdr) | ||
| isMultipart := errParseCT == nil && mt == multiPartFormData | ||
| if headers.IsDir || isMultipart { |
There was a problem hiding this comment.
if (headers.IsDir || isMultipart) && contentTypeHdr == "" {...}
There was a problem hiding this comment.
Maybe this is more readable, wdyt ?
isDirUpload := headers.IsDir || isMultipart
if !isDirUpload {
s.fileUploadHandler(ctx, logger, span, ow, r, putter, headers.Encrypt, tag, headers.RLevel, headers.Act, headers.HistoryAddress)
return
}
if contentTypeHdr == "" {
logger.Error(nil, "content-type required for directory upload")
jsonhttp.BadRequest(w, errInvalidContentType)
return
}
s.dirUploadHandler(ctx, logger, span, ow, r, putter, headers.Encrypt, tag, headers.RLevel, headers.Act, headers.HistoryAddress)There was a problem hiding this comment.
both are fine 👍 the point was to just reduce nesting
…and directory uploads
| if headers.IsDir || headers.ContentType == multiPartFormData { | ||
| s.dirUploadHandler(ctx, logger, span, ow, r, putter, r.Header.Get(ContentTypeHeader), headers.Encrypt, tag, headers.RLevel, headers.Act, headers.HistoryAddress) | ||
| contentTypeHdr := strings.TrimSpace(headers.ContentType) | ||
| r.Header.Set(ContentTypeHeader, contentTypeHdr) |
There was a problem hiding this comment.
Before my changes the dirUploadHandler was accepting contentTypeString (although it is present request).
So I changed that here and did trim and set the new value in main handler.
There was a problem hiding this comment.
I think that I agree with @martinconic that this is not really clear why this is needed. If you're already reading, cleaning, parsing and checking the content type header - it does not make any more sense to update the value in the Header type.
| return | ||
| } | ||
|
|
||
| if contentTypeHdr == "" { |
There was a problem hiding this comment.
Maybe a test for empty content type if there is none? Also if no content type and no body?
| @@ -58,7 +57,7 @@ func (s *Service) dirUploadHandler( | |||
| } | |||
|
|
|||
| // The error is ignored because the header was already validated by the caller. | |||
There was a problem hiding this comment.
The comment is not relevant anymore, right? the header is not validated by the caller. Maybe something like this:
// Parse error is ignored; unsupported media types are caught by the default case below.
, or do we want to validate here?
sbackend123
left a comment
There was a problem hiding this comment.
How would it work on edge cases like:
- empty body
Checklist
Description
For single-file POST
/bzz, Content-Type is optional: if the client sends it, we keep it exactly, if not we infer it from the first bytes of the body.Open API Spec Version Changes (if applicable)
Motivation and Context (Optional)
Related Issue (Optional)
#5346
Screenshots (if appropriate):