[FIX] tools: use user friendly message in case of error
closes odoo/odoo#37058 Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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))
|
||||
|
||||
+8
-5
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user