From ce6c2737a8b6bb276f660403501ea37d68b43a7a Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Tue, 22 Nov 2022 18:08:46 +0100 Subject: [PATCH] Notification screenshot/JPEG was not being regenerated correctly (#1149) --- changedetectionio/model/Watch.py | 17 ++++------------- changedetectionio/store.py | 6 ++++++ changedetectionio/tests/test_notification.py | 9 ++++++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py index 62eaf33b..0b94aa65 100644 --- a/changedetectionio/model/Watch.py +++ b/changedetectionio/model/Watch.py @@ -252,20 +252,11 @@ class model(dict): return None def get_screenshot_as_jpeg(self): - """Best used in notifications due to its smaller size""" - png_fname = os.path.join(self.watch_data_dir, "last-screenshot.png") - jpg_fname = os.path.join(self.watch_data_dir, "last-screenshot.jpg") - - if os.path.isfile(jpg_fname): - return jpg_fname - - if os.path.isfile(png_fname) and not os.path.isfile(jpg_fname): - # Doesnt exist, so create the JPEG from the PNG - from PIL import Image - im1 = Image.open(png_fname) - im1.convert('RGB').save(jpg_fname, quality=int(os.getenv("NOTIFICATION_SCREENSHOT_JPG_QUALITY", 75))) - return jpg_fname + # Created by save_screenshot() + fname = os.path.join(self.watch_data_dir, "last-screenshot.jpg") + if os.path.isfile(fname): + return fname # False is not an option for AppRise, must be type None return None diff --git a/changedetectionio/store.py b/changedetectionio/store.py index 04d4fb39..adfba663 100644 --- a/changedetectionio/store.py +++ b/changedetectionio/store.py @@ -368,6 +368,12 @@ class ChangeDetectionStore: f.write(screenshot) f.close() + # Make a JPEG that's used in notifications (due to being a smaller size) available + from PIL import Image + im1 = Image.open(target_path) + im1.convert('RGB').save(target_path.replace('.png','.jpg'), quality=int(os.getenv("NOTIFICATION_SCREENSHOT_JPG_QUALITY", 75))) + + def save_error_text(self, watch_uuid, contents): if not self.data['watching'].get(watch_uuid): return diff --git a/changedetectionio/tests/test_notification.py b/changedetectionio/tests/test_notification.py index b71e5470..e4999962 100644 --- a/changedetectionio/tests/test_notification.py +++ b/changedetectionio/tests/test_notification.py @@ -71,13 +71,18 @@ def test_check_notification(client, live_server): time.sleep(3) # We write the PNG to disk, but a JPEG should appear in the notification - testimage_png = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=' # Write the last screenshot png + testimage_png = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=' + # This one is created when we save the screenshot from the webdriver/playwright session (converted from PNG) + testimage_jpg = '/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/wAALCAABAAEBAREA/8QAFAABAAAAAAAAAAAAAAAAAAAACf/EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEAAD8AKp//2Q==' + uuid = extract_UUID_from_client(client) datastore = 'test-datastore' with open(os.path.join(datastore, str(uuid), 'last-screenshot.png'), 'wb') as f: f.write(base64.b64decode(testimage_png)) + with open(os.path.join(datastore, str(uuid), 'last-screenshot.jpg'), 'wb') as f: + f.write(base64.b64decode(testimage_jpg)) # Goto the edit page, add our ignore text # Add our URL to the import page @@ -128,8 +133,6 @@ def test_check_notification(client, live_server): time.sleep(3) set_modified_response() - notification_submission = None - # Trigger a check client.get(url_for("form_watch_checknow"), follow_redirects=True) time.sleep(3)