From 02fedead11f738c09e503c3bdc74e2dd98e21cc8 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 3 Jun 2013 22:02:49 -0400 Subject: re-factored some file system access --- .../ScheduledTasks/ScheduledTaskWorker.cs | 75 ++++++++++------------ 1 file changed, 34 insertions(+), 41 deletions(-) (limited to 'MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs') diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs index 94fb3ce86..ab6b3786d 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs @@ -123,9 +123,14 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks { try { - return JsonSerializer.DeserializeFromFile(GetHistoryFilePath()); + return JsonSerializer.DeserializeFromFile(GetHistoryFilePath(false)); } - catch (IOException) + catch (DirectoryNotFoundException) + { + // File doesn't exist. No biggie + return null; + } + catch (FileNotFoundException) { // File doesn't exist. No biggie return null; @@ -413,63 +418,46 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks } } - /// - /// The _scheduled tasks configuration directory - /// - private string _scheduledTasksConfigurationDirectory; /// /// Gets the scheduled tasks configuration directory. /// - /// The scheduled tasks configuration directory. - private string ScheduledTasksConfigurationDirectory + /// if set to true [create]. + /// System.String. + private string GetScheduledTasksConfigurationDirectory(bool create) { - get - { - if (_scheduledTasksConfigurationDirectory == null) - { - _scheduledTasksConfigurationDirectory = Path.Combine(ApplicationPaths.ConfigurationDirectoryPath, "ScheduledTasks"); + var path = Path.Combine(ApplicationPaths.ConfigurationDirectoryPath, "ScheduledTasks"); - if (!Directory.Exists(_scheduledTasksConfigurationDirectory)) - { - Directory.CreateDirectory(_scheduledTasksConfigurationDirectory); - } - } - return _scheduledTasksConfigurationDirectory; + if (create && !Directory.Exists(path)) + { + Directory.CreateDirectory(path); } + + return path; } - /// - /// The _scheduled tasks data directory - /// - private string _scheduledTasksDataDirectory; /// /// Gets the scheduled tasks data directory. /// - /// The scheduled tasks data directory. - private string ScheduledTasksDataDirectory + /// if set to true [create]. + /// System.String. + private string GetScheduledTasksDataDirectory(bool create) { - get - { - if (_scheduledTasksDataDirectory == null) - { - _scheduledTasksDataDirectory = Path.Combine(ApplicationPaths.DataPath, "ScheduledTasks"); + var path = Path.Combine(ApplicationPaths.DataPath, "ScheduledTasks"); - if (!Directory.Exists(_scheduledTasksDataDirectory)) - { - Directory.CreateDirectory(_scheduledTasksDataDirectory); - } - } - return _scheduledTasksDataDirectory; + if (create && !Directory.Exists(path)) + { + Directory.CreateDirectory(path); } + return path; } /// /// Gets the history file path. /// /// The history file path. - private string GetHistoryFilePath() + private string GetHistoryFilePath(bool createDirectory) { - return Path.Combine(ScheduledTasksDataDirectory, Id + ".js"); + return Path.Combine(GetScheduledTasksDataDirectory(createDirectory), Id + ".js"); } /// @@ -478,7 +466,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks /// System.String. private string GetConfigurationFilePath() { - return Path.Combine(ScheduledTasksConfigurationDirectory, Id + ".js"); + return Path.Combine(GetScheduledTasksConfigurationDirectory(false), Id + ".js"); } /// @@ -493,7 +481,12 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks .Select(ScheduledTaskHelpers.GetTrigger) .ToList(); } - catch (IOException) + catch (FileNotFoundException) + { + // File doesn't exist. No biggie. Return defaults. + return ScheduledTask.GetDefaultTriggers(); + } + catch (DirectoryNotFoundException) { // File doesn't exist. No biggie. Return defaults. return ScheduledTask.GetDefaultTriggers(); @@ -530,7 +523,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks Id = Id }; - JsonSerializer.SerializeToFile(result, GetHistoryFilePath()); + JsonSerializer.SerializeToFile(result, GetHistoryFilePath(true)); LastExecutionResult = result; -- cgit v1.2.3