mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-23 04:22:04 -06:00
[IMP] attachment_preview: improve performance for large files with filenames (#100)
This commit is contained in:
parent
b71c6e8327
commit
0d001986dd
@ -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",
|
||||||
|
@ -18,12 +18,16 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
##############################################################################
|
##############################################################################
|
||||||
import collections
|
|
||||||
import os.path
|
|
||||||
import mimetypes
|
|
||||||
import base64
|
import base64
|
||||||
|
import collections
|
||||||
|
import logging
|
||||||
|
import mimetypes
|
||||||
|
import os.path
|
||||||
|
|
||||||
from openerp.osv.orm import Model
|
from openerp.osv.orm import Model
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class IrAttachment(Model):
|
class IrAttachment(Model):
|
||||||
_inherit = 'ir.attachment'
|
_inherit = 'ir.attachment'
|
||||||
@ -32,35 +36,58 @@ 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]
|
||||||
if not this.id:
|
# First pass: load fields in bin_size mode to avoid loading big files
|
||||||
result[this.id] = False
|
# unnecessarily.
|
||||||
continue
|
if filename_field:
|
||||||
extension = ''
|
for this in self.pool[model].browse(cr, uid, ids_to_browse,
|
||||||
if filename_field and this[filename_field]:
|
context=context_bin_size):
|
||||||
filename, extension = os.path.splitext(this[filename_field])
|
if not this.id:
|
||||||
if not this[binary_field]:
|
result[this.id] = False
|
||||||
result[this.id] = False
|
continue
|
||||||
continue
|
extension = ''
|
||||||
if not extension:
|
if this[filename_field]:
|
||||||
try:
|
filename, extension = os.path.splitext(
|
||||||
import magic
|
this[filename_field])
|
||||||
ms = magic.open(
|
if this[binary_field] and extension:
|
||||||
hasattr(magic, 'MAGIC_MIME_TYPE') and
|
result[this.id] = extension
|
||||||
magic.MAGIC_MIME_TYPE or magic.MAGIC_MIME)
|
_logger.debug('Got extension %s from filename %s',
|
||||||
ms.load()
|
extension, this[filename_field])
|
||||||
|
# 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:
|
||||||
|
import magic
|
||||||
|
ms = magic.open(
|
||||||
|
hasattr(magic, 'MAGIC_MIME_TYPE') and
|
||||||
|
magic.MAGIC_MIME_TYPE or magic.MAGIC_MIME)
|
||||||
|
ms.load()
|
||||||
|
if model == self._name and binary_field == 'datas'\
|
||||||
|
and this.store_fname:
|
||||||
|
mimetype = ms.file(
|
||||||
|
this._full_path(this.store_fname))
|
||||||
|
_logger.debug('Magic determined mimetype %s from file %s',
|
||||||
|
mimetype, this.store_fname)
|
||||||
|
else:
|
||||||
mimetype = ms.buffer(
|
mimetype = ms.buffer(
|
||||||
base64.b64decode(this[binary_field]))
|
base64.b64decode(this[binary_field]))
|
||||||
except ImportError:
|
_logger.debug('Magic determined mimetype %s from buffer',
|
||||||
(mimetype, encoding) = mimetypes.guess_type(
|
mimetype)
|
||||||
'data:;base64,' + this[binary_field], strict=False)
|
except ImportError:
|
||||||
extension = mimetypes.guess_extension(
|
(mimetype, encoding) = mimetypes.guess_type(
|
||||||
mimetype.split(';')[0], strict=False)
|
'data:;base64,' + this[binary_field], strict=False)
|
||||||
|
_logger.debug('Mimetypes guessed type %s from buffer',
|
||||||
result[this.id] = (extension or '').lstrip('.').lower()
|
mimetype)
|
||||||
|
extension = mimetypes.guess_extension(
|
||||||
|
mimetype.split(';')[0], strict=False)
|
||||||
|
result[this.id] = extension
|
||||||
|
for _id in result:
|
||||||
|
result[_id] = (result[_id] 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):
|
||||||
|
Loading…
Reference in New Issue
Block a user