2018-03-25 15:09:12 +02:00
|
|
|
import subprocess
|
2018-06-22 20:38:29 +02:00
|
|
|
import imghdr
|
2018-03-25 15:09:12 +02:00
|
|
|
import json
|
2018-04-01 00:43:36 +02:00
|
|
|
import os
|
2018-06-06 23:50:25 +02:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2018-06-08 17:34:53 +02:00
|
|
|
import re
|
2018-03-25 15:09:12 +02:00
|
|
|
|
2018-04-01 12:30:00 +02:00
|
|
|
import cairo
|
|
|
|
|
2018-03-25 15:09:12 +02:00
|
|
|
import gi
|
|
|
|
gi.require_version('GdkPixbuf', '2.0')
|
|
|
|
from gi.repository import GdkPixbuf
|
|
|
|
|
|
|
|
from . import abstract
|
|
|
|
|
2018-04-04 23:21:48 +02:00
|
|
|
|
2018-07-02 00:22:05 +02:00
|
|
|
class _ImageParser(abstract.AbstractParser):
|
2018-06-08 17:34:53 +02:00
|
|
|
@staticmethod
|
2018-07-02 00:22:05 +02:00
|
|
|
def __handle_problematic_filename(filename: str, callback) -> str:
|
2018-06-08 17:34:53 +02:00
|
|
|
""" This method takes a filename with a problematic name,
|
|
|
|
and safely applies it a `callback`."""
|
|
|
|
tmpdirname = tempfile.mkdtemp()
|
|
|
|
fname = os.path.join(tmpdirname, "temp_file")
|
|
|
|
shutil.copy(filename, fname)
|
|
|
|
out = callback(fname)
|
|
|
|
shutil.rmtree(tmpdirname)
|
|
|
|
return out
|
|
|
|
|
2018-06-06 23:50:25 +02:00
|
|
|
def get_meta(self):
|
|
|
|
""" There is no way to escape the leading(s) dash(es) of the current
|
2018-06-08 17:34:53 +02:00
|
|
|
self.filename to prevent parameter injections, so we need to take care
|
|
|
|
of this.
|
2018-06-06 23:50:25 +02:00
|
|
|
"""
|
2018-06-08 17:34:53 +02:00
|
|
|
fun = lambda f: subprocess.check_output(['/usr/bin/exiftool', '-json', f])
|
2018-06-10 00:43:38 +02:00
|
|
|
if re.search('^[a-z0-9/]', self.filename) is None:
|
2018-06-08 17:34:53 +02:00
|
|
|
out = self.__handle_problematic_filename(self.filename, fun)
|
|
|
|
else:
|
|
|
|
out = fun(self.filename)
|
2018-06-06 23:50:25 +02:00
|
|
|
meta = json.loads(out.decode('utf-8'))[0]
|
|
|
|
for key in self.meta_whitelist:
|
|
|
|
meta.pop(key, None)
|
|
|
|
return meta
|
|
|
|
|
2018-07-02 00:22:05 +02:00
|
|
|
class PNGParser(_ImageParser):
|
2018-04-01 12:30:00 +02:00
|
|
|
mimetypes = {'image/png', }
|
|
|
|
meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
|
2018-05-16 22:36:59 +02:00
|
|
|
'Directory', 'FileSize', 'FileModifyDate',
|
|
|
|
'FileAccessDate', 'FileInodeChangeDate',
|
|
|
|
'FilePermissions', 'FileType', 'FileTypeExtension',
|
|
|
|
'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType',
|
|
|
|
'Compression', 'Filter', 'Interlace', 'BackgroundColor',
|
|
|
|
'ImageSize', 'Megapixels', 'ImageHeight'}
|
2018-04-01 12:30:00 +02:00
|
|
|
|
2018-05-06 21:58:31 +02:00
|
|
|
def __init__(self, filename):
|
|
|
|
super().__init__(filename)
|
|
|
|
try: # better fail here than later
|
|
|
|
cairo.ImageSurface.create_from_png(self.filename)
|
|
|
|
except MemoryError:
|
|
|
|
raise ValueError
|
|
|
|
|
2018-04-01 12:30:00 +02:00
|
|
|
def remove_all(self):
|
|
|
|
surface = cairo.ImageSurface.create_from_png(self.filename)
|
|
|
|
surface.write_to_png(self.output_filename)
|
|
|
|
return True
|
|
|
|
|
2018-04-04 23:21:48 +02:00
|
|
|
|
2018-07-02 00:22:05 +02:00
|
|
|
class GdkPixbufAbstractParser(_ImageParser):
|
2018-04-02 23:40:08 +02:00
|
|
|
""" GdkPixbuf can handle a lot of surfaces, so we're rending images on it,
|
|
|
|
this has the side-effect of removing metadata completely.
|
|
|
|
"""
|
2018-06-22 20:38:29 +02:00
|
|
|
_type = ''
|
|
|
|
|
2018-03-25 15:09:12 +02:00
|
|
|
def remove_all(self):
|
2018-04-01 00:43:36 +02:00
|
|
|
_, extension = os.path.splitext(self.filename)
|
2018-03-25 15:09:12 +02:00
|
|
|
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
|
2018-04-01 00:43:36 +02:00
|
|
|
if extension == '.jpg':
|
2018-06-08 17:34:53 +02:00
|
|
|
extension = '.jpeg' # gdk is picky
|
2018-04-16 22:24:41 +02:00
|
|
|
pixbuf.savev(self.output_filename, extension[1:], [], [])
|
2018-03-25 15:09:12 +02:00
|
|
|
return True
|
2018-04-01 00:43:36 +02:00
|
|
|
|
2018-06-22 20:38:29 +02:00
|
|
|
def __init__(self, filename):
|
|
|
|
super().__init__(filename)
|
|
|
|
if imghdr.what(filename) != self._type: # better safe than sorry
|
|
|
|
raise ValueError
|
|
|
|
|
2018-04-01 00:43:36 +02:00
|
|
|
|
|
|
|
class JPGParser(GdkPixbufAbstractParser):
|
2018-06-22 20:38:29 +02:00
|
|
|
_type = 'jpeg'
|
2018-04-03 23:56:39 +02:00
|
|
|
mimetypes = {'image/jpeg'}
|
2018-04-01 00:43:36 +02:00
|
|
|
meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
|
2018-05-16 22:36:59 +02:00
|
|
|
'Directory', 'FileSize', 'FileModifyDate',
|
|
|
|
'FileAccessDate', "FileInodeChangeDate",
|
|
|
|
'FilePermissions', 'FileType', 'FileTypeExtension',
|
|
|
|
'MIMEType', 'ImageWidth', 'ImageSize', 'BitsPerSample',
|
|
|
|
'ColorComponents', 'EncodingProcess', 'JFIFVersion',
|
|
|
|
'ResolutionUnit', 'XResolution', 'YCbCrSubSampling',
|
|
|
|
'YResolution', 'Megapixels', 'ImageHeight'}
|
2018-04-01 00:43:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TiffParser(GdkPixbufAbstractParser):
|
2018-06-22 20:38:29 +02:00
|
|
|
_type = 'tiff'
|
2018-04-01 00:43:36 +02:00
|
|
|
mimetypes = {'image/tiff'}
|
|
|
|
meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples',
|
2018-05-16 22:36:59 +02:00
|
|
|
'FillOrder', 'PhotometricInterpretation',
|
|
|
|
'PlanarConfiguration', 'RowsPerStrip', 'SamplesPerPixel',
|
|
|
|
'StripByteCounts', 'StripOffsets', 'BitsPerSample',
|
|
|
|
'Directory', 'ExifToolVersion', 'FileAccessDate',
|
|
|
|
'FileInodeChangeDate', 'FileModifyDate', 'FileName',
|
|
|
|
'FilePermissions', 'FileSize', 'FileType',
|
|
|
|
'FileTypeExtension', 'ImageHeight', 'ImageSize',
|
|
|
|
'ImageWidth', 'MIMEType', 'Megapixels', 'SourceFile'}
|