From da20e8dcd2867df0a9a6ebc1081edb2db2eebdef Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 29 Oct 2016 16:02:21 -0400 Subject: continue with .net core targeting --- .../BaseApplicationHost.cs | 773 ----------------- .../BaseApplicationPaths.cs | 178 ---- .../Configuration/BaseConfigurationManager.cs | 328 -------- .../Configuration/ConfigurationHelper.cs | 60 -- .../Cryptography/CryptographyProvider.cs | 30 - .../Devices/DeviceId.cs | 109 --- .../HttpClientManager/HttpClientInfo.cs | 16 - .../HttpClientManager/HttpClientManager.cs | 936 --------------------- .../IO/IsoManager.cs | 75 -- .../IO/ManagedFileSystem.cs | 705 ---------------- .../IO/WindowsFileSystem.cs | 13 - .../MediaBrowser.Common.Implementations.csproj | 110 --- .../Networking/BaseNetworkManager.cs | 385 --------- .../Properties/AssemblyInfo.cs | 30 - .../ScheduledTasks/DailyTrigger.cs | 91 -- .../ScheduledTasks/IntervalTrigger.cs | 112 --- .../ScheduledTasks/ScheduledTaskWorker.cs | 781 ----------------- .../ScheduledTasks/StartupTrigger.cs | 67 -- .../ScheduledTasks/SystemEventTrigger.cs | 84 -- .../ScheduledTasks/TaskManager.cs | 361 -------- .../ScheduledTasks/Tasks/DeleteCacheFileTask.cs | 217 ----- .../ScheduledTasks/Tasks/DeleteLogFileTask.cs | 140 --- .../ScheduledTasks/Tasks/ReloadLoggerFileTask.cs | 113 --- .../ScheduledTasks/WeeklyTrigger.cs | 116 --- .../Serialization/XmlSerializer.cs | 130 --- .../Updates/GithubUpdater.cs | 269 ------ 26 files changed, 6229 deletions(-) delete mode 100644 MediaBrowser.Common.Implementations/BaseApplicationHost.cs delete mode 100644 MediaBrowser.Common.Implementations/BaseApplicationPaths.cs delete mode 100644 MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs delete mode 100644 MediaBrowser.Common.Implementations/Configuration/ConfigurationHelper.cs delete mode 100644 MediaBrowser.Common.Implementations/Cryptography/CryptographyProvider.cs delete mode 100644 MediaBrowser.Common.Implementations/Devices/DeviceId.cs delete mode 100644 MediaBrowser.Common.Implementations/HttpClientManager/HttpClientInfo.cs delete mode 100644 MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs delete mode 100644 MediaBrowser.Common.Implementations/IO/IsoManager.cs delete mode 100644 MediaBrowser.Common.Implementations/IO/ManagedFileSystem.cs delete mode 100644 MediaBrowser.Common.Implementations/IO/WindowsFileSystem.cs delete mode 100644 MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj delete mode 100644 MediaBrowser.Common.Implementations/Networking/BaseNetworkManager.cs delete mode 100644 MediaBrowser.Common.Implementations/Properties/AssemblyInfo.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/DailyTrigger.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/IntervalTrigger.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/StartupTrigger.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/SystemEventTrigger.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/ReloadLoggerFileTask.cs delete mode 100644 MediaBrowser.Common.Implementations/ScheduledTasks/WeeklyTrigger.cs delete mode 100644 MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs delete mode 100644 MediaBrowser.Common.Implementations/Updates/GithubUpdater.cs (limited to 'MediaBrowser.Common.Implementations') diff --git a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs deleted file mode 100644 index ec96818aa..000000000 --- a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs +++ /dev/null @@ -1,773 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Events; -using MediaBrowser.Common.Implementations.Devices; -using MediaBrowser.Common.Implementations.IO; -using MediaBrowser.Common.Implementations.ScheduledTasks; -using MediaBrowser.Common.Implementations.Serialization; -using MediaBrowser.Common.Implementations.Updates; -using MediaBrowser.Common.Net; -using MediaBrowser.Common.Plugins; -using MediaBrowser.Common.Progress; -using MediaBrowser.Common.ScheduledTasks; -using MediaBrowser.Common.Security; -using MediaBrowser.Common.Updates; -using MediaBrowser.Model.Events; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Serialization; -using MediaBrowser.Model.Updates; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Reflection; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Common.Extensions; -using MediaBrowser.Common.Implementations.Cryptography; -using MediaBrowser.Common.IO; -using MediaBrowser.Model.Cryptography; -using MediaBrowser.Model.System; -using MediaBrowser.Model.Tasks; - -namespace MediaBrowser.Common.Implementations -{ - /// - /// Class BaseApplicationHost - /// - /// The type of the T application paths type. - public abstract class BaseApplicationHost : IApplicationHost - where TApplicationPathsType : class, IApplicationPaths - { - /// - /// Occurs when [has pending restart changed]. - /// - public event EventHandler HasPendingRestartChanged; - - /// - /// Occurs when [application updated]. - /// - public event EventHandler> ApplicationUpdated; - - /// - /// Gets or sets a value indicating whether this instance has changes that require the entire application to restart. - /// - /// true if this instance has pending application restart; otherwise, false. - public bool HasPendingRestart { get; private set; } - - /// - /// Gets or sets the logger. - /// - /// The logger. - protected ILogger Logger { get; private set; } - - /// - /// Gets or sets the plugins. - /// - /// The plugins. - public IPlugin[] Plugins { get; protected set; } - - /// - /// Gets or sets the log manager. - /// - /// The log manager. - public ILogManager LogManager { get; protected set; } - - /// - /// Gets the application paths. - /// - /// The application paths. - protected TApplicationPathsType ApplicationPaths { get; private set; } - - /// - /// The json serializer - /// - public IJsonSerializer JsonSerializer { get; private set; } - - /// - /// The _XML serializer - /// - protected readonly IXmlSerializer XmlSerializer; - - /// - /// Gets assemblies that failed to load - /// - /// The failed assemblies. - public List FailedAssemblies { get; protected set; } - - /// - /// Gets all concrete types. - /// - /// All concrete types. - public Type[] AllConcreteTypes { get; protected set; } - - /// - /// The disposable parts - /// - protected readonly List DisposableParts = new List(); - - /// - /// Gets a value indicating whether this instance is first run. - /// - /// true if this instance is first run; otherwise, false. - public bool IsFirstRun { get; private set; } - - /// - /// Gets the kernel. - /// - /// The kernel. - protected ITaskManager TaskManager { get; private set; } - /// - /// Gets the HTTP client. - /// - /// The HTTP client. - public IHttpClient HttpClient { get; private set; } - /// - /// Gets the network manager. - /// - /// The network manager. - protected INetworkManager NetworkManager { get; private set; } - - /// - /// Gets the configuration manager. - /// - /// The configuration manager. - protected IConfigurationManager ConfigurationManager { get; private set; } - - protected IFileSystem FileSystemManager { get; private set; } - - protected IIsoManager IsoManager { get; private set; } - - protected ISystemEvents SystemEvents { get; private set; } - - /// - /// Gets the name. - /// - /// The name. - public abstract string Name { get; } - - /// - /// Gets a value indicating whether this instance is running as service. - /// - /// true if this instance is running as service; otherwise, false. - public abstract bool IsRunningAsService { get; } - - protected ICryptographyProvider CryptographyProvider = new CryptographyProvider(); - - private DeviceId _deviceId; - public string SystemId - { - get - { - if (_deviceId == null) - { - _deviceId = new DeviceId(ApplicationPaths, LogManager.GetLogger("SystemId"), FileSystemManager); - } - - return _deviceId.Value; - } - } - - public virtual string OperatingSystemDisplayName - { - get { return Environment.OSVersion.VersionString; } - } - - public IMemoryStreamProvider MemoryStreamProvider { get; set; } - - /// - /// Initializes a new instance of the class. - /// - protected BaseApplicationHost(TApplicationPathsType applicationPaths, - ILogManager logManager, - IFileSystem fileSystem) - { - // hack alert, until common can target .net core - BaseExtensions.CryptographyProvider = CryptographyProvider; - - XmlSerializer = new XmlSerializer(fileSystem, logManager.GetLogger("XmlSerializer")); - FailedAssemblies = new List(); - - ApplicationPaths = applicationPaths; - LogManager = logManager; - FileSystemManager = fileSystem; - - ConfigurationManager = GetConfigurationManager(); - - // Initialize this early in case the -v command line option is used - Logger = LogManager.GetLogger("App"); - } - - /// - /// Inits this instance. - /// - /// Task. - public virtual async Task Init(IProgress progress) - { - progress.Report(1); - - JsonSerializer = CreateJsonSerializer(); - - MemoryStreamProvider = CreateMemoryStreamProvider(); - SystemEvents = CreateSystemEvents(); - - OnLoggerLoaded(true); - LogManager.LoggerLoaded += (s, e) => OnLoggerLoaded(false); - - IsFirstRun = !ConfigurationManager.CommonConfiguration.IsStartupWizardCompleted; - progress.Report(2); - - LogManager.LogSeverity = ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging - ? LogSeverity.Debug - : LogSeverity.Info; - - progress.Report(3); - - DiscoverTypes(); - progress.Report(14); - - SetHttpLimit(); - progress.Report(15); - - var innerProgress = new ActionableProgress(); - innerProgress.RegisterAction(p => progress.Report(.8 * p + 15)); - - await RegisterResources(innerProgress).ConfigureAwait(false); - - FindParts(); - progress.Report(95); - - await InstallIsoMounters(CancellationToken.None).ConfigureAwait(false); - - progress.Report(100); - } - - protected abstract IMemoryStreamProvider CreateMemoryStreamProvider(); - protected abstract ISystemEvents CreateSystemEvents(); - - protected virtual void OnLoggerLoaded(bool isFirstLoad) - { - Logger.Info("Application version: {0}", ApplicationVersion); - - if (!isFirstLoad) - { - LogEnvironmentInfo(Logger, ApplicationPaths, false); - } - - // Put the app config in the log for troubleshooting purposes - Logger.LogMultiline("Application configuration:", LogSeverity.Info, new StringBuilder(JsonSerializer.SerializeToString(ConfigurationManager.CommonConfiguration))); - - if (Plugins != null) - { - var pluginBuilder = new StringBuilder(); - - foreach (var plugin in Plugins) - { - pluginBuilder.AppendLine(string.Format("{0} {1}", plugin.Name, plugin.Version)); - } - - Logger.LogMultiline("Plugins:", LogSeverity.Info, pluginBuilder); - } - } - - public static void LogEnvironmentInfo(ILogger logger, IApplicationPaths appPaths, bool isStartup) - { - logger.LogMultiline("Emby", LogSeverity.Info, GetBaseExceptionMessage(appPaths)); - } - - protected static StringBuilder GetBaseExceptionMessage(IApplicationPaths appPaths) - { - var builder = new StringBuilder(); - - builder.AppendLine(string.Format("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()))); - - builder.AppendLine(string.Format("Operating system: {0}", Environment.OSVersion)); - builder.AppendLine(string.Format("Processor count: {0}", Environment.ProcessorCount)); - builder.AppendLine(string.Format("64-Bit OS: {0}", Environment.Is64BitOperatingSystem)); - builder.AppendLine(string.Format("64-Bit Process: {0}", Environment.Is64BitProcess)); - builder.AppendLine(string.Format("Program data path: {0}", appPaths.ProgramDataPath)); - - Type type = Type.GetType("Mono.Runtime"); - if (type != null) - { - MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); - if (displayName != null) - { - builder.AppendLine("Mono: " + displayName.Invoke(null, null)); - } - } - - builder.AppendLine(string.Format("Application Path: {0}", appPaths.ApplicationPath)); - - return builder; - } - - protected abstract IJsonSerializer CreateJsonSerializer(); - - private void SetHttpLimit() - { - try - { - // Increase the max http request limit - ServicePointManager.DefaultConnectionLimit = Math.Max(96, ServicePointManager.DefaultConnectionLimit); - } - catch (Exception ex) - { - Logger.ErrorException("Error setting http limit", ex); - } - } - - /// - /// Installs the iso mounters. - /// - /// The cancellation token. - /// Task. - private async Task InstallIsoMounters(CancellationToken cancellationToken) - { - var list = new List(); - - foreach (var isoMounter in GetExports()) - { - try - { - if (isoMounter.RequiresInstallation && !isoMounter.IsInstalled) - { - Logger.Info("Installing {0}", isoMounter.Name); - - await isoMounter.Install(cancellationToken).ConfigureAwait(false); - } - - list.Add(isoMounter); - } - catch (Exception ex) - { - Logger.ErrorException("{0} failed to load.", ex, isoMounter.Name); - } - } - - IsoManager.AddParts(list); - } - - /// - /// Runs the startup tasks. - /// - /// Task. - public virtual Task RunStartupTasks() - { - Resolve().AddTasks(GetExports(false)); - - ConfigureAutorun(); - - ConfigurationManager.ConfigurationUpdated += OnConfigurationUpdated; - - return Task.FromResult(true); - } - - /// - /// Configures the autorun. - /// - private void ConfigureAutorun() - { - try - { - ConfigureAutoRunAtStartup(ConfigurationManager.CommonConfiguration.RunAtStartup); - } - catch (Exception ex) - { - Logger.ErrorException("Error configuring autorun", ex); - } - } - - /// - /// Gets the composable part assemblies. - /// - /// IEnumerable{Assembly}. - protected abstract IEnumerable GetComposablePartAssemblies(); - - /// - /// Gets the configuration manager. - /// - /// IConfigurationManager. - protected abstract IConfigurationManager GetConfigurationManager(); - - /// - /// Finds the parts. - /// - protected virtual void FindParts() - { - ConfigurationManager.AddParts(GetExports()); - Plugins = GetExports().Select(LoadPlugin).Where(i => i != null).ToArray(); - } - - private IPlugin LoadPlugin(IPlugin plugin) - { - try - { - var assemblyPlugin = plugin as IPluginAssembly; - - if (assemblyPlugin != null) - { - var assembly = plugin.GetType().Assembly; - var assemblyName = assembly.GetName(); - - var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]; - var assemblyId = new Guid(attribute.Value); - - var assemblyFileName = assemblyName.Name + ".dll"; - var assemblyFilePath = Path.Combine(ApplicationPaths.PluginsPath, assemblyFileName); - - assemblyPlugin.SetAttributes(assemblyFilePath, assemblyFileName, assemblyName.Version, assemblyId); - } - - var isFirstRun = !File.Exists(plugin.ConfigurationFilePath); - - plugin.SetStartupInfo(isFirstRun, File.GetLastWriteTimeUtc, s => Directory.CreateDirectory(s)); - } - catch (Exception ex) - { - Logger.ErrorException("Error loading plugin {0}", ex, plugin.GetType().FullName); - return null; - } - - return plugin; - } - - /// - /// Discovers the types. - /// - protected void DiscoverTypes() - { - FailedAssemblies.Clear(); - - var assemblies = GetComposablePartAssemblies().ToList(); - - foreach (var assembly in assemblies) - { - Logger.Info("Loading {0}", assembly.FullName); - } - - AllConcreteTypes = assemblies - .SelectMany(GetTypes) - .Where(t => t.IsClass && !t.IsAbstract && !t.IsInterface && !t.IsGenericType) - .ToArray(); - } - - /// - /// Registers resources that classes will depend on - /// - /// Task. - protected virtual Task RegisterResources(IProgress progress) - { - RegisterSingleInstance(ConfigurationManager); - RegisterSingleInstance(this); - - RegisterSingleInstance(ApplicationPaths); - - TaskManager = new TaskManager(ApplicationPaths, JsonSerializer, LogManager.GetLogger("TaskManager"), FileSystemManager, SystemEvents); - - RegisterSingleInstance(JsonSerializer); - RegisterSingleInstance(XmlSerializer); - RegisterSingleInstance(MemoryStreamProvider); - RegisterSingleInstance(SystemEvents); - - RegisterSingleInstance(LogManager); - RegisterSingleInstance(Logger); - - RegisterSingleInstance(TaskManager); - - RegisterSingleInstance(FileSystemManager); - - HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, LogManager.GetLogger("HttpClient"), FileSystemManager, MemoryStreamProvider); - RegisterSingleInstance(HttpClient); - - NetworkManager = CreateNetworkManager(LogManager.GetLogger("NetworkManager")); - RegisterSingleInstance(NetworkManager); - - IsoManager = new IsoManager(); - RegisterSingleInstance(IsoManager); - - return Task.FromResult(true); - } - - /// - /// Gets a list of types within an assembly - /// This will handle situations that would normally throw an exception - such as a type within the assembly that depends on some other non-existant reference - /// - /// The assembly. - /// IEnumerable{Type}. - /// assembly - protected IEnumerable GetTypes(Assembly assembly) - { - if (assembly == null) - { - throw new ArgumentNullException("assembly"); - } - - try - { - return assembly.GetTypes(); - } - catch (ReflectionTypeLoadException ex) - { - if (ex.LoaderExceptions != null) - { - foreach (var loaderException in ex.LoaderExceptions) - { - Logger.Error("LoaderException: " + loaderException.Message); - } - } - - // If it fails we can still get a list of the Types it was able to resolve - return ex.Types.Where(t => t != null); - } - } - - protected abstract INetworkManager CreateNetworkManager(ILogger logger); - - /// - /// Creates an instance of type and resolves all constructor dependancies - /// - /// The type. - /// System.Object. - public abstract object CreateInstance(Type type); - - /// - /// Creates the instance safe. - /// - /// The type. - /// System.Object. - protected abstract object CreateInstanceSafe(Type type); - - /// - /// Registers the specified obj. - /// - /// - /// The obj. - /// if set to true [manage lifetime]. - protected abstract void RegisterSingleInstance(T obj, bool manageLifetime = true) - where T : class; - - /// - /// Registers the single instance. - /// - /// - /// The func. - protected abstract void RegisterSingleInstance(Func func) - where T : class; - - /// - /// Resolves this instance. - /// - /// - /// ``0. - public abstract T Resolve(); - - /// - /// Resolves this instance. - /// - /// - /// ``0. - public abstract T TryResolve(); - - /// - /// Loads the assembly. - /// - /// The file. - /// Assembly. - protected Assembly LoadAssembly(string file) - { - try - { - return Assembly.Load(File.ReadAllBytes(file)); - } - catch (Exception ex) - { - FailedAssemblies.Add(file); - Logger.ErrorException("Error loading assembly {0}", ex, file); - return null; - } - } - - /// - /// Gets the export types. - /// - /// - /// IEnumerable{Type}. - public IEnumerable GetExportTypes() - { - var currentType = typeof(T); - - return AllConcreteTypes.AsParallel().Where(currentType.IsAssignableFrom); - } - - /// - /// Gets the exports. - /// - /// - /// if set to true [manage liftime]. - /// IEnumerable{``0}. - public IEnumerable GetExports(bool manageLiftime = true) - { - var parts = GetExportTypes() - .Select(CreateInstanceSafe) - .Where(i => i != null) - .Cast() - .ToList(); - - if (manageLiftime) - { - lock (DisposableParts) - { - DisposableParts.AddRange(parts.OfType()); - } - } - - return parts; - } - - /// - /// Gets the application version. - /// - /// The application version. - public abstract Version ApplicationVersion { get; } - - /// - /// Handles the ConfigurationUpdated event of the ConfigurationManager control. - /// - /// The source of the event. - /// The instance containing the event data. - /// - protected virtual void OnConfigurationUpdated(object sender, EventArgs e) - { - ConfigureAutorun(); - } - - protected abstract void ConfigureAutoRunAtStartup(bool autorun); - - /// - /// Removes the plugin. - /// - /// The plugin. - public void RemovePlugin(IPlugin plugin) - { - var list = Plugins.ToList(); - list.Remove(plugin); - Plugins = list.ToArray(); - } - - /// - /// Gets a value indicating whether this instance can self restart. - /// - /// true if this instance can self restart; otherwise, false. - public abstract bool CanSelfRestart { get; } - - /// - /// Notifies that the kernel that a change has been made that requires a restart - /// - public void NotifyPendingRestart() - { - var changed = !HasPendingRestart; - - HasPendingRestart = true; - - if (changed) - { - EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger); - } - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - Dispose(true); - } - - /// - /// Releases unmanaged and - optionally - managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool dispose) - { - if (dispose) - { - var type = GetType(); - - Logger.Info("Disposing " + type.Name); - - var parts = DisposableParts.Distinct().Where(i => i.GetType() != type).ToList(); - DisposableParts.Clear(); - - foreach (var part in parts) - { - Logger.Info("Disposing " + part.GetType().Name); - - try - { - part.Dispose(); - } - catch (Exception ex) - { - Logger.ErrorException("Error disposing {0}", ex, part.GetType().Name); - } - } - } - } - - /// - /// Restarts this instance. - /// - public abstract Task Restart(); - - /// - /// Gets or sets a value indicating whether this instance can self update. - /// - /// true if this instance can self update; otherwise, false. - public abstract bool CanSelfUpdate { get; } - - /// - /// Checks for update. - /// - /// The cancellation token. - /// The progress. - /// Task{CheckForUpdateResult}. - public abstract Task CheckForApplicationUpdate(CancellationToken cancellationToken, - IProgress progress); - - /// - /// Updates the application. - /// - /// The package that contains the update - /// The cancellation token. - /// The progress. - /// Task. - public abstract Task UpdateApplication(PackageVersionInfo package, CancellationToken cancellationToken, - IProgress progress); - - /// - /// Shuts down. - /// - public abstract Task Shutdown(); - - /// - /// Called when [application updated]. - /// - /// The package. - protected void OnApplicationUpdated(PackageVersionInfo package) - { - Logger.Info("Application has been updated to version {0}", package.versionStr); - - EventHelper.FireEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs - { - Argument = package - - }, Logger); - - NotifyPendingRestart(); - } - } -} diff --git a/MediaBrowser.Common.Implementations/BaseApplicationPaths.cs b/MediaBrowser.Common.Implementations/BaseApplicationPaths.cs deleted file mode 100644 index 9ba2effd3..000000000 --- a/MediaBrowser.Common.Implementations/BaseApplicationPaths.cs +++ /dev/null @@ -1,178 +0,0 @@ -using MediaBrowser.Common.Configuration; -using System.IO; - -namespace MediaBrowser.Common.Implementations -{ - /// - /// Provides a base class to hold common application paths used by both the Ui and Server. - /// This can be subclassed to add application-specific paths. - /// - public abstract class BaseApplicationPaths : IApplicationPaths - { - /// - /// Initializes a new instance of the class. - /// - protected BaseApplicationPaths(string programDataPath, string applicationPath) - { - ProgramDataPath = programDataPath; - ApplicationPath = applicationPath; - } - - public string ApplicationPath { get; private set; } - public string ProgramDataPath { get; private set; } - - /// - /// Gets the path to the system folder - /// - public string ProgramSystemPath - { - get { return Path.GetDirectoryName(ApplicationPath); } - } - - /// - /// The _data directory - /// - private string _dataDirectory; - /// - /// Gets the folder path to the data directory - /// - /// The data directory. - public string DataPath - { - get - { - if (_dataDirectory == null) - { - _dataDirectory = Path.Combine(ProgramDataPath, "data"); - - Directory.CreateDirectory(_dataDirectory); - } - - return _dataDirectory; - } - } - - /// - /// Gets the image cache path. - /// - /// The image cache path. - public string ImageCachePath - { - get - { - return Path.Combine(CachePath, "images"); - } - } - - /// - /// Gets the path to the plugin directory - /// - /// The plugins path. - public string PluginsPath - { - get - { - return Path.Combine(ProgramDataPath, "plugins"); - } - } - - /// - /// Gets the path to the plugin configurations directory - /// - /// The plugin configurations path. - public string PluginConfigurationsPath - { - get - { - return Path.Combine(PluginsPath, "configurations"); - } - } - - /// - /// Gets the path to where temporary update files will be stored - /// - /// The plugin configurations path. - public string TempUpdatePath - { - get - { - return Path.Combine(ProgramDataPath, "updates"); - } - } - - /// - /// Gets the path to the log directory - /// - /// The log directory path. - public string LogDirectoryPath - { - get - { - return Path.Combine(ProgramDataPath, "logs"); - } - } - - /// - /// Gets the path to the application configuration root directory - /// - /// The configuration directory path. - public string ConfigurationDirectoryPath - { - get - { - return Path.Combine(ProgramDataPath, "config"); - } - } - - /// - /// Gets the path to the system configuration file - /// - /// The system configuration file path. - public string SystemConfigurationFilePath - { - get - { - return Path.Combine(ConfigurationDirectoryPath, "system.xml"); - } - } - - /// - /// The _cache directory - /// - private string _cachePath; - /// - /// Gets the folder path to the cache directory - /// - /// The cache directory. - public string CachePath - { - get - { - if (string.IsNullOrEmpty(_cachePath)) - { - _cachePath = Path.Combine(ProgramDataPath, "cache"); - - Directory.CreateDirectory(_cachePath); - } - - return _cachePath; - } - set - { - _cachePath = value; - } - } - - /// - /// Gets the folder path to the temp directory within the cache folder - /// - /// The temp directory. - public string TempDirectory - { - get - { - return Path.Combine(CachePath, "temp"); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs deleted file mode 100644 index 943e6c5f8..000000000 --- a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs +++ /dev/null @@ -1,328 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Events; -using MediaBrowser.Model.Configuration; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Serialization; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using MediaBrowser.Model.IO; -using MediaBrowser.Common.Extensions; - -namespace MediaBrowser.Common.Implementations.Configuration -{ - /// - /// Class BaseConfigurationManager - /// - public abstract class BaseConfigurationManager : IConfigurationManager - { - /// - /// Gets the type of the configuration. - /// - /// The type of the configuration. - protected abstract Type ConfigurationType { get; } - - /// - /// Occurs when [configuration updated]. - /// - public event EventHandler ConfigurationUpdated; - - /// - /// Occurs when [configuration updating]. - /// - public event EventHandler NamedConfigurationUpdating; - - /// - /// Occurs when [named configuration updated]. - /// - public event EventHandler NamedConfigurationUpdated; - - /// - /// Gets the logger. - /// - /// The logger. - protected ILogger Logger { get; private set; } - /// - /// Gets the XML serializer. - /// - /// The XML serializer. - protected IXmlSerializer XmlSerializer { get; private set; } - - /// - /// Gets or sets the application paths. - /// - /// The application paths. - public IApplicationPaths CommonApplicationPaths { get; private set; } - public readonly IFileSystem FileSystem; - - /// - /// The _configuration loaded - /// - private bool _configurationLoaded; - /// - /// The _configuration sync lock - /// - private object _configurationSyncLock = new object(); - /// - /// The _configuration - /// - private BaseApplicationConfiguration _configuration; - /// - /// Gets the system configuration - /// - /// The configuration. - public BaseApplicationConfiguration CommonConfiguration - { - get - { - // Lazy load - LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => (BaseApplicationConfiguration)ConfigurationHelper.GetXmlConfiguration(ConfigurationType, CommonApplicationPaths.SystemConfigurationFilePath, XmlSerializer, FileSystem)); - return _configuration; - } - protected set - { - _configuration = value; - - _configurationLoaded = value != null; - } - } - - private ConfigurationStore[] _configurationStores = { }; - private IConfigurationFactory[] _configurationFactories = { }; - - /// - /// Initializes a new instance of the class. - /// - /// The application paths. - /// The log manager. - /// The XML serializer. - protected BaseConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer, IFileSystem fileSystem) - { - CommonApplicationPaths = applicationPaths; - XmlSerializer = xmlSerializer; - FileSystem = fileSystem; - Logger = logManager.GetLogger(GetType().Name); - - UpdateCachePath(); - } - - public virtual void AddParts(IEnumerable factories) - { - _configurationFactories = factories.ToArray(); - - _configurationStores = _configurationFactories - .SelectMany(i => i.GetConfigurations()) - .ToArray(); - } - - /// - /// Saves the configuration. - /// - public void SaveConfiguration() - { - Logger.Info("Saving system configuration"); - var path = CommonApplicationPaths.SystemConfigurationFilePath; - - FileSystem.CreateDirectory(Path.GetDirectoryName(path)); - - lock (_configurationSyncLock) - { - XmlSerializer.SerializeToFile(CommonConfiguration, path); - } - - OnConfigurationUpdated(); - } - - /// - /// Called when [configuration updated]. - /// - protected virtual void OnConfigurationUpdated() - { - UpdateCachePath(); - - EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger); - } - - /// - /// Replaces the configuration. - /// - /// The new configuration. - /// newConfiguration - public virtual void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration) - { - if (newConfiguration == null) - { - throw new ArgumentNullException("newConfiguration"); - } - - ValidateCachePath(newConfiguration); - - CommonConfiguration = newConfiguration; - SaveConfiguration(); - } - - /// - /// Updates the items by name path. - /// - private void UpdateCachePath() - { - string cachePath; - - if (string.IsNullOrWhiteSpace(CommonConfiguration.CachePath)) - { - cachePath = null; - } - else - { - cachePath = Path.Combine(CommonConfiguration.CachePath, "cache"); - } - - ((BaseApplicationPaths)CommonApplicationPaths).CachePath = cachePath; - } - - /// - /// Replaces the cache path. - /// - /// The new configuration. - /// - private void ValidateCachePath(BaseApplicationConfiguration newConfig) - { - var newPath = newConfig.CachePath; - - if (!string.IsNullOrWhiteSpace(newPath) - && !string.Equals(CommonConfiguration.CachePath ?? string.Empty, newPath)) - { - // Validate - if (!FileSystem.DirectoryExists(newPath)) - { - throw new FileNotFoundException(string.Format("{0} does not exist.", newPath)); - } - - EnsureWriteAccess(newPath); - } - } - - protected void EnsureWriteAccess(string path) - { - var file = Path.Combine(path, Guid.NewGuid().ToString()); - - FileSystem.WriteAllText(file, string.Empty); - FileSystem.DeleteFile(file); - } - - private readonly ConcurrentDictionary _configurations = new ConcurrentDictionary(); - - private string GetConfigurationFile(string key) - { - return Path.Combine(CommonApplicationPaths.ConfigurationDirectoryPath, key.ToLower() + ".xml"); - } - - public object GetConfiguration(string key) - { - return _configurations.GetOrAdd(key, k => - { - var file = GetConfigurationFile(key); - - var configurationInfo = _configurationStores - .FirstOrDefault(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)); - - if (configurationInfo == null) - { - throw new ResourceNotFoundException("Configuration with key " + key + " not found."); - } - - var configurationType = configurationInfo.ConfigurationType; - - lock (_configurationSyncLock) - { - return LoadConfiguration(file, configurationType); - } - }); - } - - private object LoadConfiguration(string path, Type configurationType) - { - try - { - return XmlSerializer.DeserializeFromFile(configurationType, path); - } - catch (FileNotFoundException) - { - return Activator.CreateInstance(configurationType); - } - catch (IOException) - { - return Activator.CreateInstance(configurationType); - } - catch (Exception ex) - { - Logger.ErrorException("Error loading configuration file: {0}", ex, path); - - return Activator.CreateInstance(configurationType); - } - } - - public void SaveConfiguration(string key, object configuration) - { - var configurationStore = GetConfigurationStore(key); - var configurationType = configurationStore.ConfigurationType; - - if (configuration.GetType() != configurationType) - { - throw new ArgumentException("Expected configuration type is " + configurationType.Name); - } - - var validatingStore = configurationStore as IValidatingConfiguration; - if (validatingStore != null) - { - var currentConfiguration = GetConfiguration(key); - - validatingStore.Validate(currentConfiguration, configuration); - } - - EventHelper.FireEventIfNotNull(NamedConfigurationUpdating, this, new ConfigurationUpdateEventArgs - { - Key = key, - NewConfiguration = configuration - - }, Logger); - - _configurations.AddOrUpdate(key, configuration, (k, v) => configuration); - - var path = GetConfigurationFile(key); - FileSystem.CreateDirectory(Path.GetDirectoryName(path)); - - lock (_configurationSyncLock) - { - XmlSerializer.SerializeToFile(configuration, path); - } - - OnNamedConfigurationUpdated(key, configuration); - } - - protected virtual void OnNamedConfigurationUpdated(string key, object configuration) - { - EventHelper.FireEventIfNotNull(NamedConfigurationUpdated, this, new ConfigurationUpdateEventArgs - { - Key = key, - NewConfiguration = configuration - - }, Logger); - } - - public Type GetConfigurationType(string key) - { - return GetConfigurationStore(key) - .ConfigurationType; - } - - private ConfigurationStore GetConfigurationStore(string key) - { - return _configurationStores - .First(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)); - } - } -} diff --git a/MediaBrowser.Common.Implementations/Configuration/ConfigurationHelper.cs b/MediaBrowser.Common.Implementations/Configuration/ConfigurationHelper.cs deleted file mode 100644 index 0c1683f93..000000000 --- a/MediaBrowser.Common.Implementations/Configuration/ConfigurationHelper.cs +++ /dev/null @@ -1,60 +0,0 @@ -using MediaBrowser.Model.Serialization; -using System; -using System.IO; -using System.Linq; -using MediaBrowser.Model.IO; - -namespace MediaBrowser.Common.Implementations.Configuration -{ - /// - /// Class ConfigurationHelper - /// - public static class ConfigurationHelper - { - /// - /// Reads an xml configuration file from the file system - /// It will immediately re-serialize and save if new serialization data is available due to property changes - /// - /// The type. - /// The path. - /// The XML serializer. - /// System.Object. - public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer, IFileSystem fileSystem) - { - object configuration; - - byte[] buffer = null; - - // Use try/catch to avoid the extra file system lookup using File.Exists - try - { - buffer = fileSystem.ReadAllBytes(path); - - configuration = xmlSerializer.DeserializeFromBytes(type, buffer); - } - catch (Exception) - { - configuration = Activator.CreateInstance(type); - } - - using (var stream = new MemoryStream()) - { - xmlSerializer.SerializeToStream(configuration, stream); - - // Take the object we just got and serialize it back to bytes - var newBytes = stream.ToArray(); - - // If the file didn't exist before, or if something has changed, re-save - if (buffer == null || !buffer.SequenceEqual(newBytes)) - { - fileSystem.CreateDirectory(Path.GetDirectoryName(path)); - - // Save it after load in case we got new items - fileSystem.WriteAllBytes(path, newBytes); - } - - return configuration; - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/Cryptography/CryptographyProvider.cs b/MediaBrowser.Common.Implementations/Cryptography/CryptographyProvider.cs deleted file mode 100644 index 67dbd76d2..000000000 --- a/MediaBrowser.Common.Implementations/Cryptography/CryptographyProvider.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.IO; -using System.Security.Cryptography; -using System.Text; -using MediaBrowser.Model.Cryptography; - -namespace MediaBrowser.Common.Implementations.Cryptography -{ - public class CryptographyProvider : ICryptographyProvider - { - public Guid GetMD5(string str) - { - return new Guid(GetMD5Bytes(str)); - } - public byte[] GetMD5Bytes(string str) - { - using (var provider = MD5.Create()) - { - return provider.ComputeHash(Encoding.Unicode.GetBytes(str)); - } - } - public byte[] GetMD5Bytes(Stream str) - { - using (var provider = MD5.Create()) - { - return provider.ComputeHash(str); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/Devices/DeviceId.cs b/MediaBrowser.Common.Implementations/Devices/DeviceId.cs deleted file mode 100644 index 40bbe8713..000000000 --- a/MediaBrowser.Common.Implementations/Devices/DeviceId.cs +++ /dev/null @@ -1,109 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Model.Logging; -using System; -using System.IO; -using System.Text; -using MediaBrowser.Model.IO; - -namespace MediaBrowser.Common.Implementations.Devices -{ - public class DeviceId - { - private readonly IApplicationPaths _appPaths; - private readonly ILogger _logger; - private readonly IFileSystem _fileSystem; - - private readonly object _syncLock = new object(); - - private string CachePath - { - get { return Path.Combine(_appPaths.DataPath, "device.txt"); } - } - - private string GetCachedId() - { - try - { - lock (_syncLock) - { - var value = File.ReadAllText(CachePath, Encoding.UTF8); - - Guid guid; - if (Guid.TryParse(value, out guid)) - { - return value; - } - - _logger.Error("Invalid value found in device id file"); - } - } - catch (DirectoryNotFoundException) - { - } - catch (FileNotFoundException) - { - } - catch (Exception ex) - { - _logger.ErrorException("Error reading file", ex); - } - - return null; - } - - private void SaveId(string id) - { - try - { - var path = CachePath; - - _fileSystem.CreateDirectory(Path.GetDirectoryName(path)); - - lock (_syncLock) - { - _fileSystem.WriteAllText(path, id, Encoding.UTF8); - } - } - catch (Exception ex) - { - _logger.ErrorException("Error writing to file", ex); - } - } - - private string GetNewId() - { - return Guid.NewGuid().ToString("N"); - } - - private string GetDeviceId() - { - var id = GetCachedId(); - - if (string.IsNullOrWhiteSpace(id)) - { - id = GetNewId(); - SaveId(id); - } - - return id; - } - - private string _id; - - public DeviceId(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem) - { - if (fileSystem == null) { - throw new ArgumentNullException ("fileSystem"); - } - - _appPaths = appPaths; - _logger = logger; - _fileSystem = fileSystem; - } - - public string Value - { - get { return _id ?? (_id = GetDeviceId()); } - } - } -} diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientInfo.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientInfo.cs deleted file mode 100644 index 8af6ef6c6..000000000 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientInfo.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace MediaBrowser.Common.Implementations.HttpClientManager -{ - /// - /// Class HttpClientInfo - /// - public class HttpClientInfo - { - /// - /// Gets or sets the last timeout. - /// - /// The last timeout. - public DateTime LastTimeout { get; set; } - } -} diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs deleted file mode 100644 index b18178ead..000000000 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ /dev/null @@ -1,936 +0,0 @@ -using System.Net.Sockets; -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Extensions; -using MediaBrowser.Common.IO; -using MediaBrowser.Common.Net; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Net; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Cache; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.IO; - -namespace MediaBrowser.Common.Implementations.HttpClientManager -{ - /// - /// Class HttpClientManager - /// - public class HttpClientManager : IHttpClient - { - /// - /// When one request to a host times out, we'll ban all other requests for this period of time, to prevent scans from stalling - /// - private const int TimeoutSeconds = 30; - - /// - /// The _logger - /// - private readonly ILogger _logger; - - /// - /// The _app paths - /// - private readonly IApplicationPaths _appPaths; - - private readonly IFileSystem _fileSystem; - private readonly IMemoryStreamProvider _memoryStreamProvider; - - /// - /// Initializes a new instance of the class. - /// - /// The app paths. - /// The logger. - /// The file system. - /// appPaths - /// or - /// logger - public HttpClientManager(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem, IMemoryStreamProvider memoryStreamProvider) - { - if (appPaths == null) - { - throw new ArgumentNullException("appPaths"); - } - if (logger == null) - { - throw new ArgumentNullException("logger"); - } - - _logger = logger; - _fileSystem = fileSystem; - _memoryStreamProvider = memoryStreamProvider; - _appPaths = appPaths; - - // http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c - ServicePointManager.Expect100Continue = false; - - // Trakt requests sometimes fail without this - ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls; - } - - /// - /// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests. - /// DON'T dispose it after use. - /// - /// The HTTP clients. - private readonly ConcurrentDictionary _httpClients = new ConcurrentDictionary(); - - /// - /// Gets - /// - /// The host. - /// if set to true [enable HTTP compression]. - /// HttpClient. - /// host - private HttpClientInfo GetHttpClient(string host, bool enableHttpCompression) - { - if (string.IsNullOrEmpty(host)) - { - throw new ArgumentNullException("host"); - } - - HttpClientInfo client; - - var key = host + enableHttpCompression; - - if (!_httpClients.TryGetValue(key, out client)) - { - client = new HttpClientInfo(); - - _httpClients.TryAdd(key, client); - } - - return client; - } - - private WebRequest CreateWebRequest(string url) - { - try - { - return WebRequest.Create(url); - } - catch (NotSupportedException) - { - //Webrequest creation does fail on MONO randomly when using WebRequest.Create - //the issue occurs in the GetCreator method here: http://www.oschina.net/code/explore/mono-2.8.1/mcs/class/System/System.Net/WebRequest.cs - - var type = Type.GetType("System.Net.HttpRequestCreator, System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"); - var creator = Activator.CreateInstance(type, nonPublic: true) as IWebRequestCreate; - return creator.Create(new Uri(url)) as HttpWebRequest; - } - } - - private void AddIpv4Option(HttpWebRequest request, HttpRequestOptions options) - { - request.ServicePoint.BindIPEndPointDelegate = (servicePount, remoteEndPoint, retryCount) => - { - if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) - { - return new IPEndPoint(IPAddress.Any, 0); - } - throw new InvalidOperationException("no IPv4 address"); - }; - } - - private WebRequest GetRequest(HttpRequestOptions options, string method) - { - var url = options.Url; - - var uriAddress = new Uri(url); - var userInfo = uriAddress.UserInfo; - if (!string.IsNullOrWhiteSpace(userInfo)) - { - _logger.Info("Found userInfo in url: {0} ... url: {1}", userInfo, url); - url = url.Replace(userInfo + "@", string.Empty); - } - - var request = CreateWebRequest(url); - var httpWebRequest = request as HttpWebRequest; - - if (httpWebRequest != null) - { - if (options.PreferIpv4) - { - AddIpv4Option(httpWebRequest, options); - } - - AddRequestHeaders(httpWebRequest, options); - - httpWebRequest.AutomaticDecompression = options.EnableHttpCompression ? - (options.DecompressionMethod ?? DecompressionMethods.Deflate) : - DecompressionMethods.None; - } - - request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache); - - if (httpWebRequest != null) - { - if (options.EnableKeepAlive) - { - httpWebRequest.KeepAlive = true; - } - } - - request.Method = method; - request.Timeout = options.TimeoutMs; - - if (httpWebRequest != null) - { - if (!string.IsNullOrEmpty(options.Host)) - { - httpWebRequest.Host = options.Host; - } - - if (!string.IsNullOrEmpty(options.Referer)) - { - httpWebRequest.Referer = options.Referer; - } - } - - if (!string.IsNullOrWhiteSpace(userInfo)) - { - var parts = userInfo.Split(':'); - if (parts.Length == 2) - { - request.Credentials = GetCredential(url, parts[0], parts[1]); - request.PreAuthenticate = true; - } - } - - return request; - } - - private CredentialCache GetCredential(string url, string username, string password) - { - //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; - CredentialCache credentialCache = new CredentialCache(); - credentialCache.Add(new Uri(url), "Basic", new NetworkCredential(username, password)); - return credentialCache; - } - - private void AddRequestHeaders(HttpWebRequest request, HttpRequestOptions options) - { - foreach (var header in options.RequestHeaders.ToList()) - { - if (string.Equals(header.Key, "Accept", StringComparison.OrdinalIgnoreCase)) - { - request.Accept = header.Value; - } - else if (string.Equals(header.Key, "User-Agent", StringComparison.OrdinalIgnoreCase)) - { - request.UserAgent = header.Value; - } - else - { - request.Headers.Set(header.Key, header.Value); - } - } - } - - /// - /// Gets the response internal. - /// - /// The options. - /// Task{HttpResponseInfo}. - public Task GetResponse(HttpRequestOptions options) - { - return SendAsync(options, "GET"); - } - - /// - /// Performs a GET request and returns the resulting stream - /// - /// The options. - /// Task{Stream}. - public async Task Get(HttpRequestOptions options) - { - var response = await GetResponse(options).ConfigureAwait(false); - - return response.Content; - } - - /// - /// Performs a GET request and returns the resulting stream - /// - /// The URL. - /// The resource pool. - /// The cancellation token. - /// Task{Stream}. - public Task Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken) - { - return Get(new HttpRequestOptions - { - Url = url, - ResourcePool = resourcePool, - CancellationToken = cancellationToken, - BufferContent = resourcePool != null - }); - } - - /// - /// Gets the specified URL. - /// - /// The URL. - /// The cancellation token. - /// Task{Stream}. - public Task Get(string url, CancellationToken cancellationToken) - { - return Get(url, null, cancellationToken); - } - - /// - /// send as an asynchronous operation. - /// - /// The options. - /// The HTTP method. - /// Task{HttpResponseInfo}. - /// - /// - public async Task SendAsync(HttpRequestOptions options, string httpMethod) - { - if (options.CacheMode == CacheMode.None) - { - return await SendAsyncInternal(options, httpMethod).ConfigureAwait(false); - } - - var url = options.Url; - var urlHash = url.ToLower().GetMD5().ToString("N"); - - var responseCachePath = Path.Combine(_appPaths.CachePath, "httpclient", urlHash); - - var response = await GetCachedResponse(responseCachePath, options.CacheLength, url).ConfigureAwait(false); - if (response != null) - { - return response; - } - - response = await SendAsyncInternal(options, httpMethod).ConfigureAwait(false); - - if (response.StatusCode == HttpStatusCode.OK) - { - await CacheResponse(response, responseCachePath).ConfigureAwait(false); - } - - return response; - } - - private async Task GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) - { - _logger.Info("Checking for cache file {0}", responseCachePath); - - try - { - if (_fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow) - { - using (var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true)) - { - var memoryStream = _memoryStreamProvider.CreateNew(); - - await stream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; - - return new HttpResponseInfo - { - ResponseUrl = url, - Content = memoryStream, - StatusCode = HttpStatusCode.OK, - ContentLength = memoryStream.Length - }; - } - } - } - catch (FileNotFoundException) - { - - } - catch (DirectoryNotFoundException) - { - - } - - return null; - } - - private async Task CacheResponse(HttpResponseInfo response, string responseCachePath) - { - _fileSystem.CreateDirectory(Path.GetDirectoryName(responseCachePath)); - - using (var responseStream = response.Content) - { - var memoryStream = _memoryStreamProvider.CreateNew(); - await responseStream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; - - using (var fileStream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None, true)) - { - await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false); - - memoryStream.Position = 0; - response.Content = memoryStream; - } - } - } - - private async Task SendAsyncInternal(HttpRequestOptions options, string httpMethod) - { - ValidateParams(options); - - options.CancellationToken.ThrowIfCancellationRequested(); - - var client = GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression); - - if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < TimeoutSeconds) - { - throw new HttpException(string.Format("Cancelling connection to {0} due to a previous timeout.", options.Url)) - { - IsTimedOut = true - }; - } - - var httpWebRequest = GetRequest(options, httpMethod); - - if (options.RequestContentBytes != null || - !string.IsNullOrEmpty(options.RequestContent) || - string.Equals(httpMethod, "post", StringComparison.OrdinalIgnoreCase)) - { - var bytes = options.RequestContentBytes ?? - Encoding.UTF8.GetBytes(options.RequestContent ?? string.Empty); - - httpWebRequest.ContentType = options.RequestContentType ?? "application/x-www-form-urlencoded"; - - httpWebRequest.ContentLength = bytes.Length; - httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length); - } - - if (options.ResourcePool != null) - { - await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); - } - - if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < TimeoutSeconds) - { - if (options.ResourcePool != null) - { - options.ResourcePool.Release(); - } - - throw new HttpException(string.Format("Connection to {0} timed out", options.Url)) { IsTimedOut = true }; - } - - if (options.LogRequest) - { - _logger.Info("HttpClientManager {0}: {1}", httpMethod.ToUpper(), options.Url); - } - - try - { - options.CancellationToken.ThrowIfCancellationRequested(); - - if (!options.BufferContent) - { - var response = await GetResponseAsync(httpWebRequest, TimeSpan.FromMilliseconds(options.TimeoutMs)).ConfigureAwait(false); - - var httpResponse = (HttpWebResponse)response; - - EnsureSuccessStatusCode(client, httpResponse, options); - - options.CancellationToken.ThrowIfCancellationRequested(); - - return GetResponseInfo(httpResponse, httpResponse.GetResponseStream(), GetContentLength(httpResponse), httpResponse); - } - - using (var response = await GetResponseAsync(httpWebRequest, TimeSpan.FromMilliseconds(options.TimeoutMs)).ConfigureAwait(false)) - { - var httpResponse = (HttpWebResponse)response; - - EnsureSuccessStatusCode(client, httpResponse, options); - - options.CancellationToken.ThrowIfCancellationRequested(); - - using (var stream = httpResponse.GetResponseStream()) - { - var memoryStream = _memoryStreamProvider.CreateNew(); - - await stream.CopyToAsync(memoryStream).ConfigureAwait(false); - - memoryStream.Position = 0; - - return GetResponseInfo(httpResponse, memoryStream, memoryStream.Length, null); - } - } - } - catch (OperationCanceledException ex) - { - throw GetCancellationException(options, client, options.CancellationToken, ex); - } - catch (Exception ex) - { - throw GetException(ex, options, client); - } - finally - { - if (options.ResourcePool != null) - { - options.ResourcePool.Release(); - } - } - } - - private HttpResponseInfo GetResponseInfo(HttpWebResponse httpResponse, Stream content, long? contentLength, IDisposable disposable) - { - var responseInfo = new HttpResponseInfo(disposable) - { - Content = content, - - StatusCode = httpResponse.StatusCode, - - ContentType = httpResponse.ContentType, - - ContentLength = contentLength, - - ResponseUrl = httpResponse.ResponseUri.ToString() - }; - - if (httpResponse.Headers != null) - { - SetHeaders(httpResponse.Headers, responseInfo); - } - - return responseInfo; - } - - private HttpResponseInfo GetResponseInfo(HttpWebResponse httpResponse, string tempFile, long? contentLength) - { - var responseInfo = new HttpResponseInfo - { - TempFilePath = tempFile, - - StatusCode = httpResponse.StatusCode, - - ContentType = httpResponse.ContentType, - - ContentLength = contentLength - }; - - if (httpResponse.Headers != null) - { - SetHeaders(httpResponse.Headers, responseInfo); - } - - return responseInfo; - } - - private void SetHeaders(WebHeaderCollection headers, HttpResponseInfo responseInfo) - { - foreach (var key in headers.AllKeys) - { - responseInfo.Headers[key] = headers[key]; - } - } - - public Task Post(HttpRequestOptions options) - { - return SendAsync(options, "POST"); - } - - /// - /// Performs a POST request - /// - /// The options. - /// Params to add to the POST data. - /// stream on success, null on failure - public async Task Post(HttpRequestOptions options, Dictionary postData) - { - options.SetPostData(postData); - - var response = await Post(options).ConfigureAwait(false); - - return response.Content; - } - - /// - /// Performs a POST request - /// - /// The URL. - /// Params to add to the POST data. - /// The resource pool. - /// The cancellation token. - /// stream on success, null on failure - public Task Post(string url, Dictionary postData, SemaphoreSlim resourcePool, CancellationToken cancellationToken) - { - return Post(new HttpRequestOptions - { - Url = url, - ResourcePool = resourcePool, - CancellationToken = cancellationToken, - BufferContent = resourcePool != null - - }, postData); - } - - /// - /// Downloads the contents of a given url into a temporary location - /// - /// The options. - /// Task{System.String}. - public async Task GetTempFile(HttpRequestOptions options) - { - var response = await GetTempFileResponse(options).ConfigureAwait(false); - - return response.TempFilePath; - } - - public async Task GetTempFileResponse(HttpRequestOptions options) - { - ValidateParams(options); - - _fileSystem.CreateDirectory(_appPaths.TempDirectory); - - var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp"); - - if (options.Progress == null) - { - throw new ArgumentNullException("progress"); - } - - options.CancellationToken.ThrowIfCancellationRequested(); - - var httpWebRequest = GetRequest(options, "GET"); - - if (options.ResourcePool != null) - { - await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); - } - - options.Progress.Report(0); - - if (options.LogRequest) - { - _logger.Info("HttpClientManager.GetTempFileResponse url: {0}", options.Url); - } - - var client = GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression); - - try - { - options.CancellationToken.ThrowIfCancellationRequested(); - - using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false)) - { - var httpResponse = (HttpWebResponse)response; - - EnsureSuccessStatusCode(client, httpResponse, options); - - options.CancellationToken.ThrowIfCancellationRequested(); - - var contentLength = GetContentLength(httpResponse); - - if (!contentLength.HasValue) - { - // We're not able to track progress - using (var stream = httpResponse.GetResponseStream()) - { - using (var fs = _fileSystem.GetFileStream(tempFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true)) - { - await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); - } - } - } - else - { - using (var stream = ProgressStream.CreateReadProgressStream(httpResponse.GetResponseStream(), options.Progress.Report, contentLength.Value)) - { - using (var fs = _fileSystem.GetFileStream(tempFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true)) - { - await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); - } - } - } - - options.Progress.Report(100); - - return GetResponseInfo(httpResponse, tempFile, contentLength); - } - } - catch (Exception ex) - { - DeleteTempFile(tempFile); - throw GetException(ex, options, client); - } - finally - { - if (options.ResourcePool != null) - { - options.ResourcePool.Release(); - } - } - } - - private long? GetContentLength(HttpWebResponse response) - { - var length = response.ContentLength; - - if (length == 0) - { - return null; - } - - return length; - } - - protected static readonly CultureInfo UsCulture = new CultureInfo("en-US"); - - private Exception GetException(Exception ex, HttpRequestOptions options, HttpClientInfo client) - { - if (ex is HttpException) - { - return ex; - } - - var webException = ex as WebException - ?? ex.InnerException as WebException; - - if (webException != null) - { - if (options.LogErrors) - { - _logger.ErrorException("Error getting response from " + options.Url, ex); - } - - var exception = new HttpException(ex.Message, ex); - - var response = webException.Response as HttpWebResponse; - if (response != null) - { - exception.StatusCode = response.StatusCode; - - if ((int)response.StatusCode == 429) - { - client.LastTimeout = DateTime.UtcNow; - } - } - - return exception; - } - - var operationCanceledException = ex as OperationCanceledException - ?? ex.InnerException as OperationCanceledException; - - if (operationCanceledException != null) - { - return GetCancellationException(options, client, options.CancellationToken, operationCanceledException); - } - - if (options.LogErrors) - { - _logger.ErrorException("Error getting response from " + options.Url, ex); - } - - return ex; - } - - private void DeleteTempFile(string file) - { - try - { - _fileSystem.DeleteFile(file); - } - catch (IOException) - { - // Might not have been created at all. No need to worry. - } - } - - private void ValidateParams(HttpRequestOptions options) - { - if (string.IsNullOrEmpty(options.Url)) - { - throw new ArgumentNullException("options"); - } - } - - /// - /// Gets the host from URL. - /// - /// The URL. - /// System.String. - private string GetHostFromUrl(string url) - { - var index = url.IndexOf("://", StringComparison.OrdinalIgnoreCase); - - if (index != -1) - { - url = url.Substring(index + 3); - var host = url.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); - - if (!string.IsNullOrWhiteSpace(host)) - { - return host; - } - } - - return url; - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Releases unmanaged and - optionally - managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool dispose) - { - if (dispose) - { - _httpClients.Clear(); - } - } - - /// - /// Throws the cancellation exception. - /// - /// The options. - /// The client. - /// The cancellation token. - /// The exception. - /// Exception. - private Exception GetCancellationException(HttpRequestOptions options, HttpClientInfo client, CancellationToken cancellationToken, OperationCanceledException exception) - { - // If the HttpClient's timeout is reached, it will cancel the Task internally - if (!cancellationToken.IsCancellationRequested) - { - var msg = string.Format("Connection to {0} timed out", options.Url); - - if (options.LogErrors) - { - _logger.Error(msg); - } - - client.LastTimeout = DateTime.UtcNow; - - // Throw an HttpException so that the caller doesn't think it was cancelled by user code - return new HttpException(msg, exception) - { - IsTimedOut = true - }; - } - - return exception; - } - - private void EnsureSuccessStatusCode(HttpClientInfo client, HttpWebResponse response, HttpRequestOptions options) - { - var statusCode = response.StatusCode; - - var isSuccessful = statusCode >= HttpStatusCode.OK && statusCode <= (HttpStatusCode)299; - - if (!isSuccessful) - { - if (options.LogErrorResponseBody) - { - try - { - using (var stream = response.GetResponseStream()) - { - if (stream != null) - { - using (var reader = new StreamReader(stream)) - { - var msg = reader.ReadToEnd(); - - _logger.Error(msg); - } - } - } - } - catch - { - - } - } - throw new HttpException(response.StatusDescription) - { - StatusCode = response.StatusCode - }; - } - } - - /// - /// Posts the specified URL. - /// - /// The URL. - /// The post data. - /// The cancellation token. - /// Task{Stream}. - public Task Post(string url, Dictionary postData, CancellationToken cancellationToken) - { - return Post(url, postData, null, cancellationToken); - } - - private Task GetResponseAsync(WebRequest request, TimeSpan timeout) - { - var taskCompletion = new TaskCompletionSource(); - - Task asyncTask = Task.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null); - - ThreadPool.RegisterWaitForSingleObject((asyncTask as IAsyncResult).AsyncWaitHandle, TimeoutCallback, request, timeout, true); - var callback = new TaskCallback { taskCompletion = taskCompletion }; - asyncTask.ContinueWith(callback.OnSuccess, TaskContinuationOptions.NotOnFaulted); - - // Handle errors - asyncTask.ContinueWith(callback.OnError, TaskContinuationOptions.OnlyOnFaulted); - - return taskCompletion.Task; - } - - private static void TimeoutCallback(object state, bool timedOut) - { - if (timedOut) - { - WebRequest request = (WebRequest)state; - if (state != null) - { - request.Abort(); - } - } - } - - private class TaskCallback - { - public TaskCompletionSource taskCompletion; - - public void OnSuccess(Task task) - { - taskCompletion.TrySetResult(task.Result); - } - - public void OnError(Task task) - { - if (task.Exception != null) - { - taskCompletion.TrySetException(task.Exception); - } - else - { - taskCompletion.TrySetException(new List()); - } - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/IO/IsoManager.cs b/MediaBrowser.Common.Implementations/IO/IsoManager.cs deleted file mode 100644 index de88ddada..000000000 --- a/MediaBrowser.Common.Implementations/IO/IsoManager.cs +++ /dev/null @@ -1,75 +0,0 @@ -using MediaBrowser.Model.IO; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace MediaBrowser.Common.Implementations.IO -{ - /// - /// Class IsoManager - /// - public class IsoManager : IIsoManager - { - /// - /// The _mounters - /// - private readonly List _mounters = new List(); - - /// - /// Mounts the specified iso path. - /// - /// The iso path. - /// The cancellation token. - /// IsoMount. - /// isoPath - /// - public Task Mount(string isoPath, CancellationToken cancellationToken) - { - if (string.IsNullOrEmpty(isoPath)) - { - throw new ArgumentNullException("isoPath"); - } - - var mounter = _mounters.FirstOrDefault(i => i.CanMount(isoPath)); - - if (mounter == null) - { - throw new ArgumentException(string.Format("No mounters are able to mount {0}", isoPath)); - } - - return mounter.Mount(isoPath, cancellationToken); - } - - /// - /// Determines whether this instance can mount the specified path. - /// - /// The path. - /// true if this instance can mount the specified path; otherwise, false. - public bool CanMount(string path) - { - return _mounters.Any(i => i.CanMount(path)); - } - - /// - /// Adds the parts. - /// - /// The mounters. - public void AddParts(IEnumerable mounters) - { - _mounters.AddRange(mounters); - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - foreach (var mounter in _mounters) - { - mounter.Dispose(); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/IO/ManagedFileSystem.cs b/MediaBrowser.Common.Implementations/IO/ManagedFileSystem.cs deleted file mode 100644 index 4f6c34cb7..000000000 --- a/MediaBrowser.Common.Implementations/IO/ManagedFileSystem.cs +++ /dev/null @@ -1,705 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Logging; - -namespace MediaBrowser.Common.Implementations.IO -{ - /// - /// Class ManagedFileSystem - /// - public class ManagedFileSystem : IFileSystem - { - protected ILogger Logger; - - private readonly bool _supportsAsyncFileStreams; - private char[] _invalidFileNameChars; - private readonly List _shortcutHandlers = new List(); - protected bool EnableFileSystemRequestConcat = true; - - public ManagedFileSystem(ILogger logger, bool supportsAsyncFileStreams, bool enableManagedInvalidFileNameChars) - { - Logger = logger; - _supportsAsyncFileStreams = supportsAsyncFileStreams; - SetInvalidFileNameChars(enableManagedInvalidFileNameChars); - } - - public void AddShortcutHandler(IShortcutHandler handler) - { - _shortcutHandlers.Add(handler); - } - - protected void SetInvalidFileNameChars(bool enableManagedInvalidFileNameChars) - { - if (enableManagedInvalidFileNameChars) - { - _invalidFileNameChars = Path.GetInvalidFileNameChars(); - } - else - { - // GetInvalidFileNameChars is less restrictive in Linux/Mac than Windows, this mimic Windows behavior for mono under Linux/Mac. - _invalidFileNameChars = new char[41] { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', - '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11', '\x12', - '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', - '\x1E', '\x1F', '\x22', '\x3C', '\x3E', '\x7C', ':', '*', '?', '\\', '/' }; - } - } - - public char DirectorySeparatorChar - { - get - { - return Path.DirectorySeparatorChar; - } - } - - public string GetFullPath(string path) - { - return Path.GetFullPath(path); - } - - /// - /// Determines whether the specified filename is shortcut. - /// - /// The filename. - /// true if the specified filename is shortcut; otherwise, false. - /// filename - public virtual bool IsShortcut(string filename) - { - if (string.IsNullOrEmpty(filename)) - { - throw new ArgumentNullException("filename"); - } - - var extension = Path.GetExtension(filename); - return _shortcutHandlers.Any(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase)); - } - - /// - /// Resolves the shortcut. - /// - /// The filename. - /// System.String. - /// filename - public virtual string ResolveShortcut(string filename) - { - if (string.IsNullOrEmpty(filename)) - { - throw new ArgumentNullException("filename"); - } - - var extension = Path.GetExtension(filename); - var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase)); - - if (handler != null) - { - return handler.Resolve(filename); - } - - return null; - } - - /// - /// Creates the shortcut. - /// - /// The shortcut path. - /// The target. - /// - /// shortcutPath - /// or - /// target - /// - public void CreateShortcut(string shortcutPath, string target) - { - if (string.IsNullOrEmpty(shortcutPath)) - { - throw new ArgumentNullException("shortcutPath"); - } - - if (string.IsNullOrEmpty(target)) - { - throw new ArgumentNullException("target"); - } - - var extension = Path.GetExtension(shortcutPath); - var handler = _shortcutHandlers.FirstOrDefault(i => string.Equals(extension, i.Extension, StringComparison.OrdinalIgnoreCase)); - - if (handler != null) - { - handler.Create(shortcutPath, target); - } - else - { - throw new NotImplementedException(); - } - } - - /// - /// Returns a object for the specified file or directory path. - /// - /// A path to a file or directory. - /// A object. - /// If the specified path points to a directory, the returned object's - /// property will be set to true and all other properties will reflect the properties of the directory. - public FileSystemMetadata GetFileSystemInfo(string path) - { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException("path"); - } - - // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists - if (Path.HasExtension(path)) - { - var fileInfo = new FileInfo(path); - - if (fileInfo.Exists) - { - return GetFileSystemMetadata(fileInfo); - } - - return GetFileSystemMetadata(new DirectoryInfo(path)); - } - else - { - var fileInfo = new DirectoryInfo(path); - - if (fileInfo.Exists) - { - return GetFileSystemMetadata(fileInfo); - } - - return GetFileSystemMetadata(new FileInfo(path)); - } - } - - /// - /// Returns a object for the specified file path. - /// - /// A path to a file. - /// A object. - /// If the specified path points to a directory, the returned object's - /// property and the property will both be set to false. - /// For automatic handling of files and directories, use . - public FileSystemMetadata GetFileInfo(string path) - { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException("path"); - } - - var fileInfo = new FileInfo(path); - - return GetFileSystemMetadata(fileInfo); - } - - /// - /// Returns a object for the specified directory path. - /// - /// A path to a directory. - /// A object. - /// If the specified path points to a file, the returned object's - /// property will be set to true and the property will be set to false. - /// For automatic handling of files and directories, use . - public FileSystemMetadata GetDirectoryInfo(string path) - { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException("path"); - } - - var fileInfo = new DirectoryInfo(path); - - return GetFileSystemMetadata(fileInfo); - } - - private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info) - { - var result = new FileSystemMetadata(); - - result.Exists = info.Exists; - result.FullName = info.FullName; - result.Extension = info.Extension; - result.Name = info.Name; - - if (result.Exists) - { - var attributes = info.Attributes; - result.IsDirectory = info is DirectoryInfo || (attributes & FileAttributes.Directory) == FileAttributes.Directory; - result.IsHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden; - result.IsReadOnly = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly; - - var fileInfo = info as FileInfo; - if (fileInfo != null) - { - result.Length = fileInfo.Length; - result.DirectoryName = fileInfo.DirectoryName; - } - - result.CreationTimeUtc = GetCreationTimeUtc(info); - result.LastWriteTimeUtc = GetLastWriteTimeUtc(info); - } - else - { - result.IsDirectory = info is DirectoryInfo; - } - - return result; - } - - /// - /// The space char - /// - private const char SpaceChar = ' '; - - /// - /// Takes a filename and removes invalid characters - /// - /// The filename. - /// System.String. - /// filename - public string GetValidFilename(string filename) - { - if (string.IsNullOrEmpty(filename)) - { - throw new ArgumentNullException("filename"); - } - - var builder = new StringBuilder(filename); - - foreach (var c in _invalidFileNameChars) - { - builder = builder.Replace(c, SpaceChar); - } - - return builder.ToString(); - } - - /// - /// Gets the creation time UTC. - /// - /// The info. - /// DateTime. - public DateTime GetCreationTimeUtc(FileSystemInfo info) - { - // This could throw an error on some file systems that have dates out of range - try - { - return info.CreationTimeUtc; - } - catch (Exception ex) - { - Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName); - return DateTime.MinValue; - } - } - - /// - /// Gets the creation time UTC. - /// - /// The path. - /// DateTime. - public DateTime GetCreationTimeUtc(string path) - { - return GetCreationTimeUtc(GetFileSystemInfo(path)); - } - - public DateTime GetCreationTimeUtc(FileSystemMetadata info) - { - return info.CreationTimeUtc; - } - - public DateTime GetLastWriteTimeUtc(FileSystemMetadata info) - { - return info.LastWriteTimeUtc; - } - - /// - /// Gets the creation time UTC. - /// - /// The info. - /// DateTime. - public DateTime GetLastWriteTimeUtc(FileSystemInfo info) - { - // This could throw an error on some file systems that have dates out of range - try - { - return info.LastWriteTimeUtc; - } - catch (Exception ex) - { - Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName); - return DateTime.MinValue; - } - } - - /// - /// Gets the last write time UTC. - /// - /// The path. - /// DateTime. - public DateTime GetLastWriteTimeUtc(string path) - { - return GetLastWriteTimeUtc(GetFileSystemInfo(path)); - } - - /// - /// Gets the file stream. - /// - /// The path. - /// The mode. - /// The access. - /// The share. - /// if set to true [is asynchronous]. - /// FileStream. - public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false) - { - if (_supportsAsyncFileStreams && isAsync) - { - return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144, true); - } - - return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144); - } - - private FileMode GetFileMode(FileOpenMode mode) - { - switch (mode) - { - case FileOpenMode.Append: - return FileMode.Append; - case FileOpenMode.Create: - return FileMode.Create; - case FileOpenMode.CreateNew: - return FileMode.CreateNew; - case FileOpenMode.Open: - return FileMode.Open; - case FileOpenMode.OpenOrCreate: - return FileMode.OpenOrCreate; - case FileOpenMode.Truncate: - return FileMode.Truncate; - default: - throw new Exception("Unrecognized FileOpenMode"); - } - } - - private FileAccess GetFileAccess(FileAccessMode mode) - { - var val = (int)mode; - - return (FileAccess)val; - } - - private FileShare GetFileShare(FileShareMode mode) - { - var val = (int)mode; - - return (FileShare)val; - } - - public void SetHidden(string path, bool isHidden) - { - var info = GetFileInfo(path); - - if (info.Exists && info.IsHidden != isHidden) - { - if (isHidden) - { - FileAttributes attributes = File.GetAttributes(path); - attributes = RemoveAttribute(attributes, FileAttributes.Hidden); - File.SetAttributes(path, attributes); - } - else - { - File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden); - } - } - } - - private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove) - { - return attributes & ~attributesToRemove; - } - - /// - /// Swaps the files. - /// - /// The file1. - /// The file2. - public void SwapFiles(string file1, string file2) - { - if (string.IsNullOrEmpty(file1)) - { - throw new ArgumentNullException("file1"); - } - - if (string.IsNullOrEmpty(file2)) - { - throw new ArgumentNullException("file2"); - } - - var temp1 = Path.GetTempFileName(); - var temp2 = Path.GetTempFileName(); - - // Copying over will fail against hidden files - RemoveHiddenAttribute(file1); - RemoveHiddenAttribute(file2); - - CopyFile(file1, temp1, true); - CopyFile(file2, temp2, true); - - CopyFile(temp1, file2, true); - CopyFile(temp2, file1, true); - - DeleteFile(temp1); - DeleteFile(temp2); - } - - /// - /// Removes the hidden attribute. - /// - /// The path. - private void RemoveHiddenAttribute(string path) - { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException("path"); - } - - var currentFile = new FileInfo(path); - - // This will fail if the file is hidden - if (currentFile.Exists) - { - if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) - { - currentFile.Attributes &= ~FileAttributes.Hidden; - } - } - } - - public bool ContainsSubPath(string parentPath, string path) - { - if (string.IsNullOrEmpty(parentPath)) - { - throw new ArgumentNullException("parentPath"); - } - - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException("path"); - } - - return path.IndexOf(parentPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) != -1; - } - - public bool IsRootPath(string path) - { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException("path"); - } - - var parent = Path.GetDirectoryName(path); - - if (!string.IsNullOrEmpty(parent)) - { - return false; - } - - return true; - } - - public string NormalizePath(string path) - { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException("path"); - } - - if (path.EndsWith(":\\", StringComparison.OrdinalIgnoreCase)) - { - return path; - } - - return path.TrimEnd(Path.DirectorySeparatorChar); - } - - public string GetFileNameWithoutExtension(FileSystemMetadata info) - { - if (info.IsDirectory) - { - return info.Name; - } - - return Path.GetFileNameWithoutExtension(info.FullName); - } - - public string GetFileNameWithoutExtension(string path) - { - return Path.GetFileNameWithoutExtension(path); - } - - public bool IsPathFile(string path) - { - if (string.IsNullOrWhiteSpace(path)) - { - throw new ArgumentNullException("path"); - } - - // Cannot use Path.IsPathRooted because it returns false under mono when using windows-based paths, e.g. C:\\ - - if (path.IndexOf("://", StringComparison.OrdinalIgnoreCase) != -1 && - !path.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) - { - return false; - } - return true; - - //return Path.IsPathRooted(path); - } - - public void DeleteFile(string path) - { - File.Delete(path); - } - - public void DeleteDirectory(string path, bool recursive) - { - Directory.Delete(path, recursive); - } - - public void CreateDirectory(string path) - { - Directory.CreateDirectory(path); - } - - public IEnumerable GetDirectories(string path, bool recursive = false) - { - var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; - - return ToMetadata(path, new DirectoryInfo(path).EnumerateDirectories("*", searchOption)); - } - - public IEnumerable GetFiles(string path, bool recursive = false) - { - var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; - - return ToMetadata(path, new DirectoryInfo(path).EnumerateFiles("*", searchOption)); - } - - public IEnumerable GetFileSystemEntries(string path, bool recursive = false) - { - var directoryInfo = new DirectoryInfo(path); - var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; - - if (EnableFileSystemRequestConcat) - { - return ToMetadata(path, directoryInfo.EnumerateDirectories("*", searchOption)) - .Concat(ToMetadata(path, directoryInfo.EnumerateFiles("*", searchOption))); - } - - return ToMetadata(path, directoryInfo.EnumerateFileSystemInfos("*", searchOption)); - } - - private IEnumerable ToMetadata(string parentPath, IEnumerable infos) - { - return infos.Select(i => - { - try - { - return GetFileSystemMetadata(i); - } - catch (PathTooLongException) - { - // Can't log using the FullName because it will throw the PathTooLongExceptiona again - //Logger.Warn("Path too long: {0}", i.FullName); - Logger.Warn("File or directory path too long. Parent folder: {0}", parentPath); - return null; - } - - }).Where(i => i != null); - } - - public Stream OpenRead(string path) - { - return File.OpenRead(path); - } - - public void CopyFile(string source, string target, bool overwrite) - { - File.Copy(source, target, overwrite); - } - - public void MoveFile(string source, string target) - { - File.Move(source, target); - } - - public void MoveDirectory(string source, string target) - { - Directory.Move(source, target); - } - - public bool DirectoryExists(string path) - { - return Directory.Exists(path); - } - - public bool FileExists(string path) - { - return File.Exists(path); - } - - public string ReadAllText(string path) - { - return File.ReadAllText(path); - } - - public byte[] ReadAllBytes(string path) - { - return File.ReadAllBytes(path); - } - - public void WriteAllText(string path, string text, Encoding encoding) - { - File.WriteAllText(path, text, encoding); - } - - public void WriteAllText(string path, string text) - { - File.WriteAllText(path, text); - } - - public void WriteAllBytes(string path, byte[] bytes) - { - File.WriteAllBytes(path, bytes); - } - - public string ReadAllText(string path, Encoding encoding) - { - return File.ReadAllText(path, encoding); - } - - public IEnumerable GetDirectoryPaths(string path, bool recursive = false) - { - var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; - return Directory.EnumerateDirectories(path, "*", searchOption); - } - - public IEnumerable GetFilePaths(string path, bool recursive = false) - { - var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; - return Directory.EnumerateFiles(path, "*", searchOption); - } - - public IEnumerable GetFileSystemEntryPaths(string path, bool recursive = false) - { - var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; - return Directory.EnumerateFileSystemEntries(path, "*", searchOption); - } - } -} diff --git a/MediaBrowser.Common.Implementations/IO/WindowsFileSystem.cs b/MediaBrowser.Common.Implementations/IO/WindowsFileSystem.cs deleted file mode 100644 index db386c682..000000000 --- a/MediaBrowser.Common.Implementations/IO/WindowsFileSystem.cs +++ /dev/null @@ -1,13 +0,0 @@ -using MediaBrowser.Model.Logging; - -namespace MediaBrowser.Common.Implementations.IO -{ - public class WindowsFileSystem : ManagedFileSystem - { - public WindowsFileSystem(ILogger logger) - : base(logger, true, true) - { - EnableFileSystemRequestConcat = false; - } - } -} diff --git a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj deleted file mode 100644 index 89e9427ae..000000000 --- a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj +++ /dev/null @@ -1,110 +0,0 @@ - - - - - Debug - AnyCPU - {C4D2573A-3FD3-441F-81AF-174AC4CD4E1D} - Library - Properties - MediaBrowser.Common.Implementations - MediaBrowser.Common.Implementations - 512 - ..\ - 10.0.0 - 2.0 - v4.6 - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - none - true - bin\Release\ - TRACE - prompt - 4 - - - none - true - bin\Release Mono\ - TRACE - prompt - 4 - - - Always - - - - - - - - - - - - Properties\SharedVersion.cs - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {9142EEFA-7570-41E1-BFCC-468BB571AF2F} - MediaBrowser.Common - - - {7EEEB4BB-F3E8-48FC-B4C5-70F0FFF8329B} - MediaBrowser.Model - - - - - - - - - - - - \ No newline at end of file diff --git a/MediaBrowser.Common.Implementations/Networking/BaseNetworkManager.cs b/MediaBrowser.Common.Implementations/Networking/BaseNetworkManager.cs deleted file mode 100644 index 5e00514d5..000000000 --- a/MediaBrowser.Common.Implementations/Networking/BaseNetworkManager.cs +++ /dev/null @@ -1,385 +0,0 @@ -using MediaBrowser.Model.Logging; -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Net; -using System.Net.NetworkInformation; -using System.Net.Sockets; -using MediaBrowser.Model.Extensions; - -namespace MediaBrowser.Common.Implementations.Networking -{ - public abstract class BaseNetworkManager - { - protected ILogger Logger { get; private set; } - private DateTime _lastRefresh; - - protected BaseNetworkManager(ILogger logger) - { - Logger = logger; - } - - private List _localIpAddresses; - private readonly object _localIpAddressSyncLock = new object(); - - /// - /// Gets the machine's local ip address - /// - /// IPAddress. - public IEnumerable GetLocalIpAddresses() - { - const int cacheMinutes = 5; - - lock (_localIpAddressSyncLock) - { - var forceRefresh = (DateTime.UtcNow - _lastRefresh).TotalMinutes >= cacheMinutes; - - if (_localIpAddresses == null || forceRefresh) - { - var addresses = GetLocalIpAddressesInternal().ToList(); - - _localIpAddresses = addresses; - _lastRefresh = DateTime.UtcNow; - - return addresses; - } - } - - return _localIpAddresses; - } - - private IEnumerable GetLocalIpAddressesInternal() - { - var list = GetIPsDefault() - .ToList(); - - if (list.Count == 0) - { - list.AddRange(GetLocalIpAddressesFallback()); - } - - return list.Where(FilterIpAddress).DistinctBy(i => i.ToString()); - } - - private bool FilterIpAddress(IPAddress address) - { - var addressString = address.ToString (); - - if (addressString.StartsWith("169.", StringComparison.OrdinalIgnoreCase)) - { - return false; - } - - return true; - } - - public bool IsInPrivateAddressSpace(string endpoint) - { - if (string.Equals(endpoint, "::1", StringComparison.OrdinalIgnoreCase)) - { - return true; - } - - // Handle ipv4 mapped to ipv6 - endpoint = endpoint.Replace("::ffff:", string.Empty); - - // Private address space: - // http://en.wikipedia.org/wiki/Private_network - - if (endpoint.StartsWith("172.", StringComparison.OrdinalIgnoreCase)) - { - return Is172AddressPrivate(endpoint); - } - - return - - endpoint.StartsWith("localhost", StringComparison.OrdinalIgnoreCase) || - endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) || - endpoint.StartsWith("10.", StringComparison.OrdinalIgnoreCase) || - endpoint.StartsWith("192.168", StringComparison.OrdinalIgnoreCase) || - endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase); - } - - private bool Is172AddressPrivate(string endpoint) - { - for (var i = 16; i <= 31; i++) - { - if (endpoint.StartsWith("172." + i.ToString(CultureInfo.InvariantCulture) + ".", StringComparison.OrdinalIgnoreCase)) - { - return true; - } - } - - return false; - } - - public bool IsInLocalNetwork(string endpoint) - { - return IsInLocalNetworkInternal(endpoint, true); - } - - public bool IsInLocalNetworkInternal(string endpoint, bool resolveHost) - { - if (string.IsNullOrWhiteSpace(endpoint)) - { - throw new ArgumentNullException("endpoint"); - } - - IPAddress address; - if (IPAddress.TryParse(endpoint, out address)) - { - var addressString = address.ToString(); - - int lengthMatch = 100; - if (address.AddressFamily == AddressFamily.InterNetwork) - { - lengthMatch = 4; - if (IsInPrivateAddressSpace(addressString)) - { - return true; - } - } - else if (address.AddressFamily == AddressFamily.InterNetworkV6) - { - lengthMatch = 10; - if (IsInPrivateAddressSpace(endpoint)) - { - return true; - } - } - - // Should be even be doing this with ipv6? - if (addressString.Length >= lengthMatch) - { - var prefix = addressString.Substring(0, lengthMatch); - - if (GetLocalIpAddresses().Any(i => i.ToString().StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) - { - return true; - } - } - } - else if (resolveHost) - { - Uri uri; - if (Uri.TryCreate(endpoint, UriKind.RelativeOrAbsolute, out uri)) - { - try - { - var host = uri.DnsSafeHost; - Logger.Debug("Resolving host {0}", host); - - address = GetIpAddresses(host).FirstOrDefault(); - - if (address != null) - { - Logger.Debug("{0} resolved to {1}", host, address); - - return IsInLocalNetworkInternal(address.ToString(), false); - } - } - catch (InvalidOperationException) - { - // Can happen with reverse proxy or IIS url rewriting - } - catch (Exception ex) - { - Logger.ErrorException("Error resovling hostname", ex); - } - } - } - - return false; - } - - public IEnumerable GetIpAddresses(string hostName) - { - return Dns.GetHostAddresses(hostName); - } - - private List GetIPsDefault() - { - NetworkInterface[] interfaces; - - try - { - interfaces = NetworkInterface.GetAllNetworkInterfaces(); - } - catch (Exception ex) - { - Logger.ErrorException("Error in GetAllNetworkInterfaces", ex); - return new List(); - } - - return interfaces.SelectMany(network => { - - try - { - Logger.Debug("Querying interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus); - - var properties = network.GetIPProperties(); - - return properties.UnicastAddresses - .Where(i => i.IsDnsEligible) - .Select(i => i.Address) - .Where(i => i.AddressFamily == AddressFamily.InterNetwork) - .ToList(); - } - catch (Exception ex) - { - Logger.ErrorException("Error querying network interface", ex); - return new List(); - } - - }).DistinctBy(i => i.ToString()) - .ToList(); - } - - private IEnumerable GetLocalIpAddressesFallback() - { - var host = Dns.GetHostEntry(Dns.GetHostName()); - - // Reverse them because the last one is usually the correct one - // It's not fool-proof so ultimately the consumer will have to examine them and decide - return host.AddressList - .Where(i => i.AddressFamily == AddressFamily.InterNetwork) - .Reverse(); - } - - /// - /// Gets a random port number that is currently available - /// - /// System.Int32. - public int GetRandomUnusedPort() - { - var listener = new TcpListener(IPAddress.Any, 0); - listener.Start(); - var port = ((IPEndPoint)listener.LocalEndpoint).Port; - listener.Stop(); - return port; - } - - /// - /// Returns MAC Address from first Network Card in Computer - /// - /// [string] MAC Address - public string GetMacAddress() - { - return NetworkInterface.GetAllNetworkInterfaces() - .Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback) - .Select(i => BitConverter.ToString(i.GetPhysicalAddress().GetAddressBytes())) - .FirstOrDefault(); - } - - /// - /// Parses the specified endpointstring. - /// - /// The endpointstring. - /// IPEndPoint. - public IPEndPoint Parse(string endpointstring) - { - return Parse(endpointstring, -1); - } - - /// - /// Parses the specified endpointstring. - /// - /// The endpointstring. - /// The defaultport. - /// IPEndPoint. - /// Endpoint descriptor may not be empty. - /// - private static IPEndPoint Parse(string endpointstring, int defaultport) - { - if (String.IsNullOrEmpty(endpointstring) - || endpointstring.Trim().Length == 0) - { - throw new ArgumentException("Endpoint descriptor may not be empty."); - } - - if (defaultport != -1 && - (defaultport < IPEndPoint.MinPort - || defaultport > IPEndPoint.MaxPort)) - { - throw new ArgumentException(String.Format("Invalid default port '{0}'", defaultport)); - } - - string[] values = endpointstring.Split(new char[] { ':' }); - IPAddress ipaddy; - int port = -1; - - //check if we have an IPv6 or ports - if (values.Length <= 2) // ipv4 or hostname - { - port = values.Length == 1 ? defaultport : GetPort(values[1]); - - //try to use the address as IPv4, otherwise get hostname - if (!IPAddress.TryParse(values[0], out ipaddy)) - ipaddy = GetIPfromHost(values[0]); - } - else if (values.Length > 2) //ipv6 - { - //could [a:b:c]:d - if (values[0].StartsWith("[") && values[values.Length - 2].EndsWith("]")) - { - string ipaddressstring = String.Join(":", values.Take(values.Length - 1).ToArray()); - ipaddy = IPAddress.Parse(ipaddressstring); - port = GetPort(values[values.Length - 1]); - } - else //[a:b:c] or a:b:c - { - ipaddy = IPAddress.Parse(endpointstring); - port = defaultport; - } - } - else - { - throw new FormatException(String.Format("Invalid endpoint ipaddress '{0}'", endpointstring)); - } - - if (port == -1) - throw new ArgumentException(String.Format("No port specified: '{0}'", endpointstring)); - - return new IPEndPoint(ipaddy, port); - } - - protected static readonly CultureInfo UsCulture = new CultureInfo("en-US"); - - /// - /// Gets the port. - /// - /// The p. - /// System.Int32. - /// - private static int GetPort(string p) - { - int port; - - if (!Int32.TryParse(p, out port) - || port < IPEndPoint.MinPort - || port > IPEndPoint.MaxPort) - { - throw new FormatException(String.Format("Invalid end point port '{0}'", p)); - } - - return port; - } - - /// - /// Gets the I pfrom host. - /// - /// The p. - /// IPAddress. - /// - private static IPAddress GetIPfromHost(string p) - { - var hosts = Dns.GetHostAddresses(p); - - if (hosts == null || hosts.Length == 0) - throw new ArgumentException(String.Format("Host not found: {0}", p)); - - return hosts[0]; - } - } -} diff --git a/MediaBrowser.Common.Implementations/Properties/AssemblyInfo.cs b/MediaBrowser.Common.Implementations/Properties/AssemblyInfo.cs deleted file mode 100644 index 63a83e003..000000000 --- a/MediaBrowser.Common.Implementations/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Reflection; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("MediaBrowser.Common.Implementations")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("MediaBrowser.Common.Implementations")] -[assembly: AssemblyCopyright("Copyright © 2013")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("fc7d85c6-0fe7-4db6-8158-54f7b18f17cd")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/DailyTrigger.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/DailyTrigger.cs deleted file mode 100644 index ab1d8f02d..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/DailyTrigger.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using System.Globalization; -using System.Threading; -using MediaBrowser.Model.Events; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Tasks; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks -{ - /// - /// Represents a task trigger that fires everyday - /// - public class DailyTrigger : ITaskTrigger - { - /// - /// Get the time of day to trigger the task to run - /// - /// The time of day. - public TimeSpan TimeOfDay { get; set; } - - /// - /// Gets or sets the timer. - /// - /// The timer. - private Timer Timer { get; set; } - - /// - /// Gets the execution properties of this task. - /// - /// - /// The execution properties of this task. - /// - public TaskExecutionOptions TaskOptions { get; set; } - - /// - /// Stars waiting for the trigger action - /// - /// The last result. - /// if set to true [is application startup]. - public void Start(TaskResult lastResult, ILogger logger, string taskName, bool isApplicationStartup) - { - DisposeTimer(); - - var now = DateTime.Now; - - var triggerDate = now.TimeOfDay > TimeOfDay ? now.Date.AddDays(1) : now.Date; - triggerDate = triggerDate.Add(TimeOfDay); - - var dueTime = triggerDate - now; - - logger.Info("Daily trigger for {0} set to fire at {1}, which is {2} minutes from now.", taskName, triggerDate.ToString(), dueTime.TotalMinutes.ToString(CultureInfo.InvariantCulture)); - - Timer = new Timer(state => OnTriggered(), null, dueTime, TimeSpan.FromMilliseconds(-1)); - } - - /// - /// Stops waiting for the trigger action - /// - public void Stop() - { - DisposeTimer(); - } - - /// - /// Disposes the timer. - /// - private void DisposeTimer() - { - if (Timer != null) - { - Timer.Dispose(); - } - } - - /// - /// Occurs when [triggered]. - /// - public event EventHandler> Triggered; - - /// - /// Called when [triggered]. - /// - private void OnTriggered() - { - if (Triggered != null) - { - Triggered(this, new GenericEventArgs(TaskOptions)); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/IntervalTrigger.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/IntervalTrigger.cs deleted file mode 100644 index 251e460cb..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/IntervalTrigger.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using System.Linq; -using System.Threading; -using MediaBrowser.Model.Events; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Tasks; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks -{ - /// - /// Represents a task trigger that runs repeatedly on an interval - /// - public class IntervalTrigger : ITaskTrigger - { - /// - /// Gets or sets the interval. - /// - /// The interval. - public TimeSpan Interval { get; set; } - - /// - /// Gets or sets the timer. - /// - /// The timer. - private Timer Timer { get; set; } - - /// - /// Gets the execution properties of this task. - /// - /// - /// The execution properties of this task. - /// - public TaskExecutionOptions TaskOptions { get; set; } - - private DateTime _lastStartDate; - - /// - /// Stars waiting for the trigger action - /// - /// The last result. - /// if set to true [is application startup]. - public void Start(TaskResult lastResult, ILogger logger, string taskName, bool isApplicationStartup) - { - DisposeTimer(); - - DateTime triggerDate; - - if (lastResult == null) - { - // Task has never been completed before - triggerDate = DateTime.UtcNow.AddHours(1); - } - else - { - triggerDate = new[] { lastResult.EndTimeUtc, _lastStartDate }.Max().Add(Interval); - } - - if (DateTime.UtcNow > triggerDate) - { - triggerDate = DateTime.UtcNow.AddMinutes(1); - } - - var dueTime = triggerDate - DateTime.UtcNow; - var maxDueTime = TimeSpan.FromDays(7); - - if (dueTime > maxDueTime) - { - dueTime = maxDueTime; - } - - Timer = new Timer(state => OnTriggered(), null, dueTime, TimeSpan.FromMilliseconds(-1)); - } - - /// - /// Stops waiting for the trigger action - /// - public void Stop() - { - DisposeTimer(); - } - - /// - /// Disposes the timer. - /// - private void DisposeTimer() - { - if (Timer != null) - { - Timer.Dispose(); - } - } - - /// - /// Occurs when [triggered]. - /// - public event EventHandler> Triggered; - - /// - /// Called when [triggered]. - /// - private void OnTriggered() - { - DisposeTimer(); - - if (Triggered != null) - { - _lastStartDate = DateTime.UtcNow; - Triggered(this, new GenericEventArgs(TaskOptions)); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs deleted file mode 100644 index 0d99283c3..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs +++ /dev/null @@ -1,781 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Events; -using MediaBrowser.Common.Extensions; -using MediaBrowser.Common.ScheduledTasks; -using MediaBrowser.Model.Events; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Serialization; -using MediaBrowser.Model.Tasks; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Common.IO; -using MediaBrowser.Model.IO; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks -{ - /// - /// Class ScheduledTaskWorker - /// - public class ScheduledTaskWorker : IScheduledTaskWorker - { - public event EventHandler> TaskProgress; - - /// - /// Gets or sets the scheduled task. - /// - /// The scheduled task. - public IScheduledTask ScheduledTask { get; private set; } - - /// - /// Gets or sets the json serializer. - /// - /// The json serializer. - private IJsonSerializer JsonSerializer { get; set; } - - /// - /// Gets or sets the application paths. - /// - /// The application paths. - private IApplicationPaths ApplicationPaths { get; set; } - - /// - /// Gets the logger. - /// - /// The logger. - private ILogger Logger { get; set; } - - /// - /// Gets the task manager. - /// - /// The task manager. - private ITaskManager TaskManager { get; set; } - private readonly IFileSystem _fileSystem; - - /// - /// Initializes a new instance of the class. - /// - /// The scheduled task. - /// The application paths. - /// The task manager. - /// The json serializer. - /// The logger. - /// - /// scheduledTask - /// or - /// applicationPaths - /// or - /// taskManager - /// or - /// jsonSerializer - /// or - /// logger - /// - public ScheduledTaskWorker(IScheduledTask scheduledTask, IApplicationPaths applicationPaths, ITaskManager taskManager, IJsonSerializer jsonSerializer, ILogger logger, IFileSystem fileSystem) - { - if (scheduledTask == null) - { - throw new ArgumentNullException("scheduledTask"); - } - if (applicationPaths == null) - { - throw new ArgumentNullException("applicationPaths"); - } - if (taskManager == null) - { - throw new ArgumentNullException("taskManager"); - } - if (jsonSerializer == null) - { - throw new ArgumentNullException("jsonSerializer"); - } - if (logger == null) - { - throw new ArgumentNullException("logger"); - } - - ScheduledTask = scheduledTask; - ApplicationPaths = applicationPaths; - TaskManager = taskManager; - JsonSerializer = jsonSerializer; - Logger = logger; - _fileSystem = fileSystem; - - InitTriggerEvents(); - } - - private bool _readFromFile = false; - /// - /// The _last execution result - /// - private TaskResult _lastExecutionResult; - /// - /// The _last execution result sync lock - /// - private readonly object _lastExecutionResultSyncLock = new object(); - /// - /// Gets the last execution result. - /// - /// The last execution result. - public TaskResult LastExecutionResult - { - get - { - var path = GetHistoryFilePath(); - - lock (_lastExecutionResultSyncLock) - { - if (_lastExecutionResult == null && !_readFromFile) - { - try - { - _lastExecutionResult = JsonSerializer.DeserializeFromFile(path); - } - catch (DirectoryNotFoundException) - { - // File doesn't exist. No biggie - } - catch (FileNotFoundException) - { - // File doesn't exist. No biggie - } - catch (Exception ex) - { - Logger.ErrorException("Error deserializing {0}", ex, path); - } - _readFromFile = true; - } - } - - return _lastExecutionResult; - } - private set - { - _lastExecutionResult = value; - - var path = GetHistoryFilePath(); - _fileSystem.CreateDirectory(Path.GetDirectoryName(path)); - - lock (_lastExecutionResultSyncLock) - { - JsonSerializer.SerializeToFile(value, path); - } - } - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return ScheduledTask.Name; } - } - - /// - /// Gets the description. - /// - /// The description. - public string Description - { - get { return ScheduledTask.Description; } - } - - /// - /// Gets the category. - /// - /// The category. - public string Category - { - get { return ScheduledTask.Category; } - } - - /// - /// Gets the current cancellation token - /// - /// The current cancellation token source. - private CancellationTokenSource CurrentCancellationTokenSource { get; set; } - - /// - /// Gets or sets the current execution start time. - /// - /// The current execution start time. - private DateTime CurrentExecutionStartTime { get; set; } - - /// - /// Gets the state. - /// - /// The state. - public TaskState State - { - get - { - if (CurrentCancellationTokenSource != null) - { - return CurrentCancellationTokenSource.IsCancellationRequested - ? TaskState.Cancelling - : TaskState.Running; - } - - return TaskState.Idle; - } - } - - /// - /// Gets the current progress. - /// - /// The current progress. - public double? CurrentProgress { get; private set; } - - /// - /// The _triggers - /// - private Tuple[] _triggers; - /// - /// Gets the triggers that define when the task will run - /// - /// The triggers. - private Tuple[] InternalTriggers - { - get - { - return _triggers; - } - set - { - if (value == null) - { - throw new ArgumentNullException("value"); - } - - // Cleanup current triggers - if (_triggers != null) - { - DisposeTriggers(); - } - - _triggers = value.ToArray(); - - ReloadTriggerEvents(false); - } - } - - /// - /// Gets the triggers that define when the task will run - /// - /// The triggers. - /// value - public TaskTriggerInfo[] Triggers - { - get - { - return InternalTriggers.Select(i => i.Item1).ToArray(); - } - set - { - if (value == null) - { - throw new ArgumentNullException("value"); - } - - SaveTriggers(value); - - InternalTriggers = value.Select(i => new Tuple(i, GetTrigger(i))).ToArray(); - } - } - - /// - /// The _id - /// - private string _id; - - /// - /// Gets the unique id. - /// - /// The unique id. - public string Id - { - get - { - if (_id == null) - { - _id = ScheduledTask.GetType().FullName.GetMD5().ToString("N"); - } - - return _id; - } - } - - private void InitTriggerEvents() - { - _triggers = LoadTriggers(); - ReloadTriggerEvents(true); - } - - public void ReloadTriggerEvents() - { - ReloadTriggerEvents(false); - } - - /// - /// Reloads the trigger events. - /// - /// if set to true [is application startup]. - private void ReloadTriggerEvents(bool isApplicationStartup) - { - foreach (var triggerInfo in InternalTriggers) - { - var trigger = triggerInfo.Item2; - - trigger.Stop(); - - trigger.Triggered -= trigger_Triggered; - trigger.Triggered += trigger_Triggered; - trigger.Start(LastExecutionResult, Logger, Name, isApplicationStartup); - } - } - - /// - /// Handles the Triggered event of the trigger control. - /// - /// The source of the event. - /// The instance containing the event data. - async void trigger_Triggered(object sender, GenericEventArgs e) - { - var trigger = (ITaskTrigger)sender; - - var configurableTask = ScheduledTask as IConfigurableScheduledTask; - - if (configurableTask != null && !configurableTask.IsEnabled) - { - return; - } - - Logger.Info("{0} fired for task: {1}", trigger.GetType().Name, Name); - - trigger.Stop(); - - TaskManager.QueueScheduledTask(ScheduledTask, e.Argument); - - await Task.Delay(1000).ConfigureAwait(false); - - trigger.Start(LastExecutionResult, Logger, Name, false); - } - - private Task _currentTask; - - /// - /// Executes the task - /// - /// Task options. - /// Task. - /// Cannot execute a Task that is already running - public async Task Execute(TaskExecutionOptions options) - { - var task = ExecuteInternal(options); - - _currentTask = task; - - try - { - await task.ConfigureAwait(false); - } - finally - { - _currentTask = null; - } - } - - private async Task ExecuteInternal(TaskExecutionOptions options) - { - // Cancel the current execution, if any - if (CurrentCancellationTokenSource != null) - { - throw new InvalidOperationException("Cannot execute a Task that is already running"); - } - - var progress = new Progress(); - - CurrentCancellationTokenSource = new CancellationTokenSource(); - - Logger.Info("Executing {0}", Name); - - ((TaskManager)TaskManager).OnTaskExecuting(this); - - progress.ProgressChanged += progress_ProgressChanged; - - TaskCompletionStatus status; - CurrentExecutionStartTime = DateTime.UtcNow; - - Exception failureException = null; - - try - { - if (options != null && options.MaxRuntimeMs.HasValue) - { - CurrentCancellationTokenSource.CancelAfter(options.MaxRuntimeMs.Value); - } - - var localTask = ScheduledTask.Execute(CurrentCancellationTokenSource.Token, progress); - - await localTask.ConfigureAwait(false); - - status = TaskCompletionStatus.Completed; - } - catch (OperationCanceledException) - { - status = TaskCompletionStatus.Cancelled; - } - catch (Exception ex) - { - Logger.ErrorException("Error", ex); - - failureException = ex; - - status = TaskCompletionStatus.Failed; - } - - var startTime = CurrentExecutionStartTime; - var endTime = DateTime.UtcNow; - - progress.ProgressChanged -= progress_ProgressChanged; - CurrentCancellationTokenSource.Dispose(); - CurrentCancellationTokenSource = null; - CurrentProgress = null; - - OnTaskCompleted(startTime, endTime, status, failureException); - } - - /// - /// Progress_s the progress changed. - /// - /// The sender. - /// The e. - void progress_ProgressChanged(object sender, double e) - { - CurrentProgress = e; - - EventHelper.FireEventIfNotNull(TaskProgress, this, new GenericEventArgs - { - Argument = e - - }, Logger); - } - - /// - /// Stops the task if it is currently executing - /// - /// Cannot cancel a Task unless it is in the Running state. - public void Cancel() - { - if (State != TaskState.Running) - { - throw new InvalidOperationException("Cannot cancel a Task unless it is in the Running state."); - } - - CancelIfRunning(); - } - - /// - /// Cancels if running. - /// - public void CancelIfRunning() - { - if (State == TaskState.Running) - { - Logger.Info("Attempting to cancel Scheduled Task {0}", Name); - CurrentCancellationTokenSource.Cancel(); - } - } - - /// - /// Gets the scheduled tasks configuration directory. - /// - /// System.String. - private string GetScheduledTasksConfigurationDirectory() - { - return Path.Combine(ApplicationPaths.ConfigurationDirectoryPath, "ScheduledTasks"); - } - - /// - /// Gets the scheduled tasks data directory. - /// - /// System.String. - private string GetScheduledTasksDataDirectory() - { - return Path.Combine(ApplicationPaths.DataPath, "ScheduledTasks"); - } - - /// - /// Gets the history file path. - /// - /// The history file path. - private string GetHistoryFilePath() - { - return Path.Combine(GetScheduledTasksDataDirectory(), new Guid(Id) + ".js"); - } - - /// - /// Gets the configuration file path. - /// - /// System.String. - private string GetConfigurationFilePath() - { - return Path.Combine(GetScheduledTasksConfigurationDirectory(), new Guid(Id) + ".js"); - } - - /// - /// Loads the triggers. - /// - /// IEnumerable{BaseTaskTrigger}. - private Tuple[] LoadTriggers() - { - var settings = LoadTriggerSettings(); - - return settings.Select(i => new Tuple(i, GetTrigger(i))).ToArray(); - } - - private TaskTriggerInfo[] LoadTriggerSettings() - { - try - { - return JsonSerializer.DeserializeFromFile>(GetConfigurationFilePath()) - .ToArray(); - } - catch (FileNotFoundException) - { - // File doesn't exist. No biggie. Return defaults. - return ScheduledTask.GetDefaultTriggers().ToArray(); - } - catch (DirectoryNotFoundException) - { - // File doesn't exist. No biggie. Return defaults. - return ScheduledTask.GetDefaultTriggers().ToArray(); - } - } - - /// - /// Saves the triggers. - /// - /// The triggers. - private void SaveTriggers(TaskTriggerInfo[] triggers) - { - var path = GetConfigurationFilePath(); - - _fileSystem.CreateDirectory(Path.GetDirectoryName(path)); - - JsonSerializer.SerializeToFile(triggers, path); - } - - /// - /// Called when [task completed]. - /// - /// The start time. - /// The end time. - /// The status. - private void OnTaskCompleted(DateTime startTime, DateTime endTime, TaskCompletionStatus status, Exception ex) - { - var elapsedTime = endTime - startTime; - - Logger.Info("{0} {1} after {2} minute(s) and {3} seconds", Name, status, Math.Truncate(elapsedTime.TotalMinutes), elapsedTime.Seconds); - - var result = new TaskResult - { - StartTimeUtc = startTime, - EndTimeUtc = endTime, - Status = status, - Name = Name, - Id = Id - }; - - result.Key = ScheduledTask.Key; - - if (ex != null) - { - result.ErrorMessage = ex.Message; - result.LongErrorMessage = ex.StackTrace; - } - - LastExecutionResult = result; - - ((TaskManager)TaskManager).OnTaskCompleted(this, result); - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Releases unmanaged and - optionally - managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool dispose) - { - if (dispose) - { - DisposeTriggers(); - - var wassRunning = State == TaskState.Running; - var startTime = CurrentExecutionStartTime; - - var token = CurrentCancellationTokenSource; - if (token != null) - { - try - { - Logger.Info(Name + ": Cancelling"); - token.Cancel(); - } - catch (Exception ex) - { - Logger.ErrorException("Error calling CancellationToken.Cancel();", ex); - } - } - var task = _currentTask; - if (task != null) - { - try - { - Logger.Info(Name + ": Waiting on Task"); - var exited = Task.WaitAll(new[] { task }, 2000); - - if (exited) - { - Logger.Info(Name + ": Task exited"); - } - else - { - Logger.Info(Name + ": Timed out waiting for task to stop"); - } - } - catch (Exception ex) - { - Logger.ErrorException("Error calling Task.WaitAll();", ex); - } - } - - if (token != null) - { - try - { - Logger.Debug(Name + ": Disposing CancellationToken"); - token.Dispose(); - } - catch (Exception ex) - { - Logger.ErrorException("Error calling CancellationToken.Dispose();", ex); - } - } - if (wassRunning) - { - OnTaskCompleted(startTime, DateTime.UtcNow, TaskCompletionStatus.Aborted, null); - } - } - } - - /// - /// Converts a TaskTriggerInfo into a concrete BaseTaskTrigger - /// - /// The info. - /// BaseTaskTrigger. - /// - /// Invalid trigger type: + info.Type - public static ITaskTrigger GetTrigger(TaskTriggerInfo info) - { - var options = new TaskExecutionOptions - { - MaxRuntimeMs = info.MaxRuntimeMs - }; - - if (info.Type.Equals(typeof(DailyTrigger).Name, StringComparison.OrdinalIgnoreCase)) - { - if (!info.TimeOfDayTicks.HasValue) - { - throw new ArgumentNullException(); - } - - return new DailyTrigger - { - TimeOfDay = TimeSpan.FromTicks(info.TimeOfDayTicks.Value), - TaskOptions = options - }; - } - - if (info.Type.Equals(typeof(WeeklyTrigger).Name, StringComparison.OrdinalIgnoreCase)) - { - if (!info.TimeOfDayTicks.HasValue) - { - throw new ArgumentNullException(); - } - - if (!info.DayOfWeek.HasValue) - { - throw new ArgumentNullException(); - } - - return new WeeklyTrigger - { - TimeOfDay = TimeSpan.FromTicks(info.TimeOfDayTicks.Value), - DayOfWeek = info.DayOfWeek.Value, - TaskOptions = options - }; - } - - if (info.Type.Equals(typeof(IntervalTrigger).Name, StringComparison.OrdinalIgnoreCase)) - { - if (!info.IntervalTicks.HasValue) - { - throw new ArgumentNullException(); - } - - return new IntervalTrigger - { - Interval = TimeSpan.FromTicks(info.IntervalTicks.Value), - TaskOptions = options - }; - } - - if (info.Type.Equals(typeof(SystemEventTrigger).Name, StringComparison.OrdinalIgnoreCase)) - { - if (!info.SystemEvent.HasValue) - { - throw new ArgumentNullException(); - } - - return new SystemEventTrigger - { - SystemEvent = info.SystemEvent.Value, - TaskOptions = options - }; - } - - if (info.Type.Equals(typeof(StartupTrigger).Name, StringComparison.OrdinalIgnoreCase)) - { - return new StartupTrigger(); - } - - throw new ArgumentException("Unrecognized trigger type: " + info.Type); - } - - /// - /// Disposes each trigger - /// - private void DisposeTriggers() - { - foreach (var triggerInfo in InternalTriggers) - { - var trigger = triggerInfo.Item2; - trigger.Triggered -= trigger_Triggered; - trigger.Stop(); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/StartupTrigger.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/StartupTrigger.cs deleted file mode 100644 index c96a41ac8..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/StartupTrigger.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Threading.Tasks; -using MediaBrowser.Model.Events; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Tasks; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks -{ - /// - /// Class StartupTaskTrigger - /// - public class StartupTrigger : ITaskTrigger - { - public int DelayMs { get; set; } - - /// - /// Gets the execution properties of this task. - /// - /// - /// The execution properties of this task. - /// - public TaskExecutionOptions TaskOptions { get; set; } - - public StartupTrigger() - { - DelayMs = 3000; - } - - /// - /// Stars waiting for the trigger action - /// - /// The last result. - /// if set to true [is application startup]. - public async void Start(TaskResult lastResult, ILogger logger, string taskName, bool isApplicationStartup) - { - if (isApplicationStartup) - { - await Task.Delay(DelayMs).ConfigureAwait(false); - - OnTriggered(); - } - } - - /// - /// Stops waiting for the trigger action - /// - public void Stop() - { - } - - /// - /// Occurs when [triggered]. - /// - public event EventHandler> Triggered; - - /// - /// Called when [triggered]. - /// - private void OnTriggered() - { - if (Triggered != null) - { - Triggered(this, new GenericEventArgs(TaskOptions)); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/SystemEventTrigger.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/SystemEventTrigger.cs deleted file mode 100644 index 9972dc804..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/SystemEventTrigger.cs +++ /dev/null @@ -1,84 +0,0 @@ -using MediaBrowser.Model.Events; -using MediaBrowser.Model.Tasks; -using Microsoft.Win32; -using System; -using System.Threading.Tasks; -using MediaBrowser.Model.Logging; - -namespace MediaBrowser.Common.ScheduledTasks -{ - /// - /// Class SystemEventTrigger - /// - public class SystemEventTrigger : ITaskTrigger - { - /// - /// Gets or sets the system event. - /// - /// The system event. - public SystemEvent SystemEvent { get; set; } - - /// - /// Gets the execution properties of this task. - /// - /// - /// The execution properties of this task. - /// - public TaskExecutionOptions TaskOptions { get; set; } - - /// - /// Stars waiting for the trigger action - /// - /// The last result. - /// if set to true [is application startup]. - public void Start(TaskResult lastResult, ILogger logger, string taskName, bool isApplicationStartup) - { - switch (SystemEvent) - { - case SystemEvent.WakeFromSleep: - SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; - break; - } - } - - /// - /// Stops waiting for the trigger action - /// - public void Stop() - { - SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; - } - - /// - /// Handles the PowerModeChanged event of the SystemEvents control. - /// - /// The source of the event. - /// The instance containing the event data. - async void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) - { - if (e.Mode == PowerModes.Resume && SystemEvent == SystemEvent.WakeFromSleep) - { - // This value is a bit arbitrary, but add a delay to help ensure network connections have been restored before running the task - await Task.Delay(10000).ConfigureAwait(false); - - OnTriggered(); - } - } - - /// - /// Occurs when [triggered]. - /// - public event EventHandler> Triggered; - - /// - /// Called when [triggered]. - /// - private void OnTriggered() - { - if (Triggered != null) - { - Triggered(this, new GenericEventArgs(TaskOptions)); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs deleted file mode 100644 index 5c721d915..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs +++ /dev/null @@ -1,361 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Events; -using MediaBrowser.Common.ScheduledTasks; -using MediaBrowser.Model.Events; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Serialization; -using MediaBrowser.Model.Tasks; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using MediaBrowser.Common.IO; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.System; -using Microsoft.Win32; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks -{ - /// - /// Class TaskManager - /// - public class TaskManager : ITaskManager - { - public event EventHandler> TaskExecuting; - public event EventHandler TaskCompleted; - - /// - /// Gets the list of Scheduled Tasks - /// - /// The scheduled tasks. - public IScheduledTaskWorker[] ScheduledTasks { get; private set; } - - /// - /// The _task queue - /// - private readonly ConcurrentQueue> _taskQueue = - new ConcurrentQueue>(); - - /// - /// Gets or sets the json serializer. - /// - /// The json serializer. - private IJsonSerializer JsonSerializer { get; set; } - - /// - /// Gets or sets the application paths. - /// - /// The application paths. - private IApplicationPaths ApplicationPaths { get; set; } - - private readonly ISystemEvents _systemEvents; - - /// - /// Gets the logger. - /// - /// The logger. - private ILogger Logger { get; set; } - private readonly IFileSystem _fileSystem; - - private bool _suspendTriggers; - - public bool SuspendTriggers - { - get { return _suspendTriggers; } - set - { - Logger.Info("Setting SuspendTriggers to {0}", value); - var executeQueued = _suspendTriggers && !value; - - _suspendTriggers = value; - - if (executeQueued) - { - ExecuteQueuedTasks(); - } - } - } - - /// - /// Initializes a new instance of the class. - /// - /// The application paths. - /// The json serializer. - /// The logger. - /// kernel - public TaskManager(IApplicationPaths applicationPaths, IJsonSerializer jsonSerializer, ILogger logger, IFileSystem fileSystem, ISystemEvents systemEvents) - { - ApplicationPaths = applicationPaths; - JsonSerializer = jsonSerializer; - Logger = logger; - _fileSystem = fileSystem; - _systemEvents = systemEvents; - - ScheduledTasks = new IScheduledTaskWorker[] { }; - } - - private void BindToSystemEvent() - { - _systemEvents.Resume += _systemEvents_Resume; - } - - private void _systemEvents_Resume(object sender, EventArgs e) - { - foreach (var task in ScheduledTasks) - { - task.ReloadTriggerEvents(); - } - } - - /// - /// Cancels if running and queue. - /// - /// - /// Task options. - public void CancelIfRunningAndQueue(TaskExecutionOptions options) - where T : IScheduledTask - { - var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)); - ((ScheduledTaskWorker)task).CancelIfRunning(); - - QueueScheduledTask(options); - } - - public void CancelIfRunningAndQueue() - where T : IScheduledTask - { - CancelIfRunningAndQueue(new TaskExecutionOptions()); - } - - /// - /// Cancels if running - /// - /// - public void CancelIfRunning() - where T : IScheduledTask - { - var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)); - ((ScheduledTaskWorker)task).CancelIfRunning(); - } - - /// - /// Queues the scheduled task. - /// - /// - /// Task options - public void QueueScheduledTask(TaskExecutionOptions options) - where T : IScheduledTask - { - var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == typeof(T)); - - if (scheduledTask == null) - { - Logger.Error("Unable to find scheduled task of type {0} in QueueScheduledTask.", typeof(T).Name); - } - else - { - QueueScheduledTask(scheduledTask, options); - } - } - - public void QueueScheduledTask() - where T : IScheduledTask - { - QueueScheduledTask(new TaskExecutionOptions()); - } - - public void QueueIfNotRunning() - where T : IScheduledTask - { - var task = ScheduledTasks.First(t => t.ScheduledTask.GetType() == typeof(T)); - - if (task.State != TaskState.Running) - { - QueueScheduledTask(new TaskExecutionOptions()); - } - } - - public void Execute() - where T : IScheduledTask - { - var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == typeof(T)); - - if (scheduledTask == null) - { - Logger.Error("Unable to find scheduled task of type {0} in Execute.", typeof(T).Name); - } - else - { - var type = scheduledTask.ScheduledTask.GetType(); - - Logger.Info("Queueing task {0}", type.Name); - - lock (_taskQueue) - { - if (scheduledTask.State == TaskState.Idle) - { - Execute(scheduledTask, new TaskExecutionOptions()); - } - } - } - } - - /// - /// Queues the scheduled task. - /// - /// The task. - /// The task options. - public void QueueScheduledTask(IScheduledTask task, TaskExecutionOptions options) - { - var scheduledTask = ScheduledTasks.FirstOrDefault(t => t.ScheduledTask.GetType() == task.GetType()); - - if (scheduledTask == null) - { - Logger.Error("Unable to find scheduled task of type {0} in QueueScheduledTask.", task.GetType().Name); - } - else - { - QueueScheduledTask(scheduledTask, options); - } - } - - /// - /// Queues the scheduled task. - /// - /// The task. - /// The task options. - private void QueueScheduledTask(IScheduledTaskWorker task, TaskExecutionOptions options) - { - var type = task.ScheduledTask.GetType(); - - Logger.Info("Queueing task {0}", type.Name); - - lock (_taskQueue) - { - if (task.State == TaskState.Idle && !SuspendTriggers) - { - Execute(task, options); - return; - } - - _taskQueue.Enqueue(new Tuple(type, options)); - } - } - - /// - /// Adds the tasks. - /// - /// The tasks. - public void AddTasks(IEnumerable tasks) - { - var myTasks = ScheduledTasks.ToList(); - - var list = tasks.ToList(); - myTasks.AddRange(list.Select(t => new ScheduledTaskWorker(t, ApplicationPaths, this, JsonSerializer, Logger, _fileSystem))); - - ScheduledTasks = myTasks.ToArray(); - - BindToSystemEvent(); - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Releases unmanaged and - optionally - managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool dispose) - { - foreach (var task in ScheduledTasks) - { - task.Dispose(); - } - } - - public void Cancel(IScheduledTaskWorker task) - { - ((ScheduledTaskWorker)task).Cancel(); - } - - public Task Execute(IScheduledTaskWorker task, TaskExecutionOptions options) - { - return ((ScheduledTaskWorker)task).Execute(options); - } - - /// - /// Called when [task executing]. - /// - /// The task. - internal void OnTaskExecuting(IScheduledTaskWorker task) - { - EventHelper.FireEventIfNotNull(TaskExecuting, this, new GenericEventArgs - { - Argument = task - - }, Logger); - } - - /// - /// Called when [task completed]. - /// - /// The task. - /// The result. - internal void OnTaskCompleted(IScheduledTaskWorker task, TaskResult result) - { - EventHelper.FireEventIfNotNull(TaskCompleted, task, new TaskCompletionEventArgs - { - Result = result, - Task = task - - }, Logger); - - ExecuteQueuedTasks(); - } - - /// - /// Executes the queued tasks. - /// - private void ExecuteQueuedTasks() - { - if (SuspendTriggers) - { - return; - } - - Logger.Info("ExecuteQueuedTasks"); - - // Execute queued tasks - lock (_taskQueue) - { - var list = new List>(); - - Tuple item; - while (_taskQueue.TryDequeue(out item)) - { - if (list.All(i => i.Item1 != item.Item1)) - { - list.Add(item); - } - } - - foreach (var enqueuedType in list) - { - var scheduledTask = ScheduledTasks.First(t => t.ScheduledTask.GetType() == enqueuedType.Item1); - - if (scheduledTask.State == TaskState.Idle) - { - Execute(scheduledTask, enqueuedType.Item2); - } - } - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs deleted file mode 100644 index 2cd3f4c0f..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs +++ /dev/null @@ -1,217 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.ScheduledTasks; -using MediaBrowser.Model.Logging; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Common.IO; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Tasks; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks -{ - /// - /// Deletes old cache files - /// - public class DeleteCacheFileTask : IScheduledTask, IConfigurableScheduledTask - { - /// - /// Gets or sets the application paths. - /// - /// The application paths. - private IApplicationPaths ApplicationPaths { get; set; } - - private readonly ILogger _logger; - - private readonly IFileSystem _fileSystem; - - /// - /// Initializes a new instance of the class. - /// - public DeleteCacheFileTask(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem) - { - ApplicationPaths = appPaths; - _logger = logger; - _fileSystem = fileSystem; - } - - /// - /// Creates the triggers that define when the task will run - /// - /// IEnumerable{BaseTaskTrigger}. - public IEnumerable GetDefaultTriggers() - { - return new[] { - - // Every so often - new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks} - }; - } - - /// - /// Returns the task to be executed - /// - /// The cancellation token. - /// The progress. - /// Task. - public Task Execute(CancellationToken cancellationToken, IProgress progress) - { - var minDateModified = DateTime.UtcNow.AddDays(-30); - - try - { - DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress); - } - catch (DirectoryNotFoundException) - { - // No biggie here. Nothing to delete - } - - progress.Report(90); - - minDateModified = DateTime.UtcNow.AddDays(-1); - - try - { - DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.TempDirectory, minDateModified, progress); - } - catch (DirectoryNotFoundException) - { - // No biggie here. Nothing to delete - } - - return Task.FromResult(true); - } - - - /// - /// Deletes the cache files from directory with a last write time less than a given date - /// - /// The task cancellation token. - /// The directory. - /// The min date modified. - /// The progress. - private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress progress) - { - var filesToDelete = _fileSystem.GetFiles(directory, true) - .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified) - .ToList(); - - var index = 0; - - foreach (var file in filesToDelete) - { - double percent = index; - percent /= filesToDelete.Count; - - progress.Report(100 * percent); - - cancellationToken.ThrowIfCancellationRequested(); - - DeleteFile(file.FullName); - - index++; - } - - DeleteEmptyFolders(directory); - - progress.Report(100); - } - - private void DeleteEmptyFolders(string parent) - { - foreach (var directory in _fileSystem.GetDirectoryPaths(parent)) - { - DeleteEmptyFolders(directory); - if (!_fileSystem.GetFileSystemEntryPaths(directory).Any()) - { - try - { - _fileSystem.DeleteDirectory(directory, false); - } - catch (UnauthorizedAccessException ex) - { - _logger.ErrorException("Error deleting directory {0}", ex, directory); - } - catch (IOException ex) - { - _logger.ErrorException("Error deleting directory {0}", ex, directory); - } - } - } - } - - private void DeleteFile(string path) - { - try - { - _fileSystem.DeleteFile(path); - } - catch (UnauthorizedAccessException ex) - { - _logger.ErrorException("Error deleting file {0}", ex, path); - } - catch (IOException ex) - { - _logger.ErrorException("Error deleting file {0}", ex, path); - } - } - - /// - /// Gets the name of the task - /// - /// The name. - public string Name - { - get { return "Cache file cleanup"; } - } - - public string Key - { - get { return "DeleteCacheFiles"; } - } - - /// - /// Gets the description. - /// - /// The description. - public string Description - { - get { return "Deletes cache files no longer needed by the system"; } - } - - /// - /// Gets the category. - /// - /// The category. - public string Category - { - get - { - return "Maintenance"; - } - } - - /// - /// Gets a value indicating whether this instance is hidden. - /// - /// true if this instance is hidden; otherwise, false. - public bool IsHidden - { - get { return true; } - } - - public bool IsEnabled - { - get { return true; } - } - - public bool IsLogged - { - get { return true; } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs deleted file mode 100644 index 336f91f96..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs +++ /dev/null @@ -1,140 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.ScheduledTasks; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Common.IO; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Tasks; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks -{ - /// - /// Deletes old log files - /// - public class DeleteLogFileTask : IScheduledTask, IConfigurableScheduledTask - { - /// - /// Gets or sets the configuration manager. - /// - /// The configuration manager. - private IConfigurationManager ConfigurationManager { get; set; } - - private readonly IFileSystem _fileSystem; - - /// - /// Initializes a new instance of the class. - /// - /// The configuration manager. - public DeleteLogFileTask(IConfigurationManager configurationManager, IFileSystem fileSystem) - { - ConfigurationManager = configurationManager; - _fileSystem = fileSystem; - } - - /// - /// Creates the triggers that define when the task will run - /// - /// IEnumerable{BaseTaskTrigger}. - public IEnumerable GetDefaultTriggers() - { - return new[] { - - // Every so often - new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks} - }; - } - - /// - /// Returns the task to be executed - /// - /// The cancellation token. - /// The progress. - /// Task. - public Task Execute(CancellationToken cancellationToken, IProgress progress) - { - // Delete log files more than n days old - var minDateModified = DateTime.UtcNow.AddDays(-ConfigurationManager.CommonConfiguration.LogFileRetentionDays); - - var filesToDelete = _fileSystem.GetFiles(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, true) - .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified) - .ToList(); - - var index = 0; - - foreach (var file in filesToDelete) - { - double percent = index; - percent /= filesToDelete.Count; - - progress.Report(100 * percent); - - cancellationToken.ThrowIfCancellationRequested(); - - _fileSystem.DeleteFile(file.FullName); - - index++; - } - - progress.Report(100); - - return Task.FromResult(true); - } - - public string Key - { - get { return "CleanLogFiles"; } - } - - /// - /// Gets the name of the task - /// - /// The name. - public string Name - { - get { return "Log file cleanup"; } - } - - /// - /// Gets the description. - /// - /// The description. - public string Description - { - get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); } - } - - /// - /// Gets the category. - /// - /// The category. - public string Category - { - get - { - return "Maintenance"; - } - } - - /// - /// Gets a value indicating whether this instance is hidden. - /// - /// true if this instance is hidden; otherwise, false. - public bool IsHidden - { - get { return true; } - } - - public bool IsEnabled - { - get { return true; } - } - - public bool IsLogged - { - get { return true; } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/ReloadLoggerFileTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/ReloadLoggerFileTask.cs deleted file mode 100644 index 3c33df832..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/ReloadLoggerFileTask.cs +++ /dev/null @@ -1,113 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.ScheduledTasks; -using MediaBrowser.Model.Logging; -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Model.Tasks; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks -{ - /// - /// Class ReloadLoggerFileTask - /// - public class ReloadLoggerFileTask : IScheduledTask, IConfigurableScheduledTask - { - /// - /// Gets or sets the log manager. - /// - /// The log manager. - private ILogManager LogManager { get; set; } - /// - /// Gets or sets the configuration manager. - /// - /// The configuration manager. - private IConfigurationManager ConfigurationManager { get; set; } - - /// - /// Initializes a new instance of the class. - /// - /// The logManager. - /// The configuration manager. - public ReloadLoggerFileTask(ILogManager logManager, IConfigurationManager configurationManager) - { - LogManager = logManager; - ConfigurationManager = configurationManager; - } - - /// - /// Gets the default triggers. - /// - /// IEnumerable{BaseTaskTrigger}. - public IEnumerable GetDefaultTriggers() - { - var trigger = new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerDaily, TimeOfDayTicks = TimeSpan.FromHours(0).Ticks }; //12am - - return new[] { trigger }; - } - - /// - /// Executes the internal. - /// - /// The cancellation token. - /// The progress. - /// Task. - public Task Execute(CancellationToken cancellationToken, IProgress progress) - { - cancellationToken.ThrowIfCancellationRequested(); - - progress.Report(0); - - LogManager.ReloadLogger(ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging - ? LogSeverity.Debug - : LogSeverity.Info); - - return Task.FromResult(true); - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return "Start new log file"; } - } - - public string Key { get; } - - /// - /// Gets the description. - /// - /// The description. - public string Description - { - get { return "Moves logging to a new file to help reduce log file sizes."; } - } - - /// - /// Gets the category. - /// - /// The category. - public string Category - { - get { return "Application"; } - } - - public bool IsHidden - { - get { return true; } - } - - public bool IsEnabled - { - get { return true; } - } - - public bool IsLogged - { - get { return true; } - } - } -} diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/WeeklyTrigger.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/WeeklyTrigger.cs deleted file mode 100644 index 0d8af4e9c..000000000 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/WeeklyTrigger.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.Threading; -using MediaBrowser.Model.Events; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Tasks; - -namespace MediaBrowser.Common.Implementations.ScheduledTasks -{ - /// - /// Represents a task trigger that fires on a weekly basis - /// - public class WeeklyTrigger : ITaskTrigger - { - /// - /// Get the time of day to trigger the task to run - /// - /// The time of day. - public TimeSpan TimeOfDay { get; set; } - - /// - /// Gets or sets the day of week. - /// - /// The day of week. - public DayOfWeek DayOfWeek { get; set; } - - /// - /// Gets the execution properties of this task. - /// - /// - /// The execution properties of this task. - /// - public TaskExecutionOptions TaskOptions { get; set; } - - /// - /// Gets or sets the timer. - /// - /// The timer. - private Timer Timer { get; set; } - - /// - /// Stars waiting for the trigger action - /// - /// The last result. - /// if set to true [is application startup]. - public void Start(TaskResult lastResult, ILogger logger, string taskName, bool isApplicationStartup) - { - DisposeTimer(); - - var triggerDate = GetNextTriggerDateTime(); - - Timer = new Timer(state => OnTriggered(), null, triggerDate - DateTime.Now, TimeSpan.FromMilliseconds(-1)); - } - - /// - /// Gets the next trigger date time. - /// - /// DateTime. - private DateTime GetNextTriggerDateTime() - { - var now = DateTime.Now; - - // If it's on the same day - if (now.DayOfWeek == DayOfWeek) - { - // It's either later today, or a week from now - return now.TimeOfDay < TimeOfDay ? now.Date.Add(TimeOfDay) : now.Date.AddDays(7).Add(TimeOfDay); - } - - var triggerDate = now.Date; - - // Walk the date forward until we get to the trigger day - while (triggerDate.DayOfWeek != DayOfWeek) - { - triggerDate = triggerDate.AddDays(1); - } - - // Return the trigger date plus the time offset - return triggerDate.Add(TimeOfDay); - } - - /// - /// Stops waiting for the trigger action - /// - public void Stop() - { - DisposeTimer(); - } - - /// - /// Disposes the timer. - /// - private void DisposeTimer() - { - if (Timer != null) - { - Timer.Dispose(); - } - } - - /// - /// Occurs when [triggered]. - /// - public event EventHandler> Triggered; - - /// - /// Called when [triggered]. - /// - private void OnTriggered() - { - if (Triggered != null) - { - Triggered(this, new GenericEventArgs(TaskOptions)); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs b/MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs deleted file mode 100644 index f48681304..000000000 --- a/MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs +++ /dev/null @@ -1,130 +0,0 @@ -using MediaBrowser.Model.Serialization; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.IO; -using System.Xml; -using MediaBrowser.Common.IO; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Logging; - -namespace MediaBrowser.Common.Implementations.Serialization -{ - /// - /// Provides a wrapper around third party xml serialization. - /// - public class XmlSerializer : IXmlSerializer - { - private readonly IFileSystem _fileSystem; - private readonly ILogger _logger; - - public XmlSerializer(IFileSystem fileSystem, ILogger logger) - { - _fileSystem = fileSystem; - _logger = logger; - } - - // Need to cache these - // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html - private readonly Dictionary _serializers = - new Dictionary(); - - private System.Xml.Serialization.XmlSerializer GetSerializer(Type type) - { - var key = type.FullName; - lock (_serializers) - { - System.Xml.Serialization.XmlSerializer serializer; - if (!_serializers.TryGetValue(key, out serializer)) - { - serializer = new System.Xml.Serialization.XmlSerializer(type); - _serializers[key] = serializer; - } - return serializer; - } - } - - /// - /// Serializes to writer. - /// - /// The obj. - /// The writer. - private void SerializeToWriter(object obj, XmlTextWriter writer) - { - writer.Formatting = Formatting.Indented; - var netSerializer = GetSerializer(obj.GetType()); - netSerializer.Serialize(writer, obj); - } - - /// - /// Deserializes from stream. - /// - /// The type. - /// The stream. - /// System.Object. - public object DeserializeFromStream(Type type, Stream stream) - { - using (var reader = new XmlTextReader(stream)) - { - var netSerializer = GetSerializer(type); - return netSerializer.Deserialize(reader); - } - } - - /// - /// Serializes to stream. - /// - /// The obj. - /// The stream. - public void SerializeToStream(object obj, Stream stream) - { - using (var writer = new XmlTextWriter(stream, null)) - { - SerializeToWriter(obj, writer); - } - } - - /// - /// Serializes to file. - /// - /// The obj. - /// The file. - public void SerializeToFile(object obj, string file) - { - _logger.Debug("Serializing to file {0}", file); - using (var stream = new FileStream(file, FileMode.Create)) - { - SerializeToStream(obj, stream); - } - } - - /// - /// Deserializes from file. - /// - /// The type. - /// The file. - /// System.Object. - public object DeserializeFromFile(Type type, string file) - { - _logger.Debug("Deserializing file {0}", file); - using (var stream = _fileSystem.OpenRead(file)) - { - return DeserializeFromStream(type, stream); - } - } - - /// - /// Deserializes from bytes. - /// - /// The type. - /// The buffer. - /// System.Object. - public object DeserializeFromBytes(Type type, byte[] buffer) - { - using (var stream = new MemoryStream(buffer)) - { - return DeserializeFromStream(type, stream); - } - } - } -} diff --git a/MediaBrowser.Common.Implementations/Updates/GithubUpdater.cs b/MediaBrowser.Common.Implementations/Updates/GithubUpdater.cs deleted file mode 100644 index 371c2ea11..000000000 --- a/MediaBrowser.Common.Implementations/Updates/GithubUpdater.cs +++ /dev/null @@ -1,269 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Common.Net; -using MediaBrowser.Model.Serialization; -using MediaBrowser.Model.Updates; - -namespace MediaBrowser.Common.Implementations.Updates -{ - public class GithubUpdater - { - private readonly IHttpClient _httpClient; - private readonly IJsonSerializer _jsonSerializer; - - public GithubUpdater(IHttpClient httpClient, IJsonSerializer jsonSerializer) - { - _httpClient = httpClient; - _jsonSerializer = jsonSerializer; - } - - public async Task CheckForUpdateResult(string organzation, string repository, Version minVersion, PackageVersionClass updateLevel, string assetFilename, string packageName, string targetFilename, TimeSpan cacheLength, CancellationToken cancellationToken) - { - var url = string.Format("https://api.github.com/repos/{0}/{1}/releases", organzation, repository); - - var options = new HttpRequestOptions - { - Url = url, - EnableKeepAlive = false, - CancellationToken = cancellationToken, - UserAgent = "Emby/3.0", - BufferContent = false - }; - - if (cacheLength.Ticks > 0) - { - options.CacheMode = CacheMode.Unconditional; - options.CacheLength = cacheLength; - } - - using (var stream = await _httpClient.Get(options).ConfigureAwait(false)) - { - var obj = _jsonSerializer.DeserializeFromStream(stream); - - return CheckForUpdateResult(obj, minVersion, updateLevel, assetFilename, packageName, targetFilename); - } - } - - private CheckForUpdateResult CheckForUpdateResult(RootObject[] obj, Version minVersion, PackageVersionClass updateLevel, string assetFilename, string packageName, string targetFilename) - { - if (updateLevel == PackageVersionClass.Release) - { - // Technically all we need to do is check that it's not pre-release - // But let's addititional checks for -beta and -dev to handle builds that might be temporarily tagged incorrectly. - obj = obj.Where(i => !i.prerelease && !i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) && !i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase)).ToArray(); - } - else if (updateLevel == PackageVersionClass.Beta) - { - obj = obj.Where(i => !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase)).ToArray(); - } - else if (updateLevel == PackageVersionClass.Dev) - { - obj = obj.Where(i => !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) || i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase)).ToArray(); - } - - var availableUpdate = obj - .Select(i => CheckForUpdateResult(i, minVersion, assetFilename, packageName, targetFilename)) - .Where(i => i != null) - .OrderByDescending(i => Version.Parse(i.AvailableVersion)) - .FirstOrDefault(); - - return availableUpdate ?? new CheckForUpdateResult - { - IsUpdateAvailable = false - }; - } - - private bool MatchesUpdateLevel(RootObject i, PackageVersionClass updateLevel) - { - if (updateLevel == PackageVersionClass.Beta) - { - return !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase); - } - if (updateLevel == PackageVersionClass.Dev) - { - return !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) || - i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase); - } - - // Technically all we need to do is check that it's not pre-release - // But let's addititional checks for -beta and -dev to handle builds that might be temporarily tagged incorrectly. - return !i.prerelease && !i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) && - !i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase); - } - - public async Task> GetLatestReleases(string organzation, string repository, string assetFilename, CancellationToken cancellationToken) - { - var list = new List(); - - var url = string.Format("https://api.github.com/repos/{0}/{1}/releases", organzation, repository); - - var options = new HttpRequestOptions - { - Url = url, - EnableKeepAlive = false, - CancellationToken = cancellationToken, - UserAgent = "Emby/3.0", - BufferContent = false - }; - - using (var stream = await _httpClient.Get(options).ConfigureAwait(false)) - { - var obj = _jsonSerializer.DeserializeFromStream(stream); - - obj = obj.Where(i => (i.assets ?? new List()).Any(a => IsAsset(a, assetFilename))).ToArray(); - - list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Release)).OrderByDescending(GetVersion).Take(1)); - list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Beta)).OrderByDescending(GetVersion).Take(1)); - list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Dev)).OrderByDescending(GetVersion).Take(1)); - - return list; - } - } - - public Version GetVersion(RootObject obj) - { - Version version; - if (!Version.TryParse(obj.tag_name, out version)) - { - return new Version(1, 0); - } - - return version; - } - - private CheckForUpdateResult CheckForUpdateResult(RootObject obj, Version minVersion, string assetFilename, string packageName, string targetFilename) - { - Version version; - if (!Version.TryParse(obj.tag_name, out version)) - { - return null; - } - - if (version < minVersion) - { - return null; - } - - var asset = (obj.assets ?? new List()).FirstOrDefault(i => IsAsset(i, assetFilename)); - - if (asset == null) - { - return null; - } - - return new CheckForUpdateResult - { - AvailableVersion = version.ToString(), - IsUpdateAvailable = version > minVersion, - Package = new PackageVersionInfo - { - classification = obj.prerelease ? - (obj.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase) ? PackageVersionClass.Dev : PackageVersionClass.Beta) : - PackageVersionClass.Release, - name = packageName, - sourceUrl = asset.browser_download_url, - targetFilename = targetFilename, - versionStr = version.ToString(), - requiredVersionStr = "1.0.0", - description = obj.body, - infoUrl = obj.html_url - } - }; - } - - private bool IsAsset(Asset asset, string assetFilename) - { - var downloadFilename = Path.GetFileName(asset.browser_download_url) ?? string.Empty; - - if (downloadFilename.IndexOf(assetFilename, StringComparison.OrdinalIgnoreCase) != -1) - { - return true; - } - - return string.Equals(assetFilename, downloadFilename, StringComparison.OrdinalIgnoreCase); - } - - public class Uploader - { - public string login { get; set; } - public int id { get; set; } - public string avatar_url { get; set; } - public string gravatar_id { get; set; } - public string url { get; set; } - public string html_url { get; set; } - public string followers_url { get; set; } - public string following_url { get; set; } - public string gists_url { get; set; } - public string starred_url { get; set; } - public string subscriptions_url { get; set; } - public string organizations_url { get; set; } - public string repos_url { get; set; } - public string events_url { get; set; } - public string received_events_url { get; set; } - public string type { get; set; } - public bool site_admin { get; set; } - } - - public class Asset - { - public string url { get; set; } - public int id { get; set; } - public string name { get; set; } - public object label { get; set; } - public Uploader uploader { get; set; } - public string content_type { get; set; } - public string state { get; set; } - public int size { get; set; } - public int download_count { get; set; } - public string created_at { get; set; } - public string updated_at { get; set; } - public string browser_download_url { get; set; } - } - - public class Author - { - public string login { get; set; } - public int id { get; set; } - public string avatar_url { get; set; } - public string gravatar_id { get; set; } - public string url { get; set; } - public string html_url { get; set; } - public string followers_url { get; set; } - public string following_url { get; set; } - public string gists_url { get; set; } - public string starred_url { get; set; } - public string subscriptions_url { get; set; } - public string organizations_url { get; set; } - public string repos_url { get; set; } - public string events_url { get; set; } - public string received_events_url { get; set; } - public string type { get; set; } - public bool site_admin { get; set; } - } - - public class RootObject - { - public string url { get; set; } - public string assets_url { get; set; } - public string upload_url { get; set; } - public string html_url { get; set; } - public int id { get; set; } - public string tag_name { get; set; } - public string target_commitish { get; set; } - public string name { get; set; } - public bool draft { get; set; } - public Author author { get; set; } - public bool prerelease { get; set; } - public string created_at { get; set; } - public string published_at { get; set; } - public List assets { get; set; } - public string tarball_url { get; set; } - public string zipball_url { get; set; } - public string body { get; set; } - } - } -} -- cgit v1.2.3