forked from jsoref/jellyfin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseItem.cs
2590 lines (2131 loc) · 80.8 KB
/
BaseItem.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#nullable disable
#pragma warning disable CS1591, SA1401
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using Jellyfin.Extensions;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Library;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Controller.Entities
{
/// <summary>
/// Class BaseItem.
/// </summary>
public abstract class BaseItem : IHasProviderIds, IHasLookupInfo<ItemLookupInfo>, IEquatable<BaseItem>
{
private BaseItemKind? _baseItemKind;
public const string ThemeSongFileName = "theme";
/// <summary>
/// The supported image extensions.
/// </summary>
public static readonly string[] SupportedImageExtensions
= new[] { ".png", ".jpg", ".jpeg", ".webp", ".tbn", ".gif" };
private static readonly List<string> _supportedExtensions = new List<string>(SupportedImageExtensions)
{
".nfo",
".xml",
".srt",
".vtt",
".sub",
".sup",
".idx",
".txt",
".edl",
".bif",
".smi",
".ttml"
};
/// <summary>
/// Extra types that should be counted and displayed as "Special Features" in the UI.
/// </summary>
public static readonly IReadOnlyCollection<ExtraType> DisplayExtraTypes = new HashSet<ExtraType>
{
Model.Entities.ExtraType.Unknown,
Model.Entities.ExtraType.BehindTheScenes,
Model.Entities.ExtraType.Clip,
Model.Entities.ExtraType.DeletedScene,
Model.Entities.ExtraType.Interview,
Model.Entities.ExtraType.Sample,
Model.Entities.ExtraType.Scene,
Model.Entities.ExtraType.Featurette,
Model.Entities.ExtraType.Short
};
private string _sortName;
private string _forcedSortName;
private string _name;
public const char SlugChar = '-';
protected BaseItem()
{
Tags = Array.Empty<string>();
Genres = Array.Empty<string>();
Studios = Array.Empty<string>();
ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
LockedFields = Array.Empty<MetadataField>();
ImageInfos = Array.Empty<ItemImageInfo>();
ProductionLocations = Array.Empty<string>();
RemoteTrailers = Array.Empty<MediaUrl>();
ExtraIds = Array.Empty<Guid>();
}
[JsonIgnore]
public string PreferredMetadataCountryCode { get; set; }
[JsonIgnore]
public string PreferredMetadataLanguage { get; set; }
public long? Size { get; set; }
public string Container { get; set; }
[JsonIgnore]
public string Tagline { get; set; }
[JsonIgnore]
public virtual ItemImageInfo[] ImageInfos { get; set; }
[JsonIgnore]
public bool IsVirtualItem { get; set; }
/// <summary>
/// Gets or sets the album.
/// </summary>
/// <value>The album.</value>
[JsonIgnore]
public string Album { get; set; }
/// <summary>
/// Gets or sets the LUFS value.
/// </summary>
/// <value>The LUFS Value.</value>
[JsonIgnore]
public float LUFS { get; set; }
/// <summary>
/// Gets or sets the channel identifier.
/// </summary>
/// <value>The channel identifier.</value>
[JsonIgnore]
public Guid ChannelId { get; set; }
[JsonIgnore]
public virtual bool SupportsAddingToPlaylist => false;
[JsonIgnore]
public virtual bool AlwaysScanInternalMetadataPath => false;
/// <summary>
/// Gets or sets a value indicating whether this instance is in mixed folder.
/// </summary>
/// <value><c>true</c> if this instance is in mixed folder; otherwise, <c>false</c>.</value>
[JsonIgnore]
public bool IsInMixedFolder { get; set; }
[JsonIgnore]
public virtual bool SupportsPlayedStatus => false;
[JsonIgnore]
public virtual bool SupportsPositionTicksResume => false;
[JsonIgnore]
public virtual bool SupportsRemoteImageDownloading => true;
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[JsonIgnore]
public virtual string Name
{
get => _name;
set
{
_name = value;
// lazy load this again
_sortName = null;
}
}
[JsonIgnore]
public bool IsUnaired => PremiereDate.HasValue && PremiereDate.Value.ToLocalTime().Date >= DateTime.Now.Date;
[JsonIgnore]
public int? TotalBitrate { get; set; }
[JsonIgnore]
public ExtraType? ExtraType { get; set; }
[JsonIgnore]
public bool IsThemeMedia => ExtraType.HasValue && (ExtraType.Value == Model.Entities.ExtraType.ThemeSong || ExtraType.Value == Model.Entities.ExtraType.ThemeVideo);
[JsonIgnore]
public string OriginalTitle { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[JsonIgnore]
public Guid Id { get; set; }
[JsonIgnore]
public Guid OwnerId { get; set; }
/// <summary>
/// Gets or sets the audio.
/// </summary>
/// <value>The audio.</value>
[JsonIgnore]
public ProgramAudio? Audio { get; set; }
/// <summary>
/// Gets the id that should be used to key display prefs for this item.
/// Default is based on the type for everything except actual generic folders.
/// </summary>
/// <value>The display prefs id.</value>
[JsonIgnore]
public virtual Guid DisplayPreferencesId
{
get
{
var thisType = GetType();
return thisType == typeof(Folder) ? Id : thisType.FullName.GetMD5();
}
}
/// <summary>
/// Gets or sets the path.
/// </summary>
/// <value>The path.</value>
[JsonIgnore]
public virtual string Path { get; set; }
[JsonIgnore]
public virtual SourceType SourceType
{
get
{
if (!ChannelId.IsEmpty())
{
return SourceType.Channel;
}
return SourceType.Library;
}
}
/// <summary>
/// Gets the folder containing the item.
/// If the item is a folder, it returns the folder itself.
/// </summary>
[JsonIgnore]
public virtual string ContainingFolderPath
{
get
{
if (IsFolder)
{
return Path;
}
return System.IO.Path.GetDirectoryName(Path);
}
}
/// <summary>
/// Gets or sets the name of the service.
/// </summary>
/// <value>The name of the service.</value>
[JsonIgnore]
public string ServiceName { get; set; }
/// <summary>
/// Gets or sets the external id.
/// </summary>
/// <remarks>
/// If this content came from an external service, the id of the content on that service.
/// </remarks>
[JsonIgnore]
public string ExternalId { get; set; }
[JsonIgnore]
public string ExternalSeriesId { get; set; }
[JsonIgnore]
public virtual bool IsHidden => false;
/// <summary>
/// Gets the type of the location.
/// </summary>
/// <value>The type of the location.</value>
[JsonIgnore]
public virtual LocationType LocationType
{
get
{
var path = Path;
if (string.IsNullOrEmpty(path))
{
if (SourceType == SourceType.Channel)
{
return LocationType.Remote;
}
return LocationType.Virtual;
}
return FileSystem.IsPathFile(path) ? LocationType.FileSystem : LocationType.Remote;
}
}
[JsonIgnore]
public MediaProtocol? PathProtocol
{
get
{
var path = Path;
if (string.IsNullOrEmpty(path))
{
return null;
}
return MediaSourceManager.GetPathProtocol(path);
}
}
[JsonIgnore]
public bool IsFileProtocol => PathProtocol == MediaProtocol.File;
[JsonIgnore]
public bool HasPathProtocol => PathProtocol.HasValue;
[JsonIgnore]
public virtual bool SupportsLocalMetadata
{
get
{
if (SourceType == SourceType.Channel)
{
return false;
}
return IsFileProtocol;
}
}
[JsonIgnore]
public virtual string FileNameWithoutExtension
{
get
{
if (IsFileProtocol)
{
return System.IO.Path.GetFileNameWithoutExtension(Path);
}
return null;
}
}
[JsonIgnore]
public virtual bool EnableAlphaNumericSorting => true;
public virtual bool IsHD => Height >= 720;
public bool IsShortcut { get; set; }
public string ShortcutPath { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public Guid[] ExtraIds { get; set; }
/// <summary>
/// Gets the primary image path.
/// </summary>
/// <remarks>
/// This is just a helper for convenience.
/// </remarks>
/// <value>The primary image path.</value>
[JsonIgnore]
public string PrimaryImagePath => this.GetImagePath(ImageType.Primary);
/// <summary>
/// Gets or sets the date created.
/// </summary>
/// <value>The date created.</value>
[JsonIgnore]
public DateTime DateCreated { get; set; }
/// <summary>
/// Gets or sets the date modified.
/// </summary>
/// <value>The date modified.</value>
[JsonIgnore]
public DateTime DateModified { get; set; }
public DateTime DateLastSaved { get; set; }
[JsonIgnore]
public DateTime DateLastRefreshed { get; set; }
[JsonIgnore]
public bool IsLocked { get; set; }
/// <summary>
/// Gets or sets the locked fields.
/// </summary>
/// <value>The locked fields.</value>
[JsonIgnore]
public MetadataField[] LockedFields { get; set; }
/// <summary>
/// Gets the type of the media.
/// </summary>
/// <value>The type of the media.</value>
[JsonIgnore]
public virtual MediaType MediaType => MediaType.Unknown;
[JsonIgnore]
public virtual string[] PhysicalLocations
{
get
{
if (!IsFileProtocol)
{
return Array.Empty<string>();
}
return new[] { Path };
}
}
[JsonIgnore]
public bool EnableMediaSourceDisplay
{
get
{
if (SourceType == SourceType.Channel)
{
return ChannelManager.EnableMediaSourceDisplay(this);
}
return true;
}
}
[JsonIgnore]
public Guid ParentId { get; set; }
/// <summary>
/// Gets or sets the logger.
/// </summary>
public static ILogger<BaseItem> Logger { get; set; }
public static ILibraryManager LibraryManager { get; set; }
public static IServerConfigurationManager ConfigurationManager { get; set; }
public static IProviderManager ProviderManager { get; set; }
public static ILocalizationManager LocalizationManager { get; set; }
public static IItemRepository ItemRepository { get; set; }
public static IFileSystem FileSystem { get; set; }
public static IUserDataManager UserDataManager { get; set; }
public static IChannelManager ChannelManager { get; set; }
public static IMediaSourceManager MediaSourceManager { get; set; }
/// <summary>
/// Gets or sets the name of the forced sort.
/// </summary>
/// <value>The name of the forced sort.</value>
[JsonIgnore]
public string ForcedSortName
{
get => _forcedSortName;
set
{
_forcedSortName = value;
_sortName = null;
}
}
/// <summary>
/// Gets or sets the name of the sort.
/// </summary>
/// <value>The name of the sort.</value>
[JsonIgnore]
public string SortName
{
get
{
if (_sortName is null)
{
if (!string.IsNullOrEmpty(ForcedSortName))
{
// Need the ToLower because that's what CreateSortName does
_sortName = ModifySortChunks(ForcedSortName).ToLowerInvariant();
}
else
{
_sortName = CreateSortName();
}
}
return _sortName;
}
set => _sortName = value;
}
[JsonIgnore]
public virtual Guid DisplayParentId => ParentId;
[JsonIgnore]
public BaseItem DisplayParent
{
get
{
var id = DisplayParentId;
if (id.IsEmpty())
{
return null;
}
return LibraryManager.GetItemById(id);
}
}
/// <summary>
/// Gets or sets the date that the item first debuted. For movies this could be premiere date, episodes would be first aired.
/// </summary>
/// <value>The premiere date.</value>
[JsonIgnore]
public DateTime? PremiereDate { get; set; }
/// <summary>
/// Gets or sets the end date.
/// </summary>
/// <value>The end date.</value>
[JsonIgnore]
public DateTime? EndDate { get; set; }
/// <summary>
/// Gets or sets the official rating.
/// </summary>
/// <value>The official rating.</value>
[JsonIgnore]
public string OfficialRating { get; set; }
[JsonIgnore]
public int? InheritedParentalRatingValue { get; set; }
/// <summary>
/// Gets or sets the critic rating.
/// </summary>
/// <value>The critic rating.</value>
[JsonIgnore]
public float? CriticRating { get; set; }
/// <summary>
/// Gets or sets the custom rating.
/// </summary>
/// <value>The custom rating.</value>
[JsonIgnore]
public string CustomRating { get; set; }
/// <summary>
/// Gets or sets the overview.
/// </summary>
/// <value>The overview.</value>
[JsonIgnore]
public string Overview { get; set; }
/// <summary>
/// Gets or sets the studios.
/// </summary>
/// <value>The studios.</value>
[JsonIgnore]
public string[] Studios { get; set; }
/// <summary>
/// Gets or sets the genres.
/// </summary>
/// <value>The genres.</value>
[JsonIgnore]
public string[] Genres { get; set; }
/// <summary>
/// Gets or sets the tags.
/// </summary>
/// <value>The tags.</value>
[JsonIgnore]
public string[] Tags { get; set; }
[JsonIgnore]
public string[] ProductionLocations { get; set; }
/// <summary>
/// Gets or sets the home page URL.
/// </summary>
/// <value>The home page URL.</value>
[JsonIgnore]
public string HomePageUrl { get; set; }
/// <summary>
/// Gets or sets the community rating.
/// </summary>
/// <value>The community rating.</value>
[JsonIgnore]
public float? CommunityRating { get; set; }
/// <summary>
/// Gets or sets the run time ticks.
/// </summary>
/// <value>The run time ticks.</value>
[JsonIgnore]
public long? RunTimeTicks { get; set; }
/// <summary>
/// Gets or sets the production year.
/// </summary>
/// <value>The production year.</value>
[JsonIgnore]
public int? ProductionYear { get; set; }
/// <summary>
/// Gets or sets the index number. If the item is part of a series, this is it's number in the series.
/// This could be episode number, album track number, etc.
/// </summary>
/// <value>The index number.</value>
[JsonIgnore]
public int? IndexNumber { get; set; }
/// <summary>
/// Gets or sets the parent index number. For an episode this could be the season number, or for a song this could be the disc number.
/// </summary>
/// <value>The parent index number.</value>
[JsonIgnore]
public int? ParentIndexNumber { get; set; }
[JsonIgnore]
public virtual bool HasLocalAlternateVersions => false;
[JsonIgnore]
public string OfficialRatingForComparison
{
get
{
var officialRating = OfficialRating;
if (!string.IsNullOrEmpty(officialRating))
{
return officialRating;
}
var parent = DisplayParent;
if (parent is not null)
{
return parent.OfficialRatingForComparison;
}
return null;
}
}
[JsonIgnore]
public string CustomRatingForComparison
{
get
{
var customRating = CustomRating;
if (!string.IsNullOrEmpty(customRating))
{
return customRating;
}
var parent = DisplayParent;
if (parent is not null)
{
return parent.CustomRatingForComparison;
}
return null;
}
}
/// <summary>
/// Gets or sets the provider ids.
/// </summary>
/// <value>The provider ids.</value>
[JsonIgnore]
public Dictionary<string, string> ProviderIds { get; set; }
[JsonIgnore]
public virtual Folder LatestItemsIndexContainer => null;
[JsonIgnore]
public string PresentationUniqueKey { get; set; }
[JsonIgnore]
public virtual bool EnableRememberingTrackSelections => true;
[JsonIgnore]
public virtual bool IsTopParent
{
get
{
if (this is BasePluginFolder || this is Channel)
{
return true;
}
if (this is IHasCollectionType view)
{
if (view.CollectionType == CollectionType.livetv)
{
return true;
}
}
if (GetParent() is AggregateFolder)
{
return true;
}
return false;
}
}
[JsonIgnore]
public virtual bool SupportsAncestors => true;
[JsonIgnore]
public virtual bool StopRefreshIfLocalMetadataFound => true;
[JsonIgnore]
protected virtual bool SupportsOwnedItems => !ParentId.IsEmpty() && IsFileProtocol;
[JsonIgnore]
public virtual bool SupportsPeople => false;
[JsonIgnore]
public virtual bool SupportsThemeMedia => false;
[JsonIgnore]
public virtual bool SupportsInheritedParentImages => false;
/// <summary>
/// Gets a value indicating whether this instance is folder.
/// </summary>
/// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
[JsonIgnore]
public virtual bool IsFolder => false;
[JsonIgnore]
public virtual bool IsDisplayedAsFolder => false;
/// <summary>
/// Gets or sets the remote trailers.
/// </summary>
/// <value>The remote trailers.</value>
public IReadOnlyList<MediaUrl> RemoteTrailers { get; set; }
public virtual double GetDefaultPrimaryImageAspectRatio()
{
return 0;
}
public virtual string CreatePresentationUniqueKey()
{
return Id.ToString("N", CultureInfo.InvariantCulture);
}
public virtual bool CanDelete()
{
if (SourceType == SourceType.Channel)
{
return ChannelManager.CanDelete(this);
}
return IsFileProtocol;
}
public virtual bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders)
{
if (user.HasPermission(PermissionKind.EnableContentDeletion))
{
return true;
}
var allowed = user.GetPreferenceValues<Guid>(PreferenceKind.EnableContentDeletionFromFolders);
if (SourceType == SourceType.Channel)
{
return allowed.Contains(ChannelId);
}
var collectionFolders = LibraryManager.GetCollectionFolders(this, allCollectionFolders);
foreach (var folder in collectionFolders)
{
if (allowed.Contains(folder.Id))
{
return true;
}
}
return false;
}
public BaseItem GetOwner()
{
var ownerId = OwnerId;
return ownerId.IsEmpty() ? null : LibraryManager.GetItemById(ownerId);
}
public bool CanDelete(User user, List<Folder> allCollectionFolders)
{
return CanDelete() && IsAuthorizedToDelete(user, allCollectionFolders);
}
public bool CanDelete(User user)
{
var allCollectionFolders = LibraryManager.GetUserRootFolder().Children.OfType<Folder>().ToList();
return CanDelete(user, allCollectionFolders);
}
public virtual bool CanDownload()
{
return false;
}
public virtual bool IsAuthorizedToDownload(User user)
{
return user.HasPermission(PermissionKind.EnableContentDownloading);
}
public bool CanDownload(User user)
{
return CanDownload() && IsAuthorizedToDownload(user);
}
/// <inheritdoc />
public override string ToString()
{
return Name;
}
public virtual string GetInternalMetadataPath()
{
var basePath = ConfigurationManager.ApplicationPaths.InternalMetadataPath;
return GetInternalMetadataPath(basePath);
}
protected virtual string GetInternalMetadataPath(string basePath)
{
if (SourceType == SourceType.Channel)
{
return System.IO.Path.Join(basePath, "channels", ChannelId.ToString("N", CultureInfo.InvariantCulture), Id.ToString("N", CultureInfo.InvariantCulture));
}
ReadOnlySpan<char> idString = Id.ToString("N", CultureInfo.InvariantCulture);
return System.IO.Path.Join(basePath, "library", idString[..2], idString);
}
/// <summary>
/// Creates the name of the sort.
/// </summary>
/// <returns>System.String.</returns>
protected virtual string CreateSortName()
{
if (Name is null)
{
return null; // some items may not have name filled in properly
}
if (!EnableAlphaNumericSorting)
{
return Name.TrimStart();
}
var sortable = Name.Trim().ToLowerInvariant();
foreach (var search in ConfigurationManager.Configuration.SortRemoveWords)
{
// Remove from beginning if a space follows
if (sortable.StartsWith(search + " ", StringComparison.Ordinal))
{
sortable = sortable.Remove(0, search.Length + 1);
}
// Remove from middle if surrounded by spaces
sortable = sortable.Replace(" " + search + " ", " ", StringComparison.Ordinal);
// Remove from end if followed by a space
if (sortable.EndsWith(" " + search, StringComparison.Ordinal))
{
sortable = sortable.Remove(sortable.Length - (search.Length + 1));
}
}
foreach (var removeChar in ConfigurationManager.Configuration.SortRemoveCharacters)
{
sortable = sortable.Replace(removeChar, string.Empty, StringComparison.Ordinal);
}
foreach (var replaceChar in ConfigurationManager.Configuration.SortReplaceCharacters)
{
sortable = sortable.Replace(replaceChar, " ", StringComparison.Ordinal);
}
return ModifySortChunks(sortable);
}
internal static string ModifySortChunks(ReadOnlySpan<char> name)
{
static void AppendChunk(StringBuilder builder, bool isDigitChunk, ReadOnlySpan<char> chunk)
{
if (isDigitChunk && chunk.Length < 10)
{
builder.Append('0', 10 - chunk.Length);
}
builder.Append(chunk);
}
if (name.IsEmpty)
{
return string.Empty;
}
var builder = new StringBuilder(name.Length);
int chunkStart = 0;
bool isDigitChunk = char.IsDigit(name[0]);
for (int i = 0; i < name.Length; i++)
{
var isDigit = char.IsDigit(name[i]);
if (isDigit != isDigitChunk)
{
AppendChunk(builder, isDigitChunk, name.Slice(chunkStart, i - chunkStart));
chunkStart = i;
isDigitChunk = isDigit;
}
}
AppendChunk(builder, isDigitChunk, name.Slice(chunkStart));
// logger.LogDebug("ModifySortChunks Start: {0} End: {1}", name, builder.ToString());
return builder.ToString().RemoveDiacritics();
}
public BaseItem GetParent()
{
var parentId = ParentId;
if (parentId.IsEmpty())
{
return null;
}
return LibraryManager.GetItemById(parentId);
}
public IEnumerable<BaseItem> GetParents()
{
var parent = GetParent();
while (parent is not null)
{
yield return parent;
parent = parent.GetParent();
}
}
/// <summary>
/// Finds a parent of a given type.
/// </summary>
/// <typeparam name="T">Type of parent.</typeparam>
/// <returns>``0.</returns>
public T FindParent<T>()
where T : Folder
{
foreach (var parent in GetParents())
{