1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
#pragma warning disable CS1591
using System;
using System.Linq;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Controller.Entities
{
public static class BaseItemExtensions
{
/// <summary>
/// Gets the image path.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="imageType">Type of the image.</param>
/// <returns>System.String.</returns>
public static string GetImagePath(this BaseItem item, ImageType imageType)
{
return item.GetImagePath(imageType, 0);
}
public static bool HasImage(this BaseItem item, ImageType imageType)
{
return item.HasImage(imageType, 0);
}
/// <summary>
/// Sets the image path.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="imageType">Type of the image.</param>
/// <param name="file">The file.</param>
public static void SetImagePath(this BaseItem item, ImageType imageType, FileSystemMetadata file)
{
item.SetImagePath(imageType, 0, file);
}
/// <summary>
/// Sets the image path.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="imageType">Type of the image.</param>
/// <param name="file">The file.</param>
public static void SetImagePath(this BaseItem item, ImageType imageType, string file)
{
if (file.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
item.SetImage(
new ItemImageInfo
{
Path = file,
Type = imageType
},
0);
}
else
{
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>
/// <typeparam name="T">Source type.</typeparam>
/// <typeparam name="TU">Destination type.</typeparam>
public static void DeepCopy<T, TU>(this T source, TU dest)
where T : BaseItem
where TU : BaseItem
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(dest);
var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList();
foreach (var sourceProp in typeof(T).GetProperties())
{
// We should be able to write to the property
// for both the source and destination type
// This is only false when the derived type hides the base member
// (which we shouldn't copy anyway)
if (!sourceProp.CanRead || !sourceProp.CanWrite)
{
continue;
}
var v = sourceProp.GetValue(source);
if (v is null)
{
continue;
}
var p = destProps.Find(x => x.Name == sourceProp.Name);
p?.SetValue(dest, v);
}
}
/// <summary>
/// Copies all properties on newly created object. Skips properties that do not exist.
/// </summary>
/// <param name="source">The source object.</param>
/// <typeparam name="T">Source type.</typeparam>
/// <typeparam name="TU">Destination type.</typeparam>
/// <returns>Destination object.</returns>
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;
}
/// <summary>
/// Determines if the item has changed.
/// </summary>
/// <param name="source">The source object.</param>
/// <param name="asOf">The timestamp to detect changes as of.</param>
/// <typeparam name="T">Source type.</typeparam>
/// <returns>Whether the item has changed.</returns>
public static bool HasChanged<T>(this T source, DateTime asOf)
where T : BaseItem
{
ArgumentNullException.ThrowIfNull(source);
return source.DateModified.Subtract(asOf).Duration().TotalSeconds > 1;
}
}
}
|