summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRomainLibert <rli@odoo.com>2019-09-18 09:17:17 +0000
committerRomainLibert <rli@odoo.com>2019-09-18 10:14:12 +0000
commit85eeaccf065b151dbc9f0e1a24f1b4f674dec6cc (patch)
tree797bc7049d38e5e0c5af0bbd422c7f7a78d0d300
parentf60acb4f49132a2103db8e868e830b4e5e11b8fb (diff)
[FIX] tools: use user friendly message in case of error
closes odoo/odoo#37058 Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
-rw-r--r--addons/web_editor/controllers/main.py4
-rw-r--r--odoo/addons/base/tests/test_image.py9
-rw-r--r--odoo/tools/image.py13
3 files changed, 15 insertions, 11 deletions
diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py
index 5a7ca5483f7..603509480f0 100644
--- a/addons/web_editor/controllers/main.py
+++ b/addons/web_editor/controllers/main.py
@@ -158,7 +158,7 @@ class Web_Editor(http.Controller):
def add_data(self, name, data, quality=0, width=0, height=0, res_id=False, res_model='ir.ui.view', filters=False, **kwargs):
try:
data = tools.image_process(data, size=(width, height), quality=quality, verify_resolution=True)
- except OSError:
+ except UserError:
pass # not an image
attachment = self._attachment_create(name=name, data=data, res_id=res_id, res_model=res_model, filters=filters)
return attachment._get_media_info()
@@ -179,7 +179,7 @@ class Web_Editor(http.Controller):
data['name'] = name
try:
data['datas'] = tools.image_process(attachment.datas, size=(width, height), quality=quality)
- except OSError:
+ except UserError:
pass # not an image
attachment.write(data)
return attachment._get_media_info()
diff --git a/odoo/addons/base/tests/test_image.py b/odoo/addons/base/tests/test_image.py
index db565bc79d8..838c37a84a1 100644
--- a/odoo/addons/base/tests/test_image.py
+++ b/odoo/addons/base/tests/test_image.py
@@ -6,6 +6,7 @@ import binascii
from PIL import Image, ImageDraw, PngImagePlugin
from odoo import tools
+from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase
@@ -54,10 +55,10 @@ class TestImage(TransactionCase):
self.assertEqual(type(image), PngImagePlugin.PngImageFile, "base64 as string, correct format")
self.assertEqual(image.size, (1, 1), "base64 as string, correct size")
- with self.assertRaises(binascii.Error, msg="wrong base64: binascii.Error: Incorrect padding"):
+ with self.assertRaises(UserError, msg="This file could not be decoded as an image file. Please try with a different file."):
image = tools.base64_to_image(b'oazdazpodazdpok')
- with self.assertRaises(OSError, msg="wrong base64: OSError: cannot identify image file"):
+ with self.assertRaises(UserError, msg="This file could not be decoded as an image file. Please try with a different file."):
image = tools.base64_to_image(b'oazdazpodazdpokd')
def test_01_image_to_base64(self):
@@ -74,10 +75,10 @@ class TestImage(TransactionCase):
self.assertEqual(tools.image_process(self.base64_svg), self.base64_svg, "return base64_source if format is SVG")
# in the following tests, pass `quality` to force the processing
- with self.assertRaises(binascii.Error, msg="wrong base64: binascii.Error: Incorrect padding"):
+ with self.assertRaises(UserError, msg="This file could not be decoded as an image file. Please try with a different file."):
tools.image_process(wrong_base64, quality=95)
- with self.assertRaises(OSError, msg="wrong base64: OSError: cannot identify image file"):
+ with self.assertRaises(UserError, msg="This file could not be decoded as an image file. Please try with a different file."):
tools.image_process(b'oazdazpodazdpokd', quality=95)
image = tools.base64_to_image(tools.image_process(self.base64_1920x1080_jpeg, quality=95))
diff --git a/odoo/tools/image.py b/odoo/tools/image.py
index 999b5c1e5e7..f5b4b953da9 100644
--- a/odoo/tools/image.py
+++ b/odoo/tools/image.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import base64
+import binascii
import io
from PIL import Image
@@ -9,6 +10,7 @@ from PIL import IcoImagePlugin
from random import randrange
+from odoo.exceptions import UserError
from odoo.tools.translate import _
@@ -49,8 +51,7 @@ class ImageProcess():
:rtype: ImageProcess
:raise: ValueError if `verify_resolution` is True and the image is too large
- :raise: binascii.Error: if the base64 is incorrect
- :raise: OSError if the image can't be identified by PIL
+ :raise: UserError if the base64 is incorrect or the image can't be identified by PIL
"""
self.base64_source = base64_source or False
self.operationsCount = 0
@@ -339,10 +340,12 @@ def base64_to_image(base64_source):
:return: the PIL image
:rtype: PIL.Image
- :raise: binascii.Error: if the base64 is incorrect
- :raise: OSError if the image can't be identified by PIL
+ :raise: UserError if the base64 is incorrect or the image can't be identified by PIL
"""
- return Image.open(io.BytesIO(base64.b64decode(base64_source)))
+ try:
+ return Image.open(io.BytesIO(base64.b64decode(base64_source)))
+ except (OSError, binascii.Error):
+ raise UserError(_("This file could not be decoded as an image file. Please try with a different file."))
def image_to_base64(image, format, **params):