Skip to content

feat(upload): auto-detect file content-type if not sent#5361

Open
akrem-chabchoub wants to merge 11 commits intomasterfrom
fix/content-type
Open

feat(upload): auto-detect file content-type if not sent#5361
akrem-chabchoub wants to merge 11 commits intomasterfrom
fix/content-type

Conversation

@akrem-chabchoub
Copy link
Copy Markdown
Contributor

@akrem-chabchoub akrem-chabchoub commented Feb 12, 2026

Checklist

  • I have read the coding guide.
  • My change requires a documentation update, and I have done it.
  • I have added tests to cover my changes.
  • I have filled out the description and linked the related issues.

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):

@akrem-chabchoub akrem-chabchoub self-assigned this Mar 5, 2026
@akrem-chabchoub akrem-chabchoub added this to the 2026 milestone Mar 5, 2026
@acud
Copy link
Copy Markdown
Contributor

acud commented Mar 30, 2026

what is the status of this PR?

@akrem-chabchoub
Copy link
Copy Markdown
Contributor Author

Still not fully tested yet.
Currently on hold due to other priorities.
Work is expected to resume asap.

@akrem-chabchoub akrem-chabchoub added the on hold Temporarily halted by other development label Mar 30, 2026
@akrem-chabchoub akrem-chabchoub removed the on hold Temporarily halted by other development label Apr 9, 2026
@akrem-chabchoub akrem-chabchoub marked this pull request as ready for review April 9, 2026 11:21
@akrem-chabchoub akrem-chabchoub changed the title fix(api): enhance content type handling in bzzUploadHandler for direc… feat(upload): auto-detect file content-type if not sent Apr 9, 2026
Comment thread pkg/api/bzz.go Outdated
Comment thread pkg/api/bzz.go Outdated
r.Header.Set(ContentTypeHeader, contentTypeHdr)
mt, _, errParseCT := mime.ParseMediaType(contentTypeHdr)
isMultipart := errParseCT == nil && mt == multiPartFormData
if headers.IsDir || isMultipart {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (headers.IsDir || isMultipart) && contentTypeHdr == "" {...}

Copy link
Copy Markdown
Contributor Author

@akrem-chabchoub akrem-chabchoub Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both are fine 👍 the point was to just reduce nesting

@akrem-chabchoub akrem-chabchoub requested a review from acud April 14, 2026 11:41
Comment thread pkg/api/bzz.go
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/api/bzz.go
return
}

if contentTypeHdr == "" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a test for empty content type if there is none? Also if no content type and no body?

Comment thread pkg/api/dirs.go
@@ -58,7 +57,7 @@ func (s *Service) dirUploadHandler(
}

// The error is ignored because the header was already validated by the caller.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

@sbackend123 sbackend123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would it work on edge cases like:

  • empty body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File upload: auto-detect content type instead of trusting user header

4 participants