Skip to content
Snippets Groups Projects
Commit fe885bab authored by Julien (jvoisin) Voisin's avatar Julien (jvoisin) Voisin
Browse files

Implement lightweight cleaning for jpg

parent 1040a594
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ import cairo ...@@ -6,7 +6,7 @@ import cairo
import gi import gi
gi.require_version('GdkPixbuf', '2.0') gi.require_version('GdkPixbuf', '2.0')
from gi.repository import GdkPixbuf from gi.repository import GdkPixbuf, GLib
from . import exiftool from . import exiftool
...@@ -50,15 +50,21 @@ class GdkPixbufAbstractParser(exiftool.ExiftoolParser): ...@@ -50,15 +50,21 @@ class GdkPixbufAbstractParser(exiftool.ExiftoolParser):
def __init__(self, filename): def __init__(self, filename):
super().__init__(filename) super().__init__(filename)
if imghdr.what(filename) != self._type: # better safe than sorry # we can't use imghdr here because of https://bugs.python.org/issue28591
try:
GdkPixbuf.Pixbuf.new_from_file(self.filename)
except GLib.GError:
raise ValueError raise ValueError
def remove_all(self) -> bool: def remove_all(self) -> bool:
if self.lightweight_cleaning:
return self._lightweight_cleanup()
_, extension = os.path.splitext(self.filename) _, extension = os.path.splitext(self.filename)
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
if extension.lower() == '.jpg': if extension.lower() == '.jpg':
extension = '.jpeg' # gdk is picky extension = '.jpeg' # gdk is picky
pixbuf.savev(self.output_filename, extension[1:], [], []) pixbuf.savev(self.output_filename, type=extension[1:], option_keys=[], option_values=[])
return True return True
......
...@@ -4,7 +4,7 @@ import unittest ...@@ -4,7 +4,7 @@ import unittest
import shutil import shutil
import os import os
from libmat2 import pdf, images from libmat2 import pdf, images, torrent
class TestLightWeightCleaning(unittest.TestCase): class TestLightWeightCleaning(unittest.TestCase):
def test_pdf(self): def test_pdf(self):
...@@ -63,3 +63,20 @@ class TestLightWeightCleaning(unittest.TestCase): ...@@ -63,3 +63,20 @@ class TestLightWeightCleaning(unittest.TestCase):
os.remove('./tests/data/clean.jpg') os.remove('./tests/data/clean.jpg')
os.remove('./tests/data/clean.cleaned.jpg') os.remove('./tests/data/clean.cleaned.jpg')
def test_torrent(self):
shutil.copy('./tests/data/dirty.torrent', './tests/data/clean.torrent')
p = torrent.TorrentParser('./tests/data/clean.torrent')
meta = p.get_meta()
self.assertEqual(meta['created by'], b'mktorrent 1.0')
p.lightweight_cleaning = True
ret = p.remove_all()
self.assertTrue(ret)
p = torrent.TorrentParser('./tests/data/clean.cleaned.torrent')
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.torrent')
os.remove('./tests/data/clean.cleaned.torrent')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment