The Free Software Media System - Server Backend & API
Fixed #320 by adding an extension method to BaseItem to make a deep copy of an object.
Erwin de Haande7fcaadb3d8
diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.csindex 91e58bfcde..f25688bba3 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<Folder,AggregateFolder>(tmpAFolder);+ var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? tmpAFolder;+// In case program data folder was movedif (!string.Equals(rootFolder.Path, rootFolderPath, StringComparison.Ordinal)){@@ -799,7 +801,8 @@ namespace Emby.Server.Implementations.Libraryif (tmpItem == null){- tmpItem = (UserRootFolder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath));+ tmpItem = new UserRootFolder();+ ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath))).DeepCopy<Folder,UserRootFolder>(tmpItem);}// In case program data folder was moveddiff --git a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs b/MediaBrowser.Controller/Entities/BaseItemExtensions.csindex c56a370a86..97e56d1fd0 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.Entitiesitem.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file));}}++ /// <summary>+ /// Copies all properties on object. Skips properties that do not exist.+ /// </summary>+ /// <param name="source">The source object.</param>+ /// <param name="dest">The destination object.</param>+ public static void DeepCopy<T, TU>(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);+ }++ }++ }++}}