aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua M. Boniface <joshua@boniface.me>2018-12-29 19:40:53 -0500
committerGitHub <noreply@github.com>2018-12-29 19:40:53 -0500
commit9d91fa8ab8bb1e429d45cf2c0fe54f59c59dff4d (patch)
treeebf3a5f911228a57ecefb01dfef6bf45e19fd153
parentaac1007bdc0167c83ef150d199c27cfaed467e44 (diff)
parent64a1a7560ee09b1568e65def65fe51bf84357726 (diff)
Merge pull request #321 from EraYaN/fix-issue-320
Fixed #320 by adding an extension method to BaseItem to make a deep copy of an object.
-rw-r--r--CONTRIBUTORS.md1
-rw-r--r--Emby.Server.Implementations/Library/LibraryManager.cs4
-rw-r--r--MediaBrowser.Controller/Entities/BaseItemExtensions.cs42
3 files changed, 45 insertions, 2 deletions
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index d19b484b5..467ba1522 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -4,6 +4,7 @@
- [nvllsvm](https://github.com/nvllsvm)
- [JustAMan](https://github.com/JustAMan)
- [dcrdev](https://github.com/dcrdev)
+ - [EraYaN](https://github.com/EraYaN)
- [flemse](https://github.com/flemse)
- [bfayers](https://github.com/bfayers)
diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs
index 91e58bfcd..bd823e0c1 100644
--- a/Emby.Server.Implementations/Library/LibraryManager.cs
+++ b/Emby.Server.Implementations/Library/LibraryManager.cs
@@ -730,7 +730,7 @@ namespace Emby.Server.Implementations.Library
_fileSystem.CreateDirectory(rootFolderPath);
- var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? (AggregateFolder)ResolvePath(_fileSystem.GetDirectoryInfo(rootFolderPath));
+ var rootFolder = GetItemById(GetNewItemId(rootFolderPath, typeof(AggregateFolder))) as AggregateFolder ?? ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(rootFolderPath))).DeepCopy<Folder,AggregateFolder>();
// In case program data folder was moved
if (!string.Equals(rootFolder.Path, rootFolderPath, StringComparison.Ordinal))
@@ -799,7 +799,7 @@ namespace Emby.Server.Implementations.Library
if (tmpItem == null)
{
- tmpItem = (UserRootFolder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath));
+ tmpItem = ((Folder)ResolvePath(_fileSystem.GetDirectoryInfo(userRootPath))).DeepCopy<Folder,UserRootFolder>();
}
// In case program data folder was moved
diff --git a/MediaBrowser.Controller/Entities/BaseItemExtensions.cs b/MediaBrowser.Controller/Entities/BaseItemExtensions.cs
index c56a370a8..8ab1788b5 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,46 @@ namespace MediaBrowser.Controller.Entities
item.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)
+ where T : BaseItem
+ where TU : BaseItem
+ {
+ 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);
+ }
+
+ }
+
+ }
+
+ /// <summary>
+ /// Copies all properties on newly created object. Skips properties that do not exist.
+ /// </summary>
+ /// <param name="source">The source object.</param>
+ public static TU DeepCopy<T, TU>(this T source)
+ where T : BaseItem
+ where TU : BaseItem, new()
+ {
+ var dest = new TU();
+ source.DeepCopy(dest);
+ return dest;
+ }
+
+
}
}