Implement lightweight cleaning for jpg
This commit is contained in:
parent
1040a594d6
commit
fe885babee
@ -6,7 +6,7 @@ import cairo
|
||||
|
||||
import gi
|
||||
gi.require_version('GdkPixbuf', '2.0')
|
||||
from gi.repository import GdkPixbuf
|
||||
from gi.repository import GdkPixbuf, GLib
|
||||
|
||||
from . import exiftool
|
||||
|
||||
@ -50,15 +50,21 @@ class GdkPixbufAbstractParser(exiftool.ExiftoolParser):
|
||||
|
||||
def __init__(self, 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
|
||||
|
||||
def remove_all(self) -> bool:
|
||||
if self.lightweight_cleaning:
|
||||
return self._lightweight_cleanup()
|
||||
|
||||
_, extension = os.path.splitext(self.filename)
|
||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
|
||||
if extension.lower() == '.jpg':
|
||||
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
|
||||
|
||||
|
||||
|
@ -4,7 +4,7 @@ import unittest
|
||||
import shutil
|
||||
import os
|
||||
|
||||
from libmat2 import pdf, images
|
||||
from libmat2 import pdf, images, torrent
|
||||
|
||||
class TestLightWeightCleaning(unittest.TestCase):
|
||||
def test_pdf(self):
|
||||
@ -63,3 +63,20 @@ class TestLightWeightCleaning(unittest.TestCase):
|
||||
|
||||
os.remove('./tests/data/clean.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')
|
||||
|
Loading…
Reference in New Issue
Block a user