From 4899c1a4f991ea8e8b1edeab7c00905f70f2db1c Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Tue, 16 Aug 2022 15:17:36 +0200 Subject: [PATCH] Crash fix: Data store sub-directories werent always being created when needed (#842) --- changedetectionio/model/Watch.py | 10 +++++++--- changedetectionio/store.py | 11 ++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/changedetectionio/model/Watch.py b/changedetectionio/model/Watch.py index c5e27d54..c59dd1da 100644 --- a/changedetectionio/model/Watch.py +++ b/changedetectionio/model/Watch.py @@ -83,6 +83,12 @@ class model(dict): return False + def ensure_data_dir_exists(self): + target_path = os.path.join(self.__datastore_path, self['uuid']) + if not os.path.isdir(target_path): + print ("> Creating data dir {}".format(target_path)) + os.mkdir(target_path) + @property def label(self): # Used for sorting @@ -149,9 +155,7 @@ class model(dict): output_path = "{}/{}".format(self.__datastore_path, self['uuid']) - # Incase the operator deleted it, check and create. - if not os.path.isdir(output_path): - os.mkdir(output_path) + self.ensure_data_dir_exists() snapshot_fname = "{}/{}.stripped.txt".format(output_path, uuid.uuid4()) logging.debug("Saving history text {}".format(snapshot_fname)) diff --git a/changedetectionio/store.py b/changedetectionio/store.py index 1f2ae453..5c3aedad 100644 --- a/changedetectionio/store.py +++ b/changedetectionio/store.py @@ -8,7 +8,7 @@ import threading import time import uuid as uuid_builder from copy import deepcopy -from os import mkdir, path, unlink +from os import path, unlink from threading import Lock import re import requests @@ -324,12 +324,7 @@ class ChangeDetectionStore: new_watch.update(apply_extras) self.__data['watching'][new_uuid]=new_watch - # Get the directory ready - output_path = "{}/{}".format(self.datastore_path, new_uuid) - try: - mkdir(output_path) - except FileExistsError: - print(output_path, "already exists.") + self.__data['watching'][new_uuid].ensure_data_dir_exists() if write_to_disk_now: self.sync_to_json() @@ -352,6 +347,8 @@ class ChangeDetectionStore: else: target_path = os.path.join(self.datastore_path, watch_uuid, "last-screenshot.png") + self.data['watching'][watch_uuid].ensure_data_dir_exists() + with open(target_path, 'wb') as f: f.write(screenshot) f.close()