diff --git a/attachment_preview/__openerp__.py b/attachment_preview/__openerp__.py index f82f178a..c6fb04c5 100644 --- a/attachment_preview/__openerp__.py +++ b/attachment_preview/__openerp__.py @@ -29,6 +29,9 @@ This addons allows to preview attachments supported by http://viewerjs.org. Currently, that's most Libreoffice files and PDFs. +For filetype recognition, you'll get the best results by installing +``python-magic``. + Acknowledgements ================ diff --git a/attachment_preview/model/ir_attachment.py b/attachment_preview/model/ir_attachment.py index 233a15a4..773367b3 100644 --- a/attachment_preview/model/ir_attachment.py +++ b/attachment_preview/model/ir_attachment.py @@ -20,6 +20,8 @@ ############################################################################## import collections import os.path +import mimetypes +import base64 from openerp.osv.orm import Model @@ -35,9 +37,18 @@ class IrAttachment(Model): extension = '' if this.datas_fname: filename, extension = os.path.splitext(this.datas_fname) - extension = extension.lstrip('.') if not extension: - # TODO: lookup the extension via mimetype - pass - result[this.id] = extension + try: + import magic + ms = magic.open(magic.MAGIC_MIME_TYPE) + ms.load() + mimetype = ms.buffer( + base64.b64decode(this.datas)) + except ImportError: + (mimetype, encoding) = mimetypes.guess_type( + 'data:;base64,' + this.datas, strict=False) + extension = mimetypes.guess_extension( + mimetype, strict=False) + + result[this.id] = (extension or '').lstrip('.').lower() return result if isinstance(ids, collections.Iterable) else result[ids]