mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-16 12:12:57 -06:00
[ADD] use libmagic or mimetypes to detect a file's type
This commit is contained in:
parent
7a7c7d6274
commit
9ff3f16430
@ -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
|
||||
================
|
||||
|
||||
|
@ -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]
|
||||
|
Loading…
Reference in New Issue
Block a user