Skip to content
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

Resolved the issue related to file mimetype #242

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions g2p_documents/models/document_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def filter_for_tags_any(self, tags):

def _compute_file_type(self):
for file in self:
if file.extension and isinstance(file.mimetype, str):
if isinstance(file.mimetype, str):
file.file_type = file.mimetype.split("/")[1].upper()
else:
file.file_type = False
Expand Down Expand Up @@ -77,11 +77,13 @@ def _compute_extract_filename(self):

def _get_mime_type(self, binary_data):
try:
if binary_data[:4] == b"%PDF":
return "application/pdf"
image = Image.open(io.BytesIO(binary_data))
mime_type = Image.MIME[image.format]
return mime_type
except OSError as e:
_logger.info(f"Image processing error: {e}")
_logger.info(f"Unexpected error in MIME detection: {e}")
return None

def _compute_data(self):
Expand Down
Loading