Year.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Text.Json.Serialization;
  7. using Microsoft.Extensions.Logging;
  8. namespace MediaBrowser.Controller.Entities
  9. {
  10. /// <summary>
  11. /// Class Year.
  12. /// </summary>
  13. [Common.RequiresSourceSerialisation]
  14. public class Year : BaseItem, IItemByName
  15. {
  16. [JsonIgnore]
  17. public override bool SupportsAncestors => false;
  18. [JsonIgnore]
  19. public override bool SupportsPeople => false;
  20. /// <summary>
  21. /// Gets the folder containing the item.
  22. /// If the item is a folder, it returns the folder itself.
  23. /// </summary>
  24. /// <value>The containing folder path.</value>
  25. [JsonIgnore]
  26. public override string ContainingFolderPath => Path;
  27. public override bool CanDelete()
  28. {
  29. return false;
  30. }
  31. public override List<string> GetUserDataKeys()
  32. {
  33. var list = base.GetUserDataKeys();
  34. list.Insert(0, "Year-" + Name);
  35. return list;
  36. }
  37. public override double GetDefaultPrimaryImageAspectRatio()
  38. {
  39. double value = 2;
  40. value /= 3;
  41. return value;
  42. }
  43. public override bool IsSaveLocalMetadataEnabled()
  44. {
  45. return true;
  46. }
  47. public IReadOnlyList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  48. {
  49. if (!int.TryParse(Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
  50. {
  51. return new List<BaseItem>();
  52. }
  53. query.Years = new[] { year };
  54. return LibraryManager.GetItemList(query);
  55. }
  56. public int? GetYearValue()
  57. {
  58. if (int.TryParse(Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
  59. {
  60. return year;
  61. }
  62. return null;
  63. }
  64. public static string GetPath(string name)
  65. {
  66. return GetPath(name, true);
  67. }
  68. public static string GetPath(string name, bool normalizeName)
  69. {
  70. // Trim the period at the end because windows will have a hard time with that
  71. var validName = normalizeName ?
  72. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  73. name;
  74. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.YearPath, validName);
  75. }
  76. private string GetRebasedPath()
  77. {
  78. return GetPath(System.IO.Path.GetFileName(Path), false);
  79. }
  80. public override bool RequiresRefresh()
  81. {
  82. var newPath = GetRebasedPath();
  83. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  84. {
  85. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  86. return true;
  87. }
  88. return base.RequiresRefresh();
  89. }
  90. /// <summary>
  91. /// This is called before any metadata refresh and returns true if changes were made.
  92. /// </summary>
  93. /// <param name="replaceAllMetadata">Whether to replace all metadata.</param>
  94. /// <returns>true if the item has change, else false.</returns>
  95. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  96. {
  97. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  98. var newPath = GetRebasedPath();
  99. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  100. {
  101. Path = newPath;
  102. hasChanges = true;
  103. }
  104. return hasChanges;
  105. }
  106. }
  107. }