Audio.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using MediaBrowser.Controller.Providers;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.Dto;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.MediaInfo;
  6. using MediaBrowser.Model.Users;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Runtime.Serialization;
  11. namespace MediaBrowser.Controller.Entities.Audio
  12. {
  13. /// <summary>
  14. /// Class Audio
  15. /// </summary>
  16. public class Audio : BaseItem,
  17. IHasAlbumArtist,
  18. IHasArtist,
  19. IHasMusicGenres,
  20. IHasLookupInfo<SongInfo>,
  21. IHasTags,
  22. IHasMediaSources,
  23. IThemeMedia,
  24. IArchivable
  25. {
  26. public long? Size { get; set; }
  27. public string Container { get; set; }
  28. public int? TotalBitrate { get; set; }
  29. public ExtraType? ExtraType { get; set; }
  30. /// <summary>
  31. /// Gets or sets the artist.
  32. /// </summary>
  33. /// <value>The artist.</value>
  34. public List<string> Artists { get; set; }
  35. public List<string> AlbumArtists { get; set; }
  36. /// <summary>
  37. /// Gets or sets the album.
  38. /// </summary>
  39. /// <value>The album.</value>
  40. public string Album { get; set; }
  41. [IgnoreDataMember]
  42. public bool IsThemeMedia
  43. {
  44. get
  45. {
  46. return ExtraType.HasValue && ExtraType.Value == Model.Entities.ExtraType.ThemeSong;
  47. }
  48. }
  49. public Audio()
  50. {
  51. Artists = new List<string>();
  52. AlbumArtists = new List<string>();
  53. }
  54. [IgnoreDataMember]
  55. public override bool SupportsAddingToPlaylist
  56. {
  57. get { return LocationType == LocationType.FileSystem && RunTimeTicks.HasValue; }
  58. }
  59. [IgnoreDataMember]
  60. protected override bool SupportsOwnedItems
  61. {
  62. get
  63. {
  64. return false;
  65. }
  66. }
  67. [IgnoreDataMember]
  68. public override Folder LatestItemsIndexContainer
  69. {
  70. get
  71. {
  72. return AlbumEntity;
  73. }
  74. }
  75. [IgnoreDataMember]
  76. public bool IsArchive
  77. {
  78. get
  79. {
  80. if (string.IsNullOrWhiteSpace(Path))
  81. {
  82. return false;
  83. }
  84. var ext = System.IO.Path.GetExtension(Path) ?? string.Empty;
  85. return new[] { ".zip", ".rar", ".7z" }.Contains(ext, StringComparer.OrdinalIgnoreCase);
  86. }
  87. }
  88. public override bool CanDownload()
  89. {
  90. var locationType = LocationType;
  91. return locationType != LocationType.Remote &&
  92. locationType != LocationType.Virtual;
  93. }
  94. [IgnoreDataMember]
  95. public List<string> AllArtists
  96. {
  97. get
  98. {
  99. var list = AlbumArtists.ToList();
  100. list.AddRange(Artists);
  101. return list;
  102. }
  103. }
  104. [IgnoreDataMember]
  105. public MusicAlbum AlbumEntity
  106. {
  107. get { return FindParent<MusicAlbum>(); }
  108. }
  109. /// <summary>
  110. /// Gets the type of the media.
  111. /// </summary>
  112. /// <value>The type of the media.</value>
  113. [IgnoreDataMember]
  114. public override string MediaType
  115. {
  116. get
  117. {
  118. return Model.Entities.MediaType.Audio;
  119. }
  120. }
  121. /// <summary>
  122. /// Creates the name of the sort.
  123. /// </summary>
  124. /// <returns>System.String.</returns>
  125. protected override string CreateSortName()
  126. {
  127. return (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  128. + (IndexNumber != null ? IndexNumber.Value.ToString("0000 - ") : "") + Name;
  129. }
  130. /// <summary>
  131. /// Gets the user data key.
  132. /// </summary>
  133. /// <returns>System.String.</returns>
  134. protected override string CreateUserDataKey()
  135. {
  136. var parent = AlbumEntity;
  137. if (parent != null)
  138. {
  139. var parentKey = parent.GetUserDataKey();
  140. if (IndexNumber.HasValue)
  141. {
  142. var songKey = (ParentIndexNumber != null ? ParentIndexNumber.Value.ToString("0000 - ") : "")
  143. + (IndexNumber.Value.ToString("0000 - "));
  144. return parentKey + songKey;
  145. }
  146. }
  147. return base.CreateUserDataKey();
  148. }
  149. public override UnratedItem GetBlockUnratedType()
  150. {
  151. return UnratedItem.Music;
  152. }
  153. public SongInfo GetLookupInfo()
  154. {
  155. var info = GetItemLookupInfo<SongInfo>();
  156. info.AlbumArtists = AlbumArtists;
  157. info.Album = Album;
  158. info.Artists = Artists;
  159. return info;
  160. }
  161. public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
  162. {
  163. var result = new List<MediaSourceInfo>
  164. {
  165. GetVersionInfo(this, enablePathSubstitution)
  166. };
  167. return result;
  168. }
  169. private static MediaSourceInfo GetVersionInfo(Audio i, bool enablePathSubstituion)
  170. {
  171. var locationType = i.LocationType;
  172. var info = new MediaSourceInfo
  173. {
  174. Id = i.Id.ToString("N"),
  175. Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
  176. MediaStreams = MediaSourceManager.GetMediaStreams(i.Id).ToList(),
  177. Name = i.Name,
  178. Path = enablePathSubstituion ? GetMappedPath(i.Path, locationType) : i.Path,
  179. RunTimeTicks = i.RunTimeTicks,
  180. Container = i.Container,
  181. Size = i.Size
  182. };
  183. if (string.IsNullOrEmpty(info.Container))
  184. {
  185. if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
  186. {
  187. info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
  188. }
  189. }
  190. var bitrate = i.TotalBitrate ??
  191. info.MediaStreams.Where(m => m.Type == MediaStreamType.Audio)
  192. .Select(m => m.BitRate ?? 0)
  193. .Sum();
  194. if (bitrate > 0)
  195. {
  196. info.Bitrate = bitrate;
  197. }
  198. return info;
  199. }
  200. }
  201. }