Browse Source

add ApplicationPath to app paths interface to hide implementation

Luke Pulverenti 11 năm trước cách đây
mục cha
commit
4e79eaf65e

+ 11 - 4
MediaBrowser.Common.Implementations/BaseApplicationHost.cs

@@ -33,7 +33,7 @@ namespace MediaBrowser.Common.Implementations
     /// </summary>
     /// <typeparam name="TApplicationPathsType">The type of the T application paths type.</typeparam>
     public abstract class BaseApplicationHost<TApplicationPathsType> : IApplicationHost
-        where TApplicationPathsType : class, IApplicationPaths, new()
+        where TApplicationPathsType : class, IApplicationPaths
     {
         /// <summary>
         /// Occurs when [has pending restart changed].
@@ -83,7 +83,7 @@ namespace MediaBrowser.Common.Implementations
         /// <summary>
         /// The json serializer
         /// </summary>
-        public readonly IJsonSerializer JsonSerializer = new JsonSerializer();
+        public IJsonSerializer JsonSerializer { get; private set; }
 
         /// <summary>
         /// The _XML serializer
@@ -153,7 +153,7 @@ namespace MediaBrowser.Common.Implementations
         protected IInstallationManager InstallationManager { get; private set; }
 
         protected IFileSystem FileSystemManager { get; private set; }
-        
+
         /// <summary>
         /// Gets or sets the zip client.
         /// </summary>
@@ -181,6 +181,8 @@ namespace MediaBrowser.Common.Implementations
         /// <returns>Task.</returns>
         public virtual async Task Init()
         {
+            JsonSerializer = CreateJsonSerializer();
+
             IsFirstRun = !ConfigurationManager.CommonConfiguration.IsStartupWizardCompleted;
 
             Logger = LogManager.GetLogger("App");
@@ -212,6 +214,11 @@ namespace MediaBrowser.Common.Implementations
 
         }
 
+        protected virtual IJsonSerializer CreateJsonSerializer()
+        {
+            return new JsonSerializer();
+        }
+
         private void SetHttpLimit()
         {
             try
@@ -224,7 +231,7 @@ namespace MediaBrowser.Common.Implementations
                 Logger.ErrorException("Error setting http limit", ex);
             }
         }
-        
+
         /// <summary>
         /// Installs the iso mounters.
         /// </summary>

+ 6 - 4
MediaBrowser.Common.Implementations/BaseApplicationPaths.cs

@@ -20,21 +20,23 @@ namespace MediaBrowser.Common.Implementations
         /// <summary>
         /// Initializes a new instance of the <see cref="BaseApplicationPaths" /> class.
         /// </summary>
-        /// <param name="useDebugPath">if set to <c>true</c> [use debug paths].</param>
-        protected BaseApplicationPaths(bool useDebugPath)
+        protected BaseApplicationPaths(bool useDebugPath, string applicationPath)
         {
             _useDebugPath = useDebugPath;
+            ApplicationPath = applicationPath;
         }
 
         /// <summary>
         /// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class.
         /// </summary>
-        /// <param name="programDataPath">The program data path.</param>
-        protected BaseApplicationPaths(string programDataPath)
+        protected BaseApplicationPaths(string programDataPath, string applicationPath)
         {
             _programDataPath = programDataPath;
+            ApplicationPath = applicationPath;
         }
 
+        public string ApplicationPath { get; private set; }
+
         /// <summary>
         /// The _program data path
         /// </summary>

+ 2 - 2
MediaBrowser.Common.Implementations/Serialization/JsonSerializer.cs

@@ -80,7 +80,7 @@ namespace MediaBrowser.Common.Implementations.Serialization
 
             using (Stream stream = File.OpenRead(file))
             {
-                return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
+                return DeserializeFromStream(stream, type);
             }
         }
 
