summaryrefslogtreecommitdiff
path: root/addons/web_unsplash/controllers
diff options
context:
space:
mode:
authorSébastien Theys <seb@odoo.com>2019-04-26 10:55:13 +0000
committerSébastien Theys <seb@odoo.com>2019-04-29 13:45:03 +0000
commit433d0070755b9917e46827c2ad1c258cfe31f804 (patch)
treed3ed4fc444c3f6548ac9babf011e6174a9a4c76d /addons/web_unsplash/controllers
parent8269f855136050c9139352ed71bc75f81899e255 (diff)
[IMP] tools,web_editor,web_unsplash: improve image optimize for web
This commit introduces the possibility to resize and to select the quality when optimizing an image for web. This will be used by the media dialog in a future commit. The arbitrary resolution limit of 42e6 has been increased to 45e6 to fit some of the biggest images we might get from Unsplash, and this value has been moved into a variable so it can be customized. The mimetype computation has been moved after the image processing because in a next commit the operation could return the image in a different format than what was originally given. Eg. BMP -> PNG, or unsupported format -> JPEG. task-1958000 PR: #31811
Diffstat (limited to 'addons/web_unsplash/controllers')
-rw-r--r--addons/web_unsplash/controllers/main.py20
1 files changed, 8 insertions, 12 deletions
diff --git a/addons/web_unsplash/controllers/main.py b/addons/web_unsplash/controllers/main.py
index 219342d1c39..6cd0ef9e82b 100644
--- a/addons/web_unsplash/controllers/main.py
+++ b/addons/web_unsplash/controllers/main.py
@@ -1,14 +1,14 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import base64
-import io
import logging
+import mimetypes
import requests
import werkzeug.utils
-from PIL import Image
from odoo import http, tools, _
from odoo.http import request
+from odoo.tools.mimetypes import guess_mimetype
from werkzeug.urls import url_encode
logger = logging.getLogger(__name__)
@@ -89,8 +89,7 @@ class Web_Unsplash(http.Controller):
continue
# get mime-type of image url because unsplash url dosn't contains mime-types in url
- mimetype = req.headers.get('Content-Type')
- datas = req.content
+ image_base64 = base64.b64encode(req.content)
except requests.exceptions.ConnectionError as e:
logger.exception("Connection Error: " + str(e))
continue
@@ -98,13 +97,10 @@ class Web_Unsplash(http.Controller):
logger.exception("Timeout: " + str(e))
continue
- # optimized image before save
- if mimetype in ('image/jpeg', 'image/png'):
- image = Image.open(io.BytesIO(datas))
- if image.format in ('PNG', 'JPEG'):
- datas = tools.image_save_for_web(image)
- # append image extension in name
- query += '.' + str.lower(image.format)
+ image_base64 = tools.image_optimize_for_web(image_base64)
+ mimetype = guess_mimetype(base64.b64decode(image_base64))
+ # append image extension in name
+ query += mimetypes.guess_extension(mimetype) or ''
# /unsplash/5gR788gfd/lion
url_frags = ['unsplash', key, query]
@@ -114,7 +110,7 @@ class Web_Unsplash(http.Controller):
'url': '/' + '/'.join(url_frags),
'datas_fname': '_'.join(url_frags),
'mimetype': mimetype,
- 'datas': base64.b64encode(datas),
+ 'datas': image_base64,
'public': res_model == 'ir.ui.view',
'res_id': res_id,
'res_model': res_model,