Year.cs 3.5 KB

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