Skip to content

Commit

Permalink
[FIX] base: fix file extension when using default name
Browse files Browse the repository at this point in the history
The file extension detection previously used to determine whether a
filename required an extension was not very smart and in fact only
checked for a dot in the filename.
`mimetypes.guess_type` is now used on the filename to better determine
whether the filename still needs a file extension or not.

This commit is a follow-up to: odoo#90614
Which aimed to fix the same issue.

TaskId-2826061

closes odoo#91205

X-original-commit: 1a75900
Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
Signed-off-by: William Braeckman (wbr) <wbr@odoo.com>
  • Loading branch information
William Braeckman committed May 12, 2022
1 parent ac0c26a commit 64aabd8
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions odoo/addons/base/models/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,10 @@ def _binary_record_content(
content = record[field] or ''

# filename
default_filename = False
if not filename:
if filename_field in record:
default_filename = (filename_field == 'name' and model != 'ir.attachment')
filename = record[filename_field]
if not filename:
default_filename = True
filename = "%s-%s-%s" % (record._name, record.id, field)

if not mimetype:
Expand All @@ -414,8 +411,8 @@ def _binary_record_content(
mimetype = guess_mimetype(decoded_content, default=default_mimetype)

# extension
_, existing_extension = os.path.splitext(filename)
if not existing_extension or default_filename:
has_extension = bool(mimetypes.guess_type(filename)[0])
if not has_extension:
extension = mimetypes.guess_extension(mimetype)
if extension:
filename = "%s%s" % (filename, extension)
Expand Down

0 comments on commit 64aabd8

Please sign in to comment.