Files
mayan-edms/mayan/apps/file_metadata/drivers/exiftool.py
Roberto Rosario 0a7908baca File metadata: Add file metadata app
Add the file metadata app. This app uses binary wrappers called drivers
to extract properties from the file of documents. The default driver
uses the exiftool to extract the EXIF record from JPEG images.
The exiftool can also extra some properties from other files like
PDFs, office files and sound file.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
2018-12-14 02:16:38 -04:00

71 lines
1.9 KiB
Python

from __future__ import unicode_literals
import json
import logging
import sh
from django.utils.translation import ugettext_lazy as _
from mayan.apps.common.utils import fs_cleanup, mkstemp
from ..classes import FileMetadataDriver
from ..settings import setting_drivers_arguments
logger = logging.getLogger(__name__)
class EXIFToolDriver(FileMetadataDriver):
label = _('EXIF Tool')
internal_name = 'exiftool'
def __init__(self, *args, **kwargs):
try:
self.command_exiftool = sh.Command(
setting_drivers_arguments.value['exif_driver']['exiftool_path']
)
except sh.CommandNotFound:
self.command_exiftool = None
else:
self.command_exiftool = self.command_exiftool.bake('-j')
def _process(self, document_version):
new_file_object, temp_filename = mkstemp()
try:
document_version.save_to_file(filepath=temp_filename)
result = self.command_exiftool(temp_filename)
return json.loads(result.stdout)[0]
finally:
fs_cleanup(filename=temp_filename)
EXIFToolDriver.register(
mimetypes=(
'application/msword',
'application/pdf',
'application/vnd.oasis.opendocument.text',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/x-bittorrent',
'application/x-gzip',
'application/x-rar-compressed',
'application/x-shockwave-flash',
'application/zip',
'application/zip',
'audio/x-pn-realaudio-plugin',
'audio/x-wav',
'image/jpeg',
'image/png',
'image/svg+xml',
'image/tiff',
'image/x-portable-pixmap',
'text/html',
'text/rtf',
'text/x-sh',
'video/mp4',
'video/webm',
'video/x-flv',
'video/x-matroska'
)
)