EntityResolutionHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.IO;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using MediaBrowser.Controller.Library;
  8. namespace MediaBrowser.Controller.Resolvers
  9. {
  10. /// <summary>
  11. /// Class EntityResolutionHelper
  12. /// </summary>
  13. public static class EntityResolutionHelper
  14. {
  15. /// <summary>
  16. /// Any extension in this list is considered a video file - can be added to at runtime for extensibility
  17. /// </summary>
  18. public static List<string> VideoFileExtensions = new List<string>
  19. {
  20. ".mkv",
  21. ".m2t",
  22. ".m2ts",
  23. ".img",
  24. ".iso",
  25. ".ts",
  26. ".rmvb",
  27. ".mov",
  28. ".avi",
  29. ".mpg",
  30. ".mpeg",
  31. ".wmv",
  32. ".mp4",
  33. ".divx",
  34. ".dvr-ms",
  35. ".wtv",
  36. ".ogm",
  37. ".ogv",
  38. ".asf",
  39. ".m4v",
  40. ".flv",
  41. ".f4v",
  42. ".3gp",
  43. ".webm"
  44. };
  45. /// <summary>
  46. /// The audio file extensions
  47. /// </summary>
  48. private static readonly string[] AudioFileExtensions = new[] {
  49. ".mp3",
  50. ".flac",
  51. ".wma",
  52. ".aac",
  53. ".acc",
  54. ".m4a",
  55. ".m4b",
  56. ".wav",
  57. ".ape",
  58. ".ogg",
  59. ".oga"
  60. };
  61. /// <summary>
  62. /// Determines whether [is audio file] [the specified args].
  63. /// </summary>
  64. /// <param name="path">The path.</param>
  65. /// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
  66. public static bool IsAudioFile(string path)
  67. {
  68. return AudioFileExtensions.Contains(Path.GetExtension(path), StringComparer.OrdinalIgnoreCase);
  69. }
  70. /// <summary>
  71. /// Determines whether [is video file] [the specified path].
  72. /// </summary>
  73. /// <param name="path">The path.</param>
  74. /// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
  75. public static bool IsVideoFile(string path)
  76. {
  77. var extension = Path.GetExtension(path) ?? String.Empty;
  78. return VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  79. }
  80. /// <summary>
  81. /// Ensures DateCreated and DateModified have values
  82. /// </summary>
  83. /// <param name="item">The item.</param>
  84. /// <param name="args">The args.</param>
  85. public static void EnsureDates(BaseItem item, ItemResolveArgs args)
  86. {
  87. if (!Path.IsPathRooted(item.Path))
  88. {
  89. return;
  90. }
  91. // See if a different path came out of the resolver than what went in
  92. if (!args.Path.Equals(item.Path, StringComparison.OrdinalIgnoreCase))
  93. {
  94. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  95. if (childData != null)
  96. {
  97. item.DateCreated = childData.CreationTimeUtc;
  98. item.DateModified = childData.LastWriteTimeUtc;
  99. }
  100. else
  101. {
  102. var fileData = FileSystem.GetFileSystemInfo(item.Path);
  103. if (fileData.Exists)
  104. {
  105. item.DateCreated = fileData.CreationTimeUtc;
  106. item.DateModified = fileData.LastWriteTimeUtc;
  107. }
  108. }
  109. }
  110. else
  111. {
  112. item.DateCreated = args.FileInfo.CreationTimeUtc;
  113. item.DateModified = args.FileInfo.LastWriteTimeUtc;
  114. }
  115. }
  116. }
  117. }