[IMP] rewrite get_binary_extension so that it uses a first pass to determine the extension from the filename only

This commit is contained in:
Thomas Rehn 2016-08-25 08:02:46 +02:00
parent 7f2b9efc01
commit 47f0eafe9a
2 changed files with 40 additions and 30 deletions

View File

@ -20,7 +20,7 @@
############################################################################## ##############################################################################
{ {
"name": "Preview attachments", "name": "Preview attachments",
"version": "8.0.1.1.0", "version": "8.0.1.2.0",
"author": "Therp BV,Odoo Community Association (OCA)", "author": "Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3", "license": "AGPL-3",
"complexity": "normal", "complexity": "normal",

View File

@ -32,20 +32,29 @@ class IrAttachment(Model):
self, cr, uid, model, ids, binary_field, filename_field=None, self, cr, uid, model, ids, binary_field, filename_field=None,
context=None): context=None):
result = {} result = {}
for this in self.pool[model].browse( context_bin_size = dict(context or {})
cr, uid, context_bin_size['bin_size'] = True
ids if isinstance(ids, collections.Iterable) else [ids], ids_to_browse = ids if isinstance(ids, collections.Iterable) \
context=context): else [ids]
# First pass: load fields in bin_size mode to avoid loading big files
# unnecessarily.
if filename_field:
for this in self.pool[model].browse(cr, uid, ids_to_browse,
context=context_bin_size):
if not this.id: if not this.id:
result[this.id] = False result[this.id] = False
continue continue
extension = '' extension = ''
if filename_field and this[filename_field]: if this[filename_field]:
filename, extension = os.path.splitext(this[filename_field]) filename, extension = os.path.splitext(
if not this[binary_field]: this[filename_field])
result[this.id] = False if this[binary_field] and extension:
continue result[this.id] = extension
if not extension: # Second pass for all attachments which have to be loaded fully
# to get the extension from the content
ids_to_browse = [_id for _id in ids_to_browse if _id not in result]
for this in self.pool[model].browse(cr, uid, ids_to_browse,
context=context):
try: try:
import magic import magic
ms = magic.open( ms = magic.open(
@ -59,8 +68,9 @@ class IrAttachment(Model):
'data:;base64,' + this[binary_field], strict=False) 'data:;base64,' + this[binary_field], strict=False)
extension = mimetypes.guess_extension( extension = mimetypes.guess_extension(
mimetype.split(';')[0], strict=False) mimetype.split(';')[0], strict=False)
result[this.id] = extension
result[this.id] = (extension or '').lstrip('.').lower() for _id in result:
result[_id] = (extension or '').lstrip('.').lower()
return result if isinstance(ids, collections.Iterable) else result[ids] return result if isinstance(ids, collections.Iterable) else result[ids]
def get_attachment_extension(self, cr, uid, ids, context=None): def get_attachment_extension(self, cr, uid, ids, context=None):