forked from jellyfin/jellyfin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemResolveArgs.cs
286 lines (245 loc) · 9.77 KB
/
ItemResolveArgs.cs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Linq;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Controller.Library
{
/// <summary>
/// These are arguments relating to the file system that are collected once and then referred to
/// whenever needed. Primarily for entity resolution.
/// </summary>
public class ItemResolveArgs
{
/// <summary>
/// The _app paths.
/// </summary>
private readonly IServerApplicationPaths _appPaths;
private readonly ILibraryManager _libraryManager;
private LibraryOptions _libraryOptions;
/// <summary>
/// Initializes a new instance of the <see cref="ItemResolveArgs" /> class.
/// </summary>
/// <param name="appPaths">The app paths.</param>
/// <param name="libraryManager">The library manager.</param>
public ItemResolveArgs(IServerApplicationPaths appPaths, ILibraryManager libraryManager)
{
_appPaths = appPaths;
_libraryManager = libraryManager;
}
/// <summary>
/// Gets or sets the file system children.
/// </summary>
/// <value>The file system children.</value>
public FileSystemMetadata[] FileSystemChildren { get; set; }
public LibraryOptions LibraryOptions
{
get => _libraryOptions ??= Parent is null ? new LibraryOptions() : _libraryManager.GetLibraryOptions(Parent);
set => _libraryOptions = value;
}
/// <summary>
/// Gets or sets the parent.
/// </summary>
/// <value>The parent.</value>
public Folder Parent { get; set; }
/// <summary>
/// Gets or sets the file info.
/// </summary>
/// <value>The file info.</value>
public FileSystemMetadata FileInfo { get; set; }
/// <summary>
/// Gets the path.
/// </summary>
/// <value>The path.</value>
public string Path => FileInfo.FullName;
/// <summary>
/// Gets a value indicating whether this instance is directory.
/// </summary>
/// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
public bool IsDirectory => FileInfo.IsDirectory;
/// <summary>
/// Gets a value indicating whether this instance is vf.
/// </summary>
/// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value>
public bool IsVf
{
// we should be considered a virtual folder if we are a child of one of the children of the system root folder.
// this is a bit of a trick to determine that... the directory name of a sub-child of the root will start with
// the root but not be equal to it
get
{
if (!IsDirectory)
{
return false;
}
var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty;
return parentDir.Length > _appPaths.RootFolderPath.Length
&& parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);
}
}
/// <summary>
/// Gets a value indicating whether this instance is physical root.
/// </summary>
/// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
public bool IsPhysicalRoot => IsDirectory && BaseItem.FileSystem.AreEqual(Path, _appPaths.RootFolderPath);
/// <summary>
/// Gets or sets the additional locations.
/// </summary>
/// <value>The additional locations.</value>
private List<string> AdditionalLocations { get; set; }
/// <summary>
/// Gets the physical locations.
/// </summary>
/// <value>The physical locations.</value>
public string[] PhysicalLocations
{
get
{
var paths = string.IsNullOrEmpty(Path) ? Array.Empty<string>() : [Path];
return AdditionalLocations is null ? paths : [..paths, ..AdditionalLocations];
}
}
public CollectionType? CollectionType { get; set; }
public bool HasParent<T>()
where T : Folder
{
var parent = Parent;
if (parent is not null)
{
var item = parent as T;
// Just in case the user decided to nest episodes.
// Not officially supported but in some cases we can handle it.
if (item is null)
{
var parents = parent.GetParents();
foreach (var currentParent in parents)
{
if (currentParent is T)
{
return true;
}
}
}
return item is not null;
}
return false;
}
/// <summary>
/// Determines whether the specified <see cref="object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The object to compare with the current object.</param>
/// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
return Equals(obj as ItemResolveArgs);
}
/// <summary>
/// Adds the additional location.
/// </summary>
/// <param name="path">The path.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c> or empty.</exception>
public void AddAdditionalLocation(string path)
{
ArgumentException.ThrowIfNullOrEmpty(path);
AdditionalLocations ??= new List<string>();
AdditionalLocations.Add(path);
}
// REVIEW: @bond
/// <summary>
/// Gets the name of the file system entry by.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>FileSystemInfo.</returns>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
public FileSystemMetadata GetFileSystemEntryByName(string name)
{
ArgumentException.ThrowIfNullOrEmpty(name);
return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
}
/// <summary>
/// Gets the file system entry by path.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>FileSystemInfo.</returns>
/// <exception cref="ArgumentNullException">Throws if path is invalid.</exception>
public FileSystemMetadata GetFileSystemEntryByPath(string path)
{
ArgumentException.ThrowIfNullOrEmpty(path);
foreach (var file in FileSystemChildren)
{
if (string.Equals(file.FullName, path, StringComparison.Ordinal))
{
return file;
}
}
return null;
}
/// <summary>
/// Determines whether [contains file system entry by name] [the specified name].
/// </summary>
/// <param name="name">The name.</param>
/// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
public bool ContainsFileSystemEntryByName(string name)
{
return GetFileSystemEntryByName(name) is not null;
}
public CollectionType? GetCollectionType()
{
return CollectionType;
}
/// <summary>
/// Gets the configured content type for the path.
/// </summary>
/// <returns>The configured content type.</returns>
public CollectionType? GetConfiguredContentType()
{
return _libraryManager.GetConfiguredContentType(Path);
}
/// <summary>
/// Gets the file system children that do not hit the ignore file check.
/// </summary>
/// <returns>The file system children that are not ignored.</returns>
public IEnumerable<FileSystemMetadata> GetActualFileSystemChildren()
{
var numberOfChildren = FileSystemChildren.Length;
for (var i = 0; i < numberOfChildren; i++)
{
var child = FileSystemChildren[i];
if (_libraryManager.IgnoreFile(child, Parent))
{
continue;
}
yield return child;
}
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode()
{
return Path.GetHashCode(StringComparison.Ordinal);
}
/// <summary>
/// Equals the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns><c>true</c> if the arguments are the same, <c>false</c> otherwise.</returns>
protected bool Equals(ItemResolveArgs args)
{
if (args is not null)
{
if (args.Path is null && Path is null)
{
return true;
}
return args.Path is not null && BaseItem.FileSystem.AreEqual(args.Path, Path);
}
return false;
}
}
}