Skip to content

Commit

Permalink
🚑 when the "image_resize_image" function was called, they received th…
Browse files Browse the repository at this point in the history
…e error "binascii.Error: decoding with base64 codec failed (Error: Incorrect padding)", since the value of the binary field is the URL, not the base_64 string
  • Loading branch information
Rafis Bikbov committed Nov 15, 2018
1 parent 5eec8b9 commit 8472706
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ir_attachment_url/__manifest__.py
Expand Up @@ -4,7 +4,7 @@
"summary": """Use attachment URL and upload data to external storage""",
"category": "Tools",
"images": [],
"version": "10.0.1.1.5",
"version": "10.0.1.1.6",
"application": False,

"author": "IT-Projects LLC, Ildar Nasyrov",
Expand Down
5 changes: 5 additions & 0 deletions ir_attachment_url/doc/changelog.rst
@@ -1,3 +1,8 @@
`1.1.6`
-------

- **Fix** When the "image_resize_image" function was called, they received the error "binascii.Error: decoding with base64 codec failed (Error: Incorrect padding)", since the value of the binary field is the URL, not the base_64 string.

`1.1.5`
-------

Expand Down
51 changes: 51 additions & 0 deletions ir_attachment_url/models/image.py
Expand Up @@ -29,4 +29,55 @@ def is_url(value):
return re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', value)


super_image_resize_image = tools.image_resize_image


def updated_image_resize_image(base64_source, size=(1024, 1024), encoding='base64', filetype=None, avoid_if_small=False):
if is_url(base64_source):
return base64_source
return super_image_resize_image(base64_source, size=size, encoding=encoding, filetype=filetype, avoid_if_small=avoid_if_small)


def updated_image_resize_image_big(base64_source, size=(1024, 1024), encoding='base64', filetype=None, avoid_if_small=True):
""" copy-pasted from odoo/tools/image.py::image_resize_image_big
because we rewrite image_resize_image function.
"""
return updated_image_resize_image(base64_source, size, encoding, filetype, avoid_if_small)


def updated_image_resize_image_medium(base64_source, size=(128, 128), encoding='base64', filetype=None, avoid_if_small=False):
""" copy-pasted from odoo/tools/image.py::image_resize_image_medium
because we rewrite image_resize_image function.
"""
return updated_image_resize_image(base64_source, size, encoding, filetype, avoid_if_small)


def updated_image_resize_image_small(base64_source, size=(64, 64), encoding='base64', filetype=None, avoid_if_small=False):
""" copy-pasted from odoo/tools/image.py::image_resize_image_small
because we rewrite image_resize_image function.
"""
return updated_image_resize_image(base64_source, size, encoding, filetype, avoid_if_small)


def updated_image_get_resized_images(base64_source, return_big=False, return_medium=True, return_small=True,
big_name='image', medium_name='image_medium', small_name='image_small',
avoid_resize_big=True, avoid_resize_medium=False, avoid_resize_small=False):
""" copy-pasted from odoo/tools/image.py::image_get_resized_images
because we rewrite image_resize_image function.
"""
return_dict = dict()
if return_big:
return_dict[big_name] = updated_image_resize_image_big(base64_source, avoid_if_small=avoid_resize_big)
if return_medium:
return_dict[medium_name] = updated_image_resize_image_medium(base64_source, avoid_if_small=avoid_resize_medium)
if return_small:
return_dict[small_name] = updated_image_resize_image_small(base64_source, avoid_if_small=avoid_resize_small)
return return_dict


tools.image_resize_images = updated_image_resize_images
tools.image_resize_image = updated_image_resize_image
tools.image_resize_image_big = updated_image_resize_image_big
tools.image_resize_image_medium = updated_image_resize_image_medium
tools.image_resize_image_small = updated_image_resize_image_small
tools.image_get_resized_images = updated_image_get_resized_images
6 changes: 3 additions & 3 deletions ir_attachment_url/models/ir_http.py
Expand Up @@ -81,7 +81,7 @@ def binary_content(cls, xmlid=None, model='ir.attachment', id=None, field='datas

# check read access
try:
last_update = obj['__last_update']
obj['__last_update']
except AccessError:
return (403, [], None)

Expand All @@ -101,7 +101,7 @@ def binary_content(cls, xmlid=None, model='ir.attachment', id=None, field='datas
if module_resource_path.startswith(module_path):
with open(module_resource_path, 'rb') as f:
content = base64.b64encode(f.read())
last_update = str(os.path.getmtime(module_resource_path))
# 'last_update' variable removed for lint error fix

if not module_resource_path:
module_resource_path = obj.url
Expand Down Expand Up @@ -144,7 +144,7 @@ def binary_content(cls, xmlid=None, model='ir.attachment', id=None, field='datas

# cache
etag = hasattr(request, 'httprequest') and request.httprequest.headers.get('If-None-Match')
retag = '"%s"' % hashlib.md5(last_update).hexdigest()
retag = '"%s"' % hashlib.md5(content).hexdigest()
status = status or (304 if etag == retag else 200)
headers.append(('ETag', retag))
headers.append(('Cache-Control', 'max-age=%s' % (STATIC_CACHE if unique else 0)))
Expand Down

0 comments on commit 8472706

Please sign in to comment.