Skip to content

Commit

Permalink
[FIX] compute the mimetype from new datas or raw to prevent errors wh…
Browse files Browse the repository at this point in the history
…en the new mimetype doesn't match the old one
  • Loading branch information
benwillig authored and nguyenminhchien committed Mar 11, 2024
1 parent df3f401 commit 2dd9d0f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
40 changes: 22 additions & 18 deletions fs_attachment/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,29 +286,33 @@ def create(self, vals_list):

def write(self, vals):
if not self:
return self
return super().write(vals)
if ("datas" in vals or "raw" in vals) and not (
"name" in vals or "mimetype" in vals
):
# When we write on an attachment, if the mimetype is not provided, it
# will be computed from the name. The problem is that if you assign a
# value to the field ``datas`` or ``raw``, the name is not provided
# nor the mimetype, so the mimetype will be set to ``application/octet-
# stream``.
# We want to avoid this, so we take the mimetype of the first attachment
# and we set it on all the attachments if they all have the same mimetype.
# If they don't have the same mimetype, we raise an error.
# OPW-3277070
mimetypes = self.mapped("mimetype")
if len(set(mimetypes)) == 1:
vals["mimetype"] = mimetypes[0]
mimetype = self._compute_mimetype(vals)
if mimetype and mimetype != "application/octet-stream":
vals["mimetype"] = mimetype
else:
raise UserError(
_(
"You can't write on multiple attachments with different "
"mimetypes at the same time."
# When we write on an attachment, if the mimetype is not provided, it
# will be computed from the name. The problem is that if you assign a
# value to the field ``datas`` or ``raw``, the name is not provided
# nor the mimetype, so the mimetype will be set to ``application/octet-
# stream``.
# We want to avoid this, so we take the mimetype of the first attachment
# and we set it on all the attachments if they all have the same mimetype.
# If they don't have the same mimetype, we raise an error.
# OPW-3277070
mimetypes = self.mapped("mimetype")
if len(set(mimetypes)) == 1:
vals["mimetype"] = mimetypes[0]
else:
raise UserError(
_(
"You can't write on multiple attachments with different "
"mimetypes at the same time."
)
)
)
for rec in self:
# As when creating a new attachment, we must pass the res_field
# and res_model into the context hence sadly we must perform 1 call
Expand Down
34 changes: 34 additions & 0 deletions fs_attachment/tests/test_fs_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,37 @@ def test_create_attachments_basic_user(self):
"res_field": partner_image_field.name,
}
)

def test_update_png_to_svg(self):
b64_data_png = (
b"iVBORw0KGgoAAAANSUhEUgAAADMAAAAhCAIAAAD73QTtAAAAA3NCSVQICAjb4U/gAA"
b"AAP0lEQVRYhe3OMQGAMBAAsVL/nh8FDDfxQ6Igz8ycle7fgU9mnVln1pl1Zp1ZZ9aZd"
b"WadWWfWmXVmnVln1u2dvfL/Az+TRcv4AAAAAElFTkSuQmCC"
)

attachment = self.ir_attachment_model.create(
{
"name": "test.png",
"datas": b64_data_png,
}
)
self.assertEqual(attachment.mimetype, "image/png")

b64_data_svg = (
b"PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/Pgo8IURPQ1RZUEU"
b"gc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDIwMDEwOTA0Ly9FTiIKICJodH"
b"RwOi8vd3d3LnczLm9yZy9UUi8yMDAxL1JFQy1TVkctMjAwMTA5MDQvRFREL3N2Zz"
b"EwLmR0ZCI+CjxzdmcgdmVyc2lvbj0iMS4wIiB4bWxucz0iaHR0cDovL3d3dy53My5"
b"vcmcvMjAwMC9zdmciCiB3aWR0aD0iNTEuMDAwMDAwcHQiIGhlaWdodD0iMzMuMDAw"
b"MDAwcHQiIHZpZXdCb3g9IjAgMCA1MS4wMDAwMDAgMzMuMDAwMDAwIgogcHJlc2Vydm"
b"VBc3BlY3RSYXRpbz0ieE1pZFlNaWQgbWVldCI+Cgo8ZyB0cmFuc2Zvcm09InRyYW5z"
b"bGF0ZSgwLjAwMDAwMCwzMy4wMDAwMDApIHNjYWxlKDAuMTAwMDAwLC0wLjEwMDAwMCk"
b"iCmZpbGw9IiMwMDAwMDAiIHN0cm9rZT0ibm9uZSI+CjwvZz4KPC9zdmc+Cg=="
)
attachment.write(
{
"datas": b64_data_svg,
}
)

self.assertEqual(attachment.mimetype, "image/svg+xml")

0 comments on commit 2dd9d0f

Please sign in to comment.