Year.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. public class Year : BaseItem, IItemByName
  14. {
  15. public override List<string> GetUserDataKeys()
  16. {
  17. var list = base.GetUserDataKeys();
  18. list.Insert(0, "Year-" + Name);
  19. return list;
  20. }
  21. /// <summary>
  22. /// Gets the folder containing the item.
  23. /// If the item is a folder, it returns the folder itself.
  24. /// </summary>
  25. /// <value>The containing folder path.</value>
  26. [JsonIgnore]
  27. public override string ContainingFolderPath => Path;
  28. public override double GetDefaultPrimaryImageAspectRatio()
  29. {
  30. double value = 2;
  31. value /= 3;
  32. return value;
  33. }
  34. [JsonIgnore]
  35. public override bool SupportsAncestors => false;
  36. public override bool CanDelete()
  37. {
  38. return false;
  39. }
  40. public override bool IsSaveLocalMetadataEnabled()
  41. {
  42. return true;
  43. }
  44. public IList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  45. {
  46. var usCulture = new CultureInfo("en-US");
  47. if (!int.TryParse(Name, NumberStyles.Integer, usCulture, out var year))
  48. {
  49. return new List<BaseItem>();
  50. }
  51. query.Years = new[] { year };
  52. return LibraryManager.GetItemList(query);
  53. }
  54. public int? GetYearValue()
  55. {
  56. if (int.TryParse(Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
  57. {
  58. return year;
  59. }
  60. return null;
  61. }
  62. [JsonIgnore]
  63. public override bool SupportsPeople => false;
  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. }