diff options
| author | qsm-odoo <qsm@odoo.com> | 2015-12-01 10:30:35 +0100 |
|---|---|---|
| committer | Christophe Simonis <chs@odoo.com> | 2015-12-03 13:26:36 +0100 |
| commit | 8c0b7558412fd30d96f6f5863a9e8d3cfb66b80c (patch) | |
| tree | 2a7b358ca7b217fc3a54a7dee2922ee6778ad25a /addons/web/controllers/main.py | |
| parent | d1af67bf28d24fe8471c2f43132488535d4f6a63 (diff) | |
[IMP] web: return file content for url attachments
URL attachments are now returned as data and not redirection.
If the url does not match with a module file, the 301 status
(redirection) is still returned.
This allows to use the /web/image/<xmlid>/<dimension> route
and get an image attachment with correct dimensions.
Diffstat (limited to 'addons/web/controllers/main.py')
| -rw-r--r-- | addons/web/controllers/main.py | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index b2cb698a256..8de78aebc2f 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -33,7 +33,7 @@ except ImportError: import openerp import openerp.modules.registry from openerp.addons.base.ir.ir_qweb import AssetsBundle, QWebTemplateNotFound -from openerp.modules import get_module_resource +from openerp.modules import get_module_path, get_resource_path from openerp.tools import topological_sort from openerp.tools.translate import _ from openerp.tools import ustr @@ -469,12 +469,40 @@ def binary_content(xmlid=None, model='ir.attachment', id=None, field='datas', un last_update = obj['__last_update'] except AccessError: return (403, [], None) - status = 200 + + status, headers, content = None, [], None + + # attachment by url check + module_resource_path = None + if model == 'ir.attachment' and obj.type == 'url' and obj.url: + url_match = re.match("^/(\w+)/(.+)$", obj.url) + if url_match: + module = url_match.group(1) + module_path = get_module_path(module) + module_resource_path = get_resource_path(module, url_match.group(2)) + if module_path and module_resource_path: + module_path = os.path.join(os.path.normpath(module_path), '') # join ensures the path ends with '/' + module_resource_path = os.path.normpath(module_resource_path) + if module_resource_path.startswith(module_path): + with open(module_resource_path, 'r') as f: + content = base64.b64encode(f.read()) + last_update = str(os.path.getmtime(module_resource_path)) + + if not module_resource_path: + module_resource_path = obj.url + + if not content: + status = 301 + content = module_resource_path + else: + content = obj[field] or '' # filename if not filename: if filename_field in obj: filename = obj[filename_field] + elif module_resource_path: + filename = os.path.basename(module_resource_path) else: filename = "%s-%s-%s" % (obj._model._name, obj.id, field) @@ -486,31 +514,19 @@ def binary_content(xmlid=None, model='ir.attachment', id=None, field='datas', un mimetype = mimetypes.guess_type(filename)[0] if not mimetype: mimetype = default_mimetype - headers = [('Content-Type', mimetype)] + headers.append(('Content-Type', mimetype)) # cache etag = hasattr(request, 'httprequest') and request.httprequest.headers.get('If-None-Match') retag = hashlib.md5(last_update).hexdigest() - if etag == retag: - status = 304 + status = status or (304 if etag == retag else 200) headers.append(('ETag', retag)) - - if unique: - headers.append(('Cache-Control', 'max-age=%s' % STATIC_CACHE)) - else: - headers.append(('Cache-Control', 'max-age=0')) + headers.append(('Cache-Control', 'max-age=%s' % (STATIC_CACHE if unique else 0))) # content-disposition default name if download: headers.append(('Content-Disposition', content_disposition(filename))) - # get content after cache control - if model == 'ir.attachment' and obj.type == 'url' and obj.url: - status = 301 - content = obj.url - else: - content = obj[field] or '' - return (status, headers, content) def db_info(): @@ -1161,7 +1177,7 @@ class Binary(http.Controller): ], type='http', auth="none", cors="*") def company_logo(self, dbname=None, **kw): imgname = 'logo.png' - placeholder = functools.partial(get_module_resource, 'web', 'static', 'src', 'img') + placeholder = functools.partial(get_resource_path, 'web', 'static', 'src', 'img') uid = None if request.session.db: dbname = request.session.db |
