Skip to content

Add content_type property to File class#225

Open
niveshdandyan wants to merge 1 commit intoKludex:masterfrom
niveshdandyan:fix/add-content-type-to-file-class
Open

Add content_type property to File class#225
niveshdandyan wants to merge 1 commit intoKludex:masterfrom
niveshdandyan:fix/add-content-type-to-file-class

Conversation

@niveshdandyan
Copy link

Summary

  • Adds a content_type property to the File class to expose the MIME type of uploaded files
  • The Content-Type is extracted from multipart headers when parsing file uploads
  • Returns None if no Content-Type header was provided in the multipart data
  • Updates FileProtocol to include the new content_type parameter

Motivation

This addresses issue #58 - the File class lacked MIME type information that was previously available in cgi's FieldStorage.type attribute. This made it difficult to properly handle uploaded files while preserving their file type.

Changes

  • Added content_type parameter to File.__init__()
  • Added content_type property to expose the MIME type
  • Updated FormParser.on_headers_finished() to extract and pass Content-Type header
  • Updated __repr__ to include content_type
  • Added comprehensive tests for the new functionality

Test plan

  • Added unit tests for File.content_type property
  • Added unit tests verifying Content-Type is properly extracted from multipart data
  • Added tests for when Content-Type is not provided (returns None)
  • All existing tests pass
  • ruff linting passes
  • mypy type checking passes

Fixes #58


Note

This PR was authored with AI assistance (Claude claude-opus-4-5).
All code changes have been reviewed and tested.

🤖 Generated with Claude Code

This adds the ability to access the MIME type (Content-Type) of uploaded
files. The Content-Type is extracted from the multipart headers when parsing
file uploads and made available via the new `content_type` property on the
File class.

This addresses a regression compared to cgi's FieldStorage class which had
a `type` attribute for the declared MIME type of uploaded files.

Fixes Kludex#58

Co-Authored-By: Claude (claude-opus-4-5) <noreply@anthropic.com>
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.

multipart.File has no MIME type information

1 participant