| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.IO; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Xml; |
| | 7 | | using Jellyfin.Extensions; |
| | 8 | | using MediaBrowser.Controller.Configuration; |
| | 9 | | using MediaBrowser.Controller.Entities; |
| | 10 | | using MediaBrowser.Controller.Entities.Audio; |
| | 11 | | using MediaBrowser.Controller.Library; |
| | 12 | | using MediaBrowser.Model.IO; |
| | 13 | | using Microsoft.Extensions.Logging; |
| | 14 | |
|
| | 15 | | namespace MediaBrowser.XbmcMetadata.Savers |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Nfo saver for albums. |
| | 19 | | /// </summary> |
| | 20 | | public class AlbumNfoSaver : BaseNfoSaver |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="AlbumNfoSaver"/> class. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="fileSystem">The file system.</param> |
| | 26 | | /// <param name="configurationManager">the server configuration manager.</param> |
| | 27 | | /// <param name="libraryManager">The library manager.</param> |
| | 28 | | /// <param name="userManager">The user manager.</param> |
| | 29 | | /// <param name="userDataManager">The user data manager.</param> |
| | 30 | | /// <param name="logger">The logger.</param> |
| | 31 | | public AlbumNfoSaver( |
| | 32 | | IFileSystem fileSystem, |
| | 33 | | IServerConfigurationManager configurationManager, |
| | 34 | | ILibraryManager libraryManager, |
| | 35 | | IUserManager userManager, |
| | 36 | | IUserDataManager userDataManager, |
| | 37 | | ILogger<AlbumNfoSaver> logger) |
| 21 | 38 | | : base(fileSystem, configurationManager, libraryManager, userManager, userDataManager, logger) |
| | 39 | | { |
| 21 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <inheritdoc /> |
| | 43 | | protected override string GetLocalSavePath(BaseItem item) |
| 0 | 44 | | => Path.Combine(item.Path, "album.nfo"); |
| | 45 | |
|
| | 46 | | /// <inheritdoc /> |
| | 47 | | protected override string GetRootElementName(BaseItem item) |
| 0 | 48 | | => "album"; |
| | 49 | |
|
| | 50 | | /// <inheritdoc /> |
| | 51 | | public override bool IsEnabledFor(BaseItem item, ItemUpdateType updateType) |
| 131 | 52 | | => item.SupportsLocalMetadata && item is MusicAlbum && updateType >= MinimumUpdateType; |
| | 53 | |
|
| | 54 | | /// <inheritdoc /> |
| | 55 | | protected override void WriteCustomElements(BaseItem item, XmlWriter writer) |
| | 56 | | { |
| 0 | 57 | | var album = (MusicAlbum)item; |
| | 58 | |
|
| 0 | 59 | | foreach (var artist in album.Artists.Trimmed().OrderBy(artist => artist)) |
| | 60 | | { |
| 0 | 61 | | writer.WriteElementString("artist", artist); |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | foreach (var artist in album.AlbumArtists.Trimmed().OrderBy(artist => artist)) |
| | 65 | | { |
| 0 | 66 | | writer.WriteElementString("albumartist", artist); |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | AddTracks(album.Tracks, writer); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | private void AddTracks(IEnumerable<BaseItem> tracks, XmlWriter writer) |
| | 73 | | { |
| 0 | 74 | | foreach (var track in tracks |
| 0 | 75 | | .OrderBy(i => i.ParentIndexNumber ?? 0) |
| 0 | 76 | | .ThenBy(i => i.IndexNumber ?? 0) |
| 0 | 77 | | .ThenBy(i => SortNameOrName(i)) |
| 0 | 78 | | .ThenBy(i => i.Name?.Trim())) |
| | 79 | | { |
| 0 | 80 | | writer.WriteStartElement("track"); |
| | 81 | |
|
| 0 | 82 | | if (track.ParentIndexNumber.HasValue && track.ParentIndexNumber.Value != 0) |
| | 83 | | { |
| 0 | 84 | | writer.WriteElementString("disc", track.ParentIndexNumber.Value.ToString(CultureInfo.InvariantCultur |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | if (track.IndexNumber.HasValue && track.IndexNumber.Value != 0) |
| | 88 | | { |
| 0 | 89 | | writer.WriteElementString("position", track.IndexNumber.Value.ToString(CultureInfo.InvariantCulture) |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | if (!string.IsNullOrEmpty(track.Name)) |
| | 93 | | { |
| 0 | 94 | | writer.WriteElementString("title", track.Name); |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | if (track.RunTimeTicks.HasValue) |
| | 98 | | { |
| 0 | 99 | | var time = TimeSpan.FromTicks(track.RunTimeTicks.Value).ToString(@"mm\:ss", CultureInfo.InvariantCul |
| | 100 | |
|
| 0 | 101 | | writer.WriteElementString("duration", time); |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | writer.WriteEndElement(); |
| | 105 | | } |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | /// <inheritdoc /> |
| | 109 | | protected override IEnumerable<string> GetTagsUsed(BaseItem item) |
| | 110 | | { |
| | 111 | | foreach (var tag in base.GetTagsUsed(item)) |
| | 112 | | { |
| | 113 | | yield return tag; |
| | 114 | | } |
| | 115 | |
|
| | 116 | | yield return "track"; |
| | 117 | | yield return "artist"; |
| | 118 | | yield return "albumartist"; |
| | 119 | | } |
| | 120 | | } |
| | 121 | | } |