From 83334ab16605eb2f03d844dfac534e05bea0d698 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Sat, 12 Oct 2013 21:44:35 +0300 Subject: [PATCH] implemented media files cleanup --- doc/todo.txt | 2 -- motioneye.py | 11 ++++++++--- settings.py | 2 +- src/cleanup.py | 14 +++++++++----- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/doc/todo.txt b/doc/todo.txt index a127156..8b72347 100644 --- a/doc/todo.txt +++ b/doc/todo.txt @@ -1,6 +1,4 @@ --> implement file preserving - -> style scroll bars -> hint text next to section titles -> clickable hints diff --git a/motioneye.py b/motioneye.py index aaea8ff..e6a962c 100755 --- a/motioneye.py +++ b/motioneye.py @@ -143,9 +143,14 @@ def _start_cleanup(): import cleanup def do_cleanup(): - cleanup.cleanup_images() - cleanup.cleanup_movies() - + try: + cleanup.cleanup_images() + cleanup.cleanup_movies() + + except Exception as e: + logging.error('failed to cleanup media files: %(msg)s' % { + 'msg': unicode(e)}) + ioloop = tornado.ioloop.IOLoop.instance() ioloop.add_timeout(datetime.timedelta(seconds=settings.CLEANUP_INTERVAL), do_cleanup) diff --git a/settings.py b/settings.py index 3131e6c..3b00366 100644 --- a/settings.py +++ b/settings.py @@ -20,4 +20,4 @@ LISTEN = '0.0.0.0' PORT = 8765 MOTION_CHECK_INTERVAL = 10 -CLEANUP_INTERVAL = 10 +CLEANUP_INTERVAL = 3 diff --git a/src/cleanup.py b/src/cleanup.py index ed6d25e..75e9890 100644 --- a/src/cleanup.py +++ b/src/cleanup.py @@ -6,18 +6,22 @@ import os import config -def _remove_older_files(dir, moment): +def _remove_older_files(dir, moment, exts): for name in os.listdir(dir): full_path = os.path.join(dir, name) if not os.path.isfile(full_path): continue - file_moment = datetime.datetime.fromtimestamp(os.path.getmtime(file)) + full_path_lower = full_path.lower() + if not [e for e in exts if full_path_lower.endswith(e)]: + continue + + file_moment = datetime.datetime.fromtimestamp(os.path.getmtime(full_path)) if file_moment < moment: logging.debug('removing file %(path)s...' % { 'path': full_path}) - #os.remove(full_path) + os.remove(full_path) def cleanup_images(): @@ -46,7 +50,7 @@ def cleanup_images(): if jpeg_filename: snapshot_path = os.path.join(target_dir, jpeg_filename) snapshot_path = os.path.dirname(snapshot_path) - _remove_older_files(dir, preserve_moment) + _remove_older_files(snapshot_path, preserve_moment, exts=['.jpg', '.png']) def cleanup_movies(): @@ -69,4 +73,4 @@ def cleanup_movies(): if movie_filename: snapshot_path = os.path.join(target_dir, movie_filename) snapshot_path = os.path.dirname(snapshot_path) - _remove_older_files(dir, preserve_moment) + _remove_older_files(snapshot_path, preserve_moment, exts=['.avi']) -- 2.39.5