@@ -101,7 +101,7 @@ namespace MediaBrowser.Common.Implementations.Serialization
 
             using (Stream stream = File.OpenRead(file))
             {
-                return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
+                return DeserializeFromStream<T>(stream);
             }
         }
 

+ 6 - 0
MediaBrowser.Common/Configuration/IApplicationPaths.cs

@@ -6,6 +6,12 @@ namespace MediaBrowser.Common.Configuration
     /// </summary>
     public interface IApplicationPaths
     {
+        /// <summary>
+        /// Gets the application path.
+        /// </summary>
+        /// <value>The application path.</value>
+        string ApplicationPath { get; }
+
         /// <summary>
         /// Gets the path to the program data folder
         /// </summary>

+ 16 - 0
MediaBrowser.Controller/Entities/TV/Season.cs

@@ -94,6 +94,22 @@ namespace MediaBrowser.Controller.Entities.TV
             get { return _series ?? (_series = FindParent<Series>()); }
         }
 
+        [IgnoreDataMember]
+        public string SeriesPath
+        {
+            get
+            {
+                var series = Series;
+
+                if (series != null)
+                {
+                    return series.Path;
+                }
+
+                return System.IO.Path.GetDirectoryName(Path);
+            }
+        }
+
         /// <summary>
         /// Our rating comes from our series
         /// </summary>

+ 14 - 1
MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs

@@ -99,6 +99,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer
             return result;
         }
 
+        private bool SupportsCompression
+        {
+            get
+            {
+                return true;
+            }
+        }
+
         /// <summary>
         /// Gets the optimized result.
         /// </summary>
@@ -116,7 +124,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
                 throw new ArgumentNullException("result");
             }
 
-            var optimizedResult = requestContext.ToOptimizedResult(result);
+            var optimizedResult = SupportsCompression ? requestContext.ToOptimizedResult(result) : result;
 
             if (responseHeaders != null)
             {
@@ -458,6 +466,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer
                 }
             }
 
+            if (!SupportsCompression)
+            {
+                return new HttpResult(content, contentType);
+            }
+
             var contents = content.Compress(requestContext.CompressionType);
 
             return new CompressedResult(contents, requestContext.CompressionType, contentType);

+ 10 - 3
MediaBrowser.Server.Implementations/HttpServer/SwaggerService.cs

@@ -1,6 +1,6 @@
-using MediaBrowser.Common.Net;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Net;
 using ServiceStack.ServiceHost;
-using System.Diagnostics;
 using System.IO;
 
 namespace MediaBrowser.Server.Implementations.HttpServer
