From 6e5d2aadaa17d891b9c4b4f17faf1316f43aaee6 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Fri, 28 Dec 2018 16:48:26 +0100 Subject: Remove custom ToArray extension --- Emby.Server.Implementations/Library/LibraryManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Emby.Server.Implementations/Library/LibraryManager.cs') diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 17516d6b9..91e58bfcd 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -1493,7 +1493,7 @@ namespace Emby.Server.Implementations.Library return new QueryResult { - Items = list.ToArray(list.Count) + Items = list.ToArray() }; } -- cgit v1.2.3 From de7fcaadb3d8fa77802ddcfb9bbd5b9306c49224 Mon Sep 17 00:00:00 2001 From: Erwin de Haan Date: Sun, 30 Dec 2018 01:12:33 +0100 Subject: Fixed #320 by adding an extension method to BaseItem to make a deep copy of an object. --- .../Library/LibraryManager.cs | 9 +++++--- .../Entities/BaseItemExtensions.cs | 27 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) (limited to 'Emby.Server.Implementations/Library/LibraryManager.cs') diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 91e58bfcd..f25688bba 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -730,8 +730,10 @@ namespace Emby.Server.Implementations.Library _fileSystem.CreateDirectory(rootFolderPath); - var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? (AggregateFolder)ResolvePath(_fileSystem.GetDirectoryInfo(rootFolderPath)); - + var tmpAFolder = new AggregateFolder(); + ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(rootFolderPath))).DeepCopy(tmpAFolder); + var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? tmpAFolder; + // In case program data folder was moved if (!string.Equals(rootFolder.Path, rootFolderPath, StringComparison.Ordinal)) { @@ -799,7 +801,8 @@ namespace Emby.Server.Implementations.Library if (tmpItem == null) { - tmpItem = (UserRootFolder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath)); + tmpItem = new UserRootFolder(); + ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath))).DeepCopy(tmpItem); } // In case program data folder was moved diff --git a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs index c56a370a8..97e56d1fd 100644 --- a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs +++ b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -61,5 +62,31 @@ namespace MediaBrowser.Controller.Entities item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file)); } } + + /// + /// Copies all properties on object. Skips properties that do not exist. + /// + /// The source object. + /// The destination object. + public static void DeepCopy(this T source, TU dest) + { + var sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList(); + var destProps = typeof(TU).GetProperties() + .Where(x => x.CanWrite) + .ToList(); + + foreach (var sourceProp in sourceProps) + { + if (destProps.Any(x => x.Name == sourceProp.Name)) + { + var p = destProps.First(x => x.Name == sourceProp.Name); + p.SetValue(dest, sourceProp.GetValue(source, null), null); + } + + } + + } + + } } -- cgit v1.2.3 From 9c1c29325d0c5693f36059b60954e2af2834205e Mon Sep 17 00:00:00 2001 From: Erwin de Haan Date: Sun, 30 Dec 2018 01:24:30 +0100 Subject: Added extra extension method that create a new instance of the destination object. --- Emby.Server.Implementations/Library/LibraryManager.cs | 9 +++------ MediaBrowser.Controller/Entities/BaseItemExtensions.cs | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'Emby.Server.Implementations/Library/LibraryManager.cs') diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index f25688bba..bd823e0c1 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -730,10 +730,8 @@ namespace Emby.Server.Implementations.Library _fileSystem.CreateDirectory(rootFolderPath); - var tmpAFolder = new AggregateFolder(); - ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(rootFolderPath))).DeepCopy(tmpAFolder); - var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? tmpAFolder; - + var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(rootFolderPath))).DeepCopy(); + // In case program data folder was moved if (!string.Equals(rootFolder.Path, rootFolderPath, StringComparison.Ordinal)) { @@ -801,8 +799,7 @@ namespace Emby.Server.Implementations.Library if (tmpItem == null) { - tmpItem = new UserRootFolder(); - ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath))).DeepCopy(tmpItem); + tmpItem = ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath))).DeepCopy(); } // In case program data folder was moved diff --git a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs index 97e56d1fd..8ab1788b5 100644 --- a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs +++ b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs @@ -68,7 +68,9 @@ namespace MediaBrowser.Controller.Entities /// /// The source object. /// The destination object. - public static void DeepCopy(this T source, TU dest) + public static void DeepCopy(this T source, TU dest) + where T : BaseItem + where TU : BaseItem { var sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList(); var destProps = typeof(TU).GetProperties() @@ -87,6 +89,19 @@ namespace MediaBrowser.Controller.Entities } + /// + /// Copies all properties on newly created object. Skips properties that do not exist. + /// + /// The source object. + public static TU DeepCopy(this T source) + where T : BaseItem + where TU : BaseItem, new() + { + var dest = new TU(); + source.DeepCopy(dest); + return dest; + } + } } -- cgit v1.2.3