From 565f954d25d1164eb914331b9de9390fd68830d6 Mon Sep 17 00:00:00 2001 From: Dan Done Date: Sun, 24 Sep 2023 20:51:19 +0100 Subject: [PATCH] #200 - Added error handling for deletions. --- SynoAI/Controllers/CameraController.cs | 31 ++++++++++++++++---------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/SynoAI/Controllers/CameraController.cs b/SynoAI/Controllers/CameraController.cs index 2a82ca8..44b74f4 100644 --- a/SynoAI/Controllers/CameraController.cs +++ b/SynoAI/Controllers/CameraController.cs @@ -336,23 +336,30 @@ private void CleanupOldImages() _logger.LogInformation($"Captures Clean Up: Cleaning up images older than {Config.DaysToKeepCaptures} day(s)."); Task.Run(() => { - lock (_cleanUpOldImagesLock) + try { - _cleanupOldImagesRunning = true; - - DirectoryInfo directory = new(Constants.DIRECTORY_CAPTURES); - IEnumerable files = directory.GetFiles("*", new EnumerationOptions() { RecurseSubdirectories = true }); - foreach (FileInfo file in files) + lock (_cleanUpOldImagesLock) { - double age = (DateTime.Now - file.CreationTime).TotalDays; - if (age > Config.DaysToKeepCaptures) + _cleanupOldImagesRunning = true; + + DirectoryInfo directory = new(Constants.DIRECTORY_CAPTURES); + IEnumerable files = directory.GetFiles("*", new EnumerationOptions() { RecurseSubdirectories = true }); + foreach (FileInfo file in files) { - _logger.LogInformation($"Captures Clean Up: {file.FullName} is {age} day(s) old and will be deleted."); - System.IO.File.Delete(file.FullName); - _logger.LogInformation($"Captures Clean Up: {file.FullName} deleted."); + double age = (DateTime.Now - file.CreationTime).TotalDays; + if (age > Config.DaysToKeepCaptures) + { + _logger.LogInformation($"Captures Clean Up: {file.FullName} is {age} day(s) old and will be deleted."); + System.IO.File.Delete(file.FullName); + _logger.LogInformation($"Captures Clean Up: {file.FullName} deleted."); + } } + _cleanupOldImagesRunning = false; } - _cleanupOldImagesRunning = false; + } + catch (Exception ex) + { + _logger.LogError(ex, "Captures Clean Up Failed"); } }); }