@@ -20,6 +20,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer
 
     public class SwaggerService : IHasResultFactory, IRestfulService
     {
+        private readonly IApplicationPaths _appPaths;
+
+        public SwaggerService(IApplicationPaths appPaths)
+        {
+            _appPaths = appPaths;
+        }
+
         /// <summary>
         /// Gets the specified request.
         /// </summary>
@@ -27,7 +34,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
         /// <returns>System.Object.</returns>
         public object Get(GetSwaggerResource request)
         {
-            var runningDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
+            var runningDirectory = Path.GetDirectoryName(_appPaths.ApplicationPath);
 
             var swaggerDirectory = Path.Combine(runningDirectory, "swagger-ui");
 

+ 29 - 10
MediaBrowser.Server.Implementations/Providers/ImageSaver.cs

@@ -6,6 +6,7 @@ using MediaBrowser.Controller.Entities.TV;
 using MediaBrowser.Controller.IO;
 using MediaBrowser.Model.Configuration;
 using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Logging;
 using System;
 using System.Collections.Generic;
 using System.Globalization;
@@ -13,7 +14,6 @@ using System.IO;
 using System.Linq;
 using System.Threading;
 using System.Threading.Tasks;
-using MediaBrowser.Model.Logging;
 
 namespace MediaBrowser.Server.Implementations.Providers
 {
@@ -89,6 +89,23 @@ namespace MediaBrowser.Server.Implementations.Providers
             if (locationType == LocationType.Remote || locationType == LocationType.Virtual)
             {
                 saveLocally = false;
+
+                var season = item as Season;
+
+                // If season is virtual under a physical series, save locally if using compatible convention
+                if (season != null && _config.Configuration.ImageSavingConvention == ImageSavingConvention.Compatible)
+                {
+                    var series = season.Series;
+
+                    if (series != null)
+                    {
+                        var seriesLocationType = series.LocationType;
+                        if (seriesLocationType == LocationType.FileSystem || seriesLocationType == LocationType.Offline)
+                        {
+                            saveLocally = true;
+                        }
+                    }
+                }
             }
 
             if (type == ImageType.Backdrop && imageIndex == null)
@@ -402,7 +419,7 @@ namespace MediaBrowser.Server.Implementations.Providers
             return path;
         }
 
-        private string GetBackdropSaveFilename(List<string> images, string zeroIndexFilename, string numberedIndexPrefix, int index)
+        private string GetBackdropSaveFilename(IEnumerable<string> images, string zeroIndexFilename, string numberedIndexPrefix, int index)
         {
             if (index == 0)
             {
@@ -431,6 +448,8 @@ namespace MediaBrowser.Server.Implementations.Providers
         /// <exception cref="System.ArgumentNullException">imageIndex</exception>
         private string[] GetCompatibleSavePaths(BaseItem item, ImageType type, int? imageIndex, string mimeType)
         {
+            var season = item as Season;
+
             var extension = mimeType.Split('/').Last();
 
             if (string.Equals(extension, "jpeg", StringComparison.OrdinalIgnoreCase))
@@ -449,9 +468,9 @@ namespace MediaBrowser.Server.Implementations.Providers
 
                 if (imageIndex.Value == 0)
                 {
-                    if (item is Season && item.IndexNumber.HasValue)
+                    if (season != null && item.IndexNumber.HasValue)
                     {
-                        var seriesFolder = Path.GetDirectoryName(item.Path);
+                        var seriesFolder = season.SeriesPath;
 
                         var seasonMarker = item.IndexNumber.Value == 0
                                                ? "-specials"
@@ -481,9 +500,9 @@ namespace MediaBrowser.Server.Implementations.Providers
 
             if (type == ImageType.Primary)
             {
-                if (item is Season && item.IndexNumber.HasValue)
+                if (season != null && item.IndexNumber.HasValue)
                 {
-                    var seriesFolder = Path.GetDirectoryName(item.Path);
+                    var seriesFolder = season.SeriesPath;
 
                     var seasonMarker = item.IndexNumber.Value == 0
                                            ? "-specials"
@@ -518,9 +537,9 @@ namespace MediaBrowser.Server.Implementations.Providers
 
             if (type == ImageType.Banner)
             {
-                if (item is Season && item.IndexNumber.HasValue)
+                if (season != null && item.IndexNumber.HasValue)
                 {
-                    var seriesFolder = Path.GetDirectoryName(item.Path);
+                    var seriesFolder = season.SeriesPath;
 
                     var seasonMarker = item.IndexNumber.Value == 0
                                            ? "-specials"
@@ -534,9 +553,9 @@ namespace MediaBrowser.Server.Implementations.Providers
 
             if (type == ImageType.Thumb)
             {
-                if (item is Season && item.IndexNumber.HasValue)
+                if (season != null && item.IndexNumber.HasValue)
                 {
-                    var seriesFolder = Path.GetDirectoryName(item.Path);
+                    var seriesFolder = season.SeriesPath;
 
                     var seasonMarker = item.IndexNumber.Value == 0
                                            ? "-specials"

+ 6 - 7
MediaBrowser.Server.Implementations/ServerApplicationPaths.cs

@@ -13,16 +13,16 @@ namespace MediaBrowser.Server.Implementations
         /// <summary>
         /// Initializes a new instance of the <see cref="ServerApplicationPaths" /> class.
         /// </summary>
-        public ServerApplicationPaths()
-            : base(true)
+        public ServerApplicationPaths(string applicationPath)
+            : base(true, applicationPath)
         {
         }
 #else
 /// <summary>
 /// Initializes a new instance of the <see cref="ServerApplicationPaths"/> class.
 /// </summary>
-        public ServerApplicationPaths()
-            : base(false)
+        public ServerApplicationPaths(string applicationPath)
+            : base(false, applicationPath)
         {
         }
 #endif
@@ -30,9 +30,8 @@ namespace MediaBrowser.Server.Implementations
         /// <summary>
         /// Initializes a new instance of the <see cref="BaseApplicationPaths" /> class.
         /// </summary>
-        /// <param name="programDataPath">The program data path.</param>
-        public ServerApplicationPaths(string programDataPath)
-            : base(programDataPath)
+        public ServerApplicationPaths(string programDataPath, string applicationPath)
+            : base(programDataPath, applicationPath)
         {
 
         }

+ 5 - 3
MediaBrowser.Server.Mono/Program.cs

@@ -36,7 +36,9 @@ namespace MediaBrowser.Server.Mono
 		{
 			Application.Init ();
 
-			var appPaths = CreateApplicationPaths();
+			var applicationPath = Assembly.GetEntryAssembly ().Location;
+
+			var appPaths = CreateApplicationPaths(applicationPath);
 
 			var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
 			logManager.ReloadLogger(LogSeverity.Info);
@@ -65,9 +67,9 @@ namespace MediaBrowser.Server.Mono
 			}
 		}
 
-		private static ServerApplicationPaths CreateApplicationPaths()
+		private static ServerApplicationPaths CreateApplicationPaths(string applicationPath)
 		{
-			return new ServerApplicationPaths();
+			return new ServerApplicationPaths(applicationPath);
 		}
 
 		/// <summary>

+ 1 - 0
MediaBrowser.ServerApplication/ApplicationHost.cs

@@ -27,6 +27,7 @@ using MediaBrowser.Controller.Session;
 using MediaBrowser.Controller.Sorting;
 using MediaBrowser.Model.Logging;
 using MediaBrowser.Model.MediaInfo;
+using MediaBrowser.Model.Serialization;
 using MediaBrowser.Model.System;
 using MediaBrowser.Model.Updates;
 using MediaBrowser.Providers;

+ 3 - 1
MediaBrowser.ServerApplication/MainStartup.cs

@@ -158,7 +158,9 @@ namespace MediaBrowser.ServerApplication
                 return new ServerApplicationPaths(programDataPath);
             }
 
-            return new ServerApplicationPaths();
+            var applicationPath = Process.GetCurrentProcess().MainModule.FileName;
+
+            return new ServerApplicationPaths(applicationPath);
         }
 
         /// <summary>

+ 1 - 3
MediaBrowser.WebDashboard/Api/DashboardService.cs

@@ -5,7 +5,6 @@ using MediaBrowser.Common.ScheduledTasks;
 using MediaBrowser.Controller;
 using MediaBrowser.Controller.Configuration;
 using MediaBrowser.Controller.Dto;
-using MediaBrowser.Controller.IO;
 using MediaBrowser.Controller.Plugins;
 using MediaBrowser.Controller.Session;
 using MediaBrowser.Model.Logging;
@@ -13,7 +12,6 @@ using MediaBrowser.Model.Tasks;
 using ServiceStack.ServiceHost;
 using System;
 using System.Collections.Generic;
-using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Reflection;
@@ -151,7 +149,7 @@ namespace MediaBrowser.WebDashboard.Api
                     return _serverConfigurationManager.Configuration.DashboardSourcePath;
                 }
 
-                var runningDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
+                var runningDirectory = Path.GetDirectoryName(_serverConfigurationManager.ApplicationPaths.ApplicationPath);
 
                 return Path.Combine(runningDirectory, "dashboard-ui");
             }