From 15ebff2b3a738855d6c35fafc37d40a72293eef8 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 8 Nov 2016 17:28:04 -0500 Subject: [PATCH 1/4] make additional classes portable --- .../Emby.Server.Implementations.csproj | 1 + MediaBrowser.Model/MediaBrowser.Model.csproj | 1 + .../HttpServer/HttpListenerHost.cs | 3 - ...MediaBrowser.Server.Implementations.csproj | 1 - .../Social/SharingManager.cs | 100 ------------------ .../Social/SharingRepository.cs | 2 +- .../ApplicationHost.cs | 1 + 7 files changed, 4 insertions(+), 105 deletions(-) delete mode 100644 MediaBrowser.Server.Implementations/Social/SharingManager.cs diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csproj index 806702dfd..f5c157234 100644 --- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj +++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj @@ -187,6 +187,7 @@ + diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index ee7582619..abf07e1dc 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -143,6 +143,7 @@ + diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 7ef8c464e..5d9d05034 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -134,8 +134,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer base.OnConfigLoad(); Config.HandlerFactoryPath = null; - - Config.MetadataRedirectPath = "metadata"; } protected override ServiceController CreateServiceController(params Assembly[] assembliesWithServices) @@ -574,7 +572,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer { httpRes.StatusCode = 302; httpRes.AddHeader(HttpHeaders.Location, url); - httpRes.EndRequest(); } diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index 4096d71dc..3d38c2e3c 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -146,7 +146,6 @@ - diff --git a/MediaBrowser.Server.Implementations/Social/SharingManager.cs b/MediaBrowser.Server.Implementations/Social/SharingManager.cs deleted file mode 100644 index 0bab7a70d..000000000 --- a/MediaBrowser.Server.Implementations/Social/SharingManager.cs +++ /dev/null @@ -1,100 +0,0 @@ -using MediaBrowser.Common.Extensions; -using MediaBrowser.Controller; -using MediaBrowser.Controller.Configuration; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Model.Social; -using System; -using System.Threading.Tasks; - -namespace MediaBrowser.Server.Implementations.Social -{ - public class SharingManager : ISharingManager - { - private readonly SharingRepository _repository; - private readonly IServerConfigurationManager _config; - private readonly ILibraryManager _libraryManager; - private readonly IServerApplicationHost _appHost; - - public SharingManager(SharingRepository repository, IServerConfigurationManager config, ILibraryManager libraryManager, IServerApplicationHost appHost) - { - _repository = repository; - _config = config; - _libraryManager = libraryManager; - _appHost = appHost; - } - - public async Task CreateShare(string itemId, string userId) - { - if (string.IsNullOrWhiteSpace(itemId)) - { - throw new ArgumentNullException("itemId"); - } - if (string.IsNullOrWhiteSpace(userId)) - { - throw new ArgumentNullException("userId"); - } - - var item = _libraryManager.GetItemById(itemId); - - if (item == null) - { - throw new ResourceNotFoundException(); - } - - var externalUrl = (await _appHost.GetSystemInfo().ConfigureAwait(false)).WanAddress; - - if (string.IsNullOrWhiteSpace(externalUrl)) - { - throw new InvalidOperationException("No external server address is currently available."); - } - - var info = new SocialShareInfo - { - Id = Guid.NewGuid().ToString("N"), - ExpirationDate = DateTime.UtcNow.AddDays(_config.Configuration.SharingExpirationDays), - ItemId = itemId, - UserId = userId - }; - - AddShareInfo(info, externalUrl); - - await _repository.CreateShare(info).ConfigureAwait(false); - - return info; - } - - private string GetTitle(BaseItem item) - { - return item.Name; - } - - public SocialShareInfo GetShareInfo(string id) - { - var info = _repository.GetShareInfo(id); - - AddShareInfo(info, _appHost.GetSystemInfo().Result.WanAddress); - - return info; - } - - private void AddShareInfo(SocialShareInfo info, string externalUrl) - { - info.ImageUrl = externalUrl + "/Social/Shares/Public/" + info.Id + "/Image"; - info.Url = externalUrl + "/emby/web/shared.html?id=" + info.Id; - - var item = _libraryManager.GetItemById(info.ItemId); - - if (item != null) - { - info.Overview = item.Overview; - info.Name = GetTitle(item); - } - } - - public Task DeleteShare(string id) - { - return _repository.DeleteShare(id); - } - } -} diff --git a/MediaBrowser.Server.Implementations/Social/SharingRepository.cs b/MediaBrowser.Server.Implementations/Social/SharingRepository.cs index c4243c1a7..dec43e4cb 100644 --- a/MediaBrowser.Server.Implementations/Social/SharingRepository.cs +++ b/MediaBrowser.Server.Implementations/Social/SharingRepository.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.Social { - public class SharingRepository : BaseSqliteRepository + public class SharingRepository : BaseSqliteRepository, ISharingRepository { public SharingRepository(ILogManager logManager, IApplicationPaths appPaths, IDbConnector dbConnector) : base(logManager, dbConnector) diff --git a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs index c0f184bef..1c811ed11 100644 --- a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs +++ b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs @@ -114,6 +114,7 @@ using Emby.Server.Implementations.Playlists; using Emby.Server.Implementations.Security; using Emby.Server.Implementations.ServerManager; using Emby.Server.Implementations.Session; +using Emby.Server.Implementations.Social; using Emby.Server.Implementations.Sync; using Emby.Server.Implementations.TV; using Emby.Server.Implementations.Updates; From 24532f3e2d7de2542b664383c5e0bc0cce61d7fc Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 8 Nov 2016 23:58:58 -0500 Subject: [PATCH 2/4] update power management --- .../Emby.Server.Implementations.csproj | 1 + .../EntryPoints/KeepServerAwake.cs | 65 ++++++++++++ .../Social/SharingManager.cs | 100 ++++++++++++++++++ .../Configuration/ServerConfiguration.cs | 11 +- MediaBrowser.Model/MediaBrowser.Model.csproj | 1 + .../Social/ISharingRepository.cs | 15 +++ MediaBrowser.Model/System/IPowerManagement.cs | 9 ++ .../MediaBrowser.Server.Mono.csproj | 1 + .../Native/BaseMonoApp.cs | 10 -- .../Native/PowerManagement.cs | 15 +++ MediaBrowser.Server.Mono/Program.cs | 2 +- .../ApplicationHost.cs | 28 +++-- .../EntryPoints/KeepServerAwake.cs | 60 ----------- .../INativeApp.cs | 7 -- .../MediaBrowser.Server.Startup.Common.csproj | 2 - .../Threading/PeriodicTimer.cs | 72 ------------- MediaBrowser.ServerApplication/MainStartup.cs | 5 +- .../MediaBrowser.ServerApplication.csproj | 1 + .../Native/PowerManagement.cs | 17 +++ .../Native/WindowsApp.cs | 10 -- 20 files changed, 254 insertions(+), 178 deletions(-) create mode 100644 Emby.Server.Implementations/EntryPoints/KeepServerAwake.cs create mode 100644 Emby.Server.Implementations/Social/SharingManager.cs create mode 100644 MediaBrowser.Model/Social/ISharingRepository.cs create mode 100644 MediaBrowser.Model/System/IPowerManagement.cs create mode 100644 MediaBrowser.Server.Mono/Native/PowerManagement.cs delete mode 100644 MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs delete mode 100644 MediaBrowser.Server.Startup.Common/Threading/PeriodicTimer.cs create mode 100644 MediaBrowser.ServerApplication/Native/PowerManagement.cs diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csproj index f5c157234..7e885b779 100644 --- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj +++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj @@ -51,6 +51,7 @@ + diff --git a/Emby.Server.Implementations/EntryPoints/KeepServerAwake.cs b/Emby.Server.Implementations/EntryPoints/KeepServerAwake.cs new file mode 100644 index 000000000..8ae85e390 --- /dev/null +++ b/Emby.Server.Implementations/EntryPoints/KeepServerAwake.cs @@ -0,0 +1,65 @@ +using MediaBrowser.Controller; +using MediaBrowser.Controller.Plugins; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Logging; +using System; +using System.Linq; +using MediaBrowser.Model.System; +using MediaBrowser.Model.Threading; + +namespace Emby.Server.Implementations.EntryPoints +{ + public class KeepServerAwake : IServerEntryPoint + { + private readonly ISessionManager _sessionManager; + private readonly ILogger _logger; + private ITimer _timer; + private readonly IServerApplicationHost _appHost; + private readonly ITimerFactory _timerFactory; + private readonly IPowerManagement _powerManagement; + + public KeepServerAwake(ISessionManager sessionManager, ILogger logger, IServerApplicationHost appHost, ITimerFactory timerFactory, IPowerManagement powerManagement) + { + _sessionManager = sessionManager; + _logger = logger; + _appHost = appHost; + _timerFactory = timerFactory; + _powerManagement = powerManagement; + } + + public void Run() + { + _timer = _timerFactory.Create(OnTimerCallback, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5)); + } + + private void OnTimerCallback(object state) + { + var now = DateTime.UtcNow; + + try + { + if (_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 15)) + { + _powerManagement.PreventSystemStandby(); + } + else + { + _powerManagement.AllowSystemStandby(); + } + } + catch (Exception ex) + { + _logger.ErrorException("Error resetting system standby timer", ex); + } + } + + public void Dispose() + { + if (_timer != null) + { + _timer.Dispose(); + _timer = null; + } + } + } +} diff --git a/Emby.Server.Implementations/Social/SharingManager.cs b/Emby.Server.Implementations/Social/SharingManager.cs new file mode 100644 index 000000000..54614c879 --- /dev/null +++ b/Emby.Server.Implementations/Social/SharingManager.cs @@ -0,0 +1,100 @@ +using MediaBrowser.Common.Extensions; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Social; +using System; +using System.Threading.Tasks; + +namespace Emby.Server.Implementations.Social +{ + public class SharingManager : ISharingManager + { + private readonly ISharingRepository _repository; + private readonly IServerConfigurationManager _config; + private readonly ILibraryManager _libraryManager; + private readonly IServerApplicationHost _appHost; + + public SharingManager(ISharingRepository repository, IServerConfigurationManager config, ILibraryManager libraryManager, IServerApplicationHost appHost) + { + _repository = repository; + _config = config; + _libraryManager = libraryManager; + _appHost = appHost; + } + + public async Task CreateShare(string itemId, string userId) + { + if (string.IsNullOrWhiteSpace(itemId)) + { + throw new ArgumentNullException("itemId"); + } + if (string.IsNullOrWhiteSpace(userId)) + { + throw new ArgumentNullException("userId"); + } + + var item = _libraryManager.GetItemById(itemId); + + if (item == null) + { + throw new ResourceNotFoundException(); + } + + var externalUrl = (await _appHost.GetSystemInfo().ConfigureAwait(false)).WanAddress; + + if (string.IsNullOrWhiteSpace(externalUrl)) + { + throw new InvalidOperationException("No external server address is currently available."); + } + + var info = new SocialShareInfo + { + Id = Guid.NewGuid().ToString("N"), + ExpirationDate = DateTime.UtcNow.AddDays(_config.Configuration.SharingExpirationDays), + ItemId = itemId, + UserId = userId + }; + + AddShareInfo(info, externalUrl); + + await _repository.CreateShare(info).ConfigureAwait(false); + + return info; + } + + private string GetTitle(BaseItem item) + { + return item.Name; + } + + public SocialShareInfo GetShareInfo(string id) + { + var info = _repository.GetShareInfo(id); + + AddShareInfo(info, _appHost.GetSystemInfo().Result.WanAddress); + + return info; + } + + private void AddShareInfo(SocialShareInfo info, string externalUrl) + { + info.ImageUrl = externalUrl + "/Social/Shares/Public/" + info.Id + "/Image"; + info.Url = externalUrl + "/emby/web/shared.html?id=" + info.Id; + + var item = _libraryManager.GetItemById(info.ItemId); + + if (item != null) + { + info.Overview = item.Overview; + info.Name = GetTitle(item); + } + } + + public Task DeleteShare(string id) + { + return _repository.DeleteShare(id); + } + } +} diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index b1e52dc7b..9715a624f 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -8,6 +8,9 @@ namespace MediaBrowser.Model.Configuration /// public class ServerConfiguration : BaseApplicationConfiguration { + public const int DefaultHttpPort = 8096; + public const int DefaultHttpsPort = 8920; + /// /// Gets or sets a value indicating whether [enable u pn p]. /// @@ -225,10 +228,10 @@ namespace MediaBrowser.Model.Configuration EnableExternalContentInSuggestions = true; ImageSavingConvention = ImageSavingConvention.Compatible; - PublicPort = 8096; - PublicHttpsPort = 8920; - HttpServerPortNumber = 8096; - HttpsPortNumber = 8920; + PublicPort = DefaultHttpPort; + PublicHttpsPort = DefaultHttpsPort; + HttpServerPortNumber = DefaultHttpPort; + HttpsPortNumber = DefaultHttpsPort; EnableHttps = false; EnableDashboardResponseCaching = true; EnableDashboardResourceMinification = true; diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index abf07e1dc..64472ca3a 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -145,6 +145,7 @@ + diff --git a/MediaBrowser.Model/Social/ISharingRepository.cs b/MediaBrowser.Model/Social/ISharingRepository.cs new file mode 100644 index 000000000..069b6e1fe --- /dev/null +++ b/MediaBrowser.Model/Social/ISharingRepository.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MediaBrowser.Model.Social +{ + public interface ISharingRepository + { + Task CreateShare(SocialShareInfo info); + Task DeleteShare(string id); + SocialShareInfo GetShareInfo(string id); + } +} diff --git a/MediaBrowser.Model/System/IPowerManagement.cs b/MediaBrowser.Model/System/IPowerManagement.cs new file mode 100644 index 000000000..91cae0d3e --- /dev/null +++ b/MediaBrowser.Model/System/IPowerManagement.cs @@ -0,0 +1,9 @@ + +namespace MediaBrowser.Model.System +{ + public interface IPowerManagement + { + void PreventSystemStandby(); + void AllowSystemStandby(); + } +} diff --git a/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj b/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj index fcaeb035d..b7dbe4dc7 100644 --- a/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj +++ b/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj @@ -100,6 +100,7 @@ + diff --git a/MediaBrowser.Server.Mono/Native/BaseMonoApp.cs b/MediaBrowser.Server.Mono/Native/BaseMonoApp.cs index 73e2686d2..46605e19f 100644 --- a/MediaBrowser.Server.Mono/Native/BaseMonoApp.cs +++ b/MediaBrowser.Server.Mono/Native/BaseMonoApp.cs @@ -68,16 +68,6 @@ namespace MediaBrowser.Server.Mono.Native get { return false; } } - public void PreventSystemStandby() - { - - } - - public void AllowSystemStandby() - { - - } - public List GetAssembliesWithParts() { var list = new List(); diff --git a/MediaBrowser.Server.Mono/Native/PowerManagement.cs b/MediaBrowser.Server.Mono/Native/PowerManagement.cs new file mode 100644 index 000000000..0fdd4de80 --- /dev/null +++ b/MediaBrowser.Server.Mono/Native/PowerManagement.cs @@ -0,0 +1,15 @@ +using MediaBrowser.Model.System; + +namespace MediaBrowser.Server.Mono.Native +{ + public class PowerManagement : IPowerManagement + { + public void PreventSystemStandby() + { + } + + public void AllowSystemStandby() + { + } + } +} diff --git a/MediaBrowser.Server.Mono/Program.cs b/MediaBrowser.Server.Mono/Program.cs index 215ee4468..662d82ff0 100644 --- a/MediaBrowser.Server.Mono/Program.cs +++ b/MediaBrowser.Server.Mono/Program.cs @@ -80,7 +80,7 @@ namespace MediaBrowser.Server.Mono var nativeApp = new NativeApp(options, logManager.GetLogger("App")); - _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "emby.mono.zip", nativeApp); + _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, nativeApp, new PowerManagement(), "emby.mono.zip"); if (options.ContainsOption("-v")) { diff --git a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs index 1c811ed11..c09e028ea 100644 --- a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs +++ b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs @@ -119,6 +119,7 @@ using Emby.Server.Implementations.Sync; using Emby.Server.Implementations.TV; using Emby.Server.Implementations.Updates; using MediaBrowser.Model.Activity; +using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Globalization; using MediaBrowser.Model.Net; @@ -260,26 +261,24 @@ namespace MediaBrowser.Server.Startup.Common internal INativeApp NativeApp { get; set; } + internal IPowerManagement PowerManagement { get; private set; } + /// /// Initializes a new instance of the class. /// - /// The application paths. - /// The log manager. - /// The options. - /// The file system. - /// The release asset filename. - /// The native application. public ApplicationHost(ServerApplicationPaths applicationPaths, ILogManager logManager, StartupOptions options, IFileSystem fileSystem, - string releaseAssetFilename, - INativeApp nativeApp) + INativeApp nativeApp, + IPowerManagement powerManagement, + string releaseAssetFilename) : base(applicationPaths, logManager, fileSystem) { _startupOptions = options; _releaseAssetFilename = releaseAssetFilename; NativeApp = nativeApp; + PowerManagement = powerManagement; SetBaseExceptionMessage(); } @@ -485,6 +484,13 @@ namespace MediaBrowser.Server.Startup.Common HttpPort = ServerConfigurationManager.Configuration.HttpServerPortNumber; HttpsPort = ServerConfigurationManager.Configuration.HttpsPortNumber; + // Safeguard against invalid configuration + if (HttpPort == HttpsPort) + { + HttpPort = ServerConfiguration.DefaultHttpPort; + HttpsPort = ServerConfiguration.DefaultHttpsPort; + } + return base.Init(progress); } @@ -535,6 +541,8 @@ namespace MediaBrowser.Server.Startup.Common { await base.RegisterResources(progress).ConfigureAwait(false); + RegisterSingleInstance(PowerManagement); + SecurityManager = new PluginSecurityManager(this, HttpClient, JsonSerializer, ApplicationPaths, LogManager, FileSystemManager, CryptographyProvider); RegisterSingleInstance(SecurityManager); @@ -999,13 +1007,13 @@ namespace MediaBrowser.Server.Startup.Common { Logger.ErrorException("Error starting http server", ex); - if (HttpPort == 8096) + if (HttpPort == ServerConfiguration.DefaultHttpPort) { throw; } } - HttpPort = 8096; + HttpPort = ServerConfiguration.DefaultHttpPort; try { diff --git a/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs b/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs deleted file mode 100644 index 95b42afbf..000000000 --- a/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs +++ /dev/null @@ -1,60 +0,0 @@ -using MediaBrowser.Controller; -using MediaBrowser.Controller.Plugins; -using MediaBrowser.Controller.Session; -using MediaBrowser.Model.Logging; -using System; -using System.Linq; -using MediaBrowser.Server.Startup.Common.Threading; - -namespace MediaBrowser.Server.Startup.Common.EntryPoints -{ - public class KeepServerAwake : IServerEntryPoint - { - private readonly ISessionManager _sessionManager; - private readonly ILogger _logger; - private PeriodicTimer _timer; - private readonly IServerApplicationHost _appHost; - - public KeepServerAwake(ISessionManager sessionManager, ILogger logger, IServerApplicationHost appHost) - { - _sessionManager = sessionManager; - _logger = logger; - _appHost = appHost; - } - - public void Run() - { - _timer = new PeriodicTimer(obj => - { - var now = DateTime.UtcNow; - var nativeApp = ((ApplicationHost)_appHost).NativeApp; - - try - { - if (_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 15)) - { - nativeApp.PreventSystemStandby(); - } - else - { - nativeApp.AllowSystemStandby(); - } - } - catch (Exception ex) - { - _logger.ErrorException("Error resetting system standby timer", ex); - } - - }, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5)); - } - - public void Dispose() - { - if (_timer != null) - { - _timer.Dispose(); - _timer = null; - } - } - } -} diff --git a/MediaBrowser.Server.Startup.Common/INativeApp.cs b/MediaBrowser.Server.Startup.Common/INativeApp.cs index c56bb9b4b..df29dfde4 100644 --- a/MediaBrowser.Server.Startup.Common/INativeApp.cs +++ b/MediaBrowser.Server.Startup.Common/INativeApp.cs @@ -88,13 +88,6 @@ namespace MediaBrowser.Server.Startup.Common /// INetworkManager. INetworkManager CreateNetworkManager(ILogger logger); - /// - /// Prevents the system stand by. - /// - void PreventSystemStandby(); - - void AllowSystemStandby(); - FFMpegInstallInfo GetFfmpegInstallInfo(); void LaunchUrl(string url); diff --git a/MediaBrowser.Server.Startup.Common/MediaBrowser.Server.Startup.Common.csproj b/MediaBrowser.Server.Startup.Common/MediaBrowser.Server.Startup.Common.csproj index aca30aa2c..97cdddf26 100644 --- a/MediaBrowser.Server.Startup.Common/MediaBrowser.Server.Startup.Common.csproj +++ b/MediaBrowser.Server.Startup.Common/MediaBrowser.Server.Startup.Common.csproj @@ -75,7 +75,6 @@ - @@ -90,7 +89,6 @@ - diff --git a/MediaBrowser.Server.Startup.Common/Threading/PeriodicTimer.cs b/MediaBrowser.Server.Startup.Common/Threading/PeriodicTimer.cs deleted file mode 100644 index 3e898adfd..000000000 --- a/MediaBrowser.Server.Startup.Common/Threading/PeriodicTimer.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Threading; -using Microsoft.Win32; - -namespace MediaBrowser.Server.Startup.Common.Threading -{ - public class PeriodicTimer : IDisposable - { - public Action Callback { get; set; } - private Timer _timer; - private readonly object _state; - private readonly object _timerLock = new object(); - private readonly TimeSpan _period; - - public PeriodicTimer(Action callback, object state, TimeSpan dueTime, TimeSpan period) - { - if (callback == null) - { - throw new ArgumentNullException("callback"); - } - - Callback = callback; - _period = period; - _state = state; - - StartTimer(dueTime); - } - - void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) - { - if (e.Mode == PowerModes.Resume) - { - DisposeTimer(); - StartTimer(Timeout.InfiniteTimeSpan); - } - } - - private void TimerCallback(object state) - { - Callback(state); - } - - private void StartTimer(TimeSpan dueTime) - { - lock (_timerLock) - { - _timer = new Timer(TimerCallback, _state, dueTime, _period); - - Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; - } - } - - private void DisposeTimer() - { - Microsoft.Win32.SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; - - lock (_timerLock) - { - if (_timer != null) - { - _timer.Dispose(); - _timer = null; - } - } - } - - public void Dispose() - { - DisposeTimer(); - } - } -} diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index 18fa80fe2..a3f409636 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -320,8 +320,9 @@ namespace MediaBrowser.ServerApplication logManager, options, fileSystem, - "emby.windows.zip", - nativeApp); + nativeApp, + new PowerManagement(), + "emby.windows.zip"); var initProgress = new Progress(); diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index a306274e1..666ba640e 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -127,6 +127,7 @@ + diff --git a/MediaBrowser.ServerApplication/Native/PowerManagement.cs b/MediaBrowser.ServerApplication/Native/PowerManagement.cs new file mode 100644 index 000000000..0bd3db1da --- /dev/null +++ b/MediaBrowser.ServerApplication/Native/PowerManagement.cs @@ -0,0 +1,17 @@ +using MediaBrowser.Model.System; + +namespace MediaBrowser.ServerApplication.Native +{ + public class PowerManagement : IPowerManagement + { + public void PreventSystemStandby() + { + MainStartup.Invoke(Standby.PreventSleep); + } + + public void AllowSystemStandby() + { + MainStartup.Invoke(Standby.AllowSleep); + } + } +} diff --git a/MediaBrowser.ServerApplication/Native/WindowsApp.cs b/MediaBrowser.ServerApplication/Native/WindowsApp.cs index cf0dbfbe3..5bed8556e 100644 --- a/MediaBrowser.ServerApplication/Native/WindowsApp.cs +++ b/MediaBrowser.ServerApplication/Native/WindowsApp.cs @@ -139,16 +139,6 @@ namespace MediaBrowser.ServerApplication.Native return new NetworkManager(logger); } - public void PreventSystemStandby() - { - MainStartup.Invoke(Standby.PreventSleep); - } - - public void AllowSystemStandby() - { - MainStartup.Invoke(Standby.AllowSleep); - } - public FFMpegInstallInfo GetFfmpegInstallInfo() { var info = new FFMpegInstallInfo(); From 48a5fa17b034947669d7ce0e81cbe599f628acf9 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 9 Nov 2016 12:24:57 -0500 Subject: [PATCH 3/4] update file saving --- .../IO/ManagedFileSystem.cs | 33 +++++++++++++++++++ MediaBrowser.Controller/Entities/BaseItem.cs | 14 +------- .../Savers/BaseXmlSaver.cs | 5 ++- MediaBrowser.Model/IO/IFileSystem.cs | 1 + MediaBrowser.Providers/Manager/ImageSaver.cs | 4 +++ .../Savers/BaseNfoSaver.cs | 4 +++ 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Emby.Common.Implementations/IO/ManagedFileSystem.cs b/Emby.Common.Implementations/IO/ManagedFileSystem.cs index 5b965efdc..37b457598 100644 --- a/Emby.Common.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Common.Implementations/IO/ManagedFileSystem.cs @@ -419,6 +419,25 @@ namespace Emby.Common.Implementations.IO } } + public void SetReadOnly(string path, bool isReadOnly) + { + var info = GetFileInfo(path); + + if (info.Exists && info.IsReadOnly != isReadOnly) + { + if (isReadOnly) + { + File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly); + } + else + { + FileAttributes attributes = File.GetAttributes(path); + attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly); + File.SetAttributes(path, attributes); + } + } + } + private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove) { return attributes & ~attributesToRemove; @@ -564,6 +583,20 @@ namespace Emby.Common.Implementations.IO public void DeleteFile(string path) { + var fileInfo = GetFileInfo(path); + + if (fileInfo.Exists) + { + if (fileInfo.IsHidden) + { + SetHidden(path, false); + } + if (fileInfo.IsReadOnly) + { + SetReadOnly(path, false); + } + } + File.Delete(path); } diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 433fdbe16..b079da97c 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -1892,19 +1892,7 @@ namespace MediaBrowser.Controller.Entities if (info.IsLocalFile) { - // Delete the source file - var currentFile = FileSystem.GetFileInfo(info.Path); - - // Deletion will fail if the file is hidden so remove the attribute first - if (currentFile.Exists) - { - if (currentFile.IsHidden) - { - FileSystem.SetHidden(currentFile.FullName, false); - } - - FileSystem.DeleteFile(currentFile.FullName); - } + FileSystem.DeleteFile(info.Path); } return UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None); diff --git a/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs b/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs index c489b5728..02c34320b 100644 --- a/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs +++ b/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs @@ -226,9 +226,12 @@ namespace MediaBrowser.LocalMetadata.Savers if (file.IsHidden) { FileSystem.SetHidden(path, false); - wasHidden = true; } + if (file.IsReadOnly) + { + FileSystem.SetReadOnly(path, false); + } } using (var filestream = FileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) diff --git a/MediaBrowser.Model/IO/IFileSystem.cs b/MediaBrowser.Model/IO/IFileSystem.cs index ca537752a..d2bb35520 100644 --- a/MediaBrowser.Model/IO/IFileSystem.cs +++ b/MediaBrowser.Model/IO/IFileSystem.cs @@ -305,6 +305,7 @@ namespace MediaBrowser.Model.IO IEnumerable GetFileSystemEntryPaths(string path, bool recursive = false); void SetHidden(string path, bool isHidden); + void SetReadOnly(string path, bool isHidden); char DirectorySeparatorChar { get; } diff --git a/MediaBrowser.Providers/Manager/ImageSaver.cs b/MediaBrowser.Providers/Manager/ImageSaver.cs index 59d67740d..b59875378 100644 --- a/MediaBrowser.Providers/Manager/ImageSaver.cs +++ b/MediaBrowser.Providers/Manager/ImageSaver.cs @@ -265,6 +265,10 @@ namespace MediaBrowser.Providers.Manager { _fileSystem.SetHidden(file.FullName, false); } + if (file.IsReadOnly) + { + _fileSystem.SetReadOnly(path, false); + } } using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true)) diff --git a/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs b/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs index 168827025..84dd095cd 100644 --- a/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs +++ b/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs @@ -221,6 +221,10 @@ namespace MediaBrowser.XbmcMetadata.Savers wasHidden = true; } + if (file.IsReadOnly) + { + FileSystem.SetReadOnly(path, false); + } } using (var filestream = FileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) From c1ae3ec2ce803b16fa9ecc0981865aa7c9be172b Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 9 Nov 2016 12:25:33 -0500 Subject: [PATCH 4/4] update recording analyze duration --- Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs b/Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs index 5d70d0383..5e55b893f 100644 --- a/Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs +++ b/Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs @@ -177,6 +177,11 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV inputModifiers = "-ss " + _mediaEncoder.GetTimeParameter(startTimeTicks) + " " + inputModifiers; } + var analyzeDurationSeconds = 5; + var analyzeDuration = " -analyzeduration " + + (analyzeDurationSeconds * 1000000).ToString(CultureInfo.InvariantCulture); + inputModifiers += analyzeDuration; + commandLineArgs = string.Format(commandLineArgs, inputTempFile, targetFile, videoArgs, GetAudioArgs(mediaSource), durationParam); return inputModifiers + " " + commandLineArgs;