1
0
mirror of synced 2024-11-22 17:24:23 +01:00

Implement support in get_meta for deep meta in office-related files

This commit is contained in:
jvoisin 2018-04-01 15:08:38 +02:00
parent 27beda354d
commit 9e7a4bd217

View File

@ -4,6 +4,7 @@ import re
import shutil import shutil
import subprocess import subprocess
import tempfile import tempfile
import datetime
import zipfile import zipfile
from . import abstract, parser_factory from . import abstract, parser_factory
@ -16,6 +17,25 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
zipinfo.date_time = (1980, 1, 1, 0, 0, 0) zipinfo.date_time = (1980, 1, 1, 0, 0, 0)
return zipinfo return zipinfo
def _get_zipinfo_meta(self, zipinfo:zipfile.ZipInfo) -> dict:
metadata = {}
if zipinfo.create_system == 3:
#metadata['create_system'] = 'Linux'
pass
elif zipinfo.create_system == 2:
metadata['create_system'] = 'Windows'
else:
metadata['create_system'] = 'Weird'
if zipinfo.comment:
metadata['comment'] = zipinfo.comment
if zipinfo.date_time != (1980, 1, 1, 0, 0, 0):
metadata['comment'] = datetime.datetime(*zipinfo.date_time)
return metadata
def _clean_internal_file(self, item:zipfile.ZipInfo, temp_folder:str, zin:zipfile.ZipFile, zout:zipfile.ZipFile): def _clean_internal_file(self, item:zipfile.ZipInfo, temp_folder:str, zin:zipfile.ZipFile, zout:zipfile.ZipFile):
zin.extract(member=item, path=temp_folder) zin.extract(member=item, path=temp_folder)
tmp_parser = parser_factory.get_parser(os.path.join(temp_folder, item.filename)) tmp_parser = parser_factory.get_parser(os.path.join(temp_folder, item.filename))
@ -43,13 +63,15 @@ class MSOfficeParser(ArchiveBasedAbstractParser):
""" """
metadata = {} metadata = {}
zipin = zipfile.ZipFile(self.filename) zipin = zipfile.ZipFile(self.filename)
for item in zipin.namelist(): for item in zipin.infolist():
if item.startswith('docProps/') and item.endswith('.xml'): if item.filename.startswith('docProps/') and item.filename.endswith('.xml'):
content = zipin.read(item).decode('utf-8') content = zipin.read(item).decode('utf-8')
for (key, value) in re.findall(r"<(.+)>(.+)</\1>", content, re.I): for (key, value) in re.findall(r"<(.+)>(.+)</\1>", content, re.I):
metadata[key] = value metadata[key] = value
if not metadata: # better safe than sorry if not metadata: # better safe than sorry
metadata[item] = 'harmful content' metadata[item] = 'harmful content'
metadata = {**metadata, **self._get_zipinfo_meta(item)}
zipin.close() zipin.close()
return metadata return metadata
@ -95,13 +117,14 @@ class LibreOfficeParser(ArchiveBasedAbstractParser):
""" """
metadata = {} metadata = {}
zipin = zipfile.ZipFile(self.filename) zipin = zipfile.ZipFile(self.filename)
for item in zipin.namelist(): for item in zipin.infolist():
if item == 'meta.xml': if item.filename == 'meta.xml':
content = zipin.read(item).decode('utf-8') content = zipin.read(item).decode('utf-8')
for (key, value) in re.findall(r"<((?:meta|dc|cp).+?)>(.+)</\1>", content, re.I): for (key, value) in re.findall(r"<((?:meta|dc|cp).+?)>(.+)</\1>", content, re.I):
metadata[key] = value metadata[key] = value
if not metadata: # better safe than sorry if not metadata: # better safe than sorry
metadata[item] = 'harmful content' metadata[item] = 'harmful content'
metadata = {**metadata, **self._get_zipinfo_meta(item)}
zipin.close() zipin.close()
return metadata return metadata