| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.IO.Compression; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text.Json; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Jellyfin.Data.Enums; |
| | | 10 | | using Jellyfin.Extensions.Json; |
| | | 11 | | using MediaBrowser.Controller.Entities; |
| | | 12 | | using MediaBrowser.Controller.Providers; |
| | | 13 | | using MediaBrowser.Model.IO; |
| | | 14 | | using MediaBrowser.Providers.Books.ComicBookInfo.Models; |
| | | 15 | | using Microsoft.Extensions.Logging; |
| | | 16 | | |
| | | 17 | | namespace MediaBrowser.Providers.Books.ComicBookInfo; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// ComicBookInfo provider. |
| | | 21 | | /// </summary> |
| | | 22 | | public class ComicBookInfoProvider : IComicProvider |
| | | 23 | | { |
| | | 24 | | private readonly ILogger<ComicBookInfoProvider> _logger; |
| | | 25 | | private readonly IFileSystem _fileSystem; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="ComicBookInfoProvider"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 31 | | /// <param name="logger">Instance of the <see cref="ILogger{ComicBookInfoProvider}"/> interface.</param> |
| | | 32 | | public ComicBookInfoProvider(IFileSystem fileSystem, ILogger<ComicBookInfoProvider> logger) |
| | | 33 | | { |
| | 22 | 34 | | _fileSystem = fileSystem; |
| | 22 | 35 | | _logger = logger; |
| | 22 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public async ValueTask<MetadataResult<Book>> ReadMetadata(ItemInfo info, IDirectoryService directoryService, Cancell |
| | | 40 | | { |
| | 0 | 41 | | var path = GetComicBookFile(info.Path)?.FullName; |
| | | 42 | | |
| | 0 | 43 | | if (path is null) |
| | | 44 | | { |
| | 0 | 45 | | _logger.LogError("could not load comic: {Path}", info.Path); |
| | 0 | 46 | | return new MetadataResult<Book> { HasMetadata = false }; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | try |
| | | 50 | | { |
| | 0 | 51 | | Stream stream = AsyncFile.OpenRead(path); |
| | 0 | 52 | | await using (stream.ConfigureAwait(false)) |
| | | 53 | | { |
| | 0 | 54 | | var archive = await ZipArchive.CreateAsync(stream, ZipArchiveMode.Read, false, null, cancellationToken). |
| | 0 | 55 | | await using (archive.ConfigureAwait(false)) |
| | | 56 | | { |
| | 0 | 57 | | if (archive.Comment is null) |
| | | 58 | | { |
| | 0 | 59 | | _logger.LogInformation("missing ComicBookInfo in archive comment: {Path}", info.Path); |
| | 0 | 60 | | return new MetadataResult<Book> { HasMetadata = false }; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | var comicBookMetadata = JsonSerializer.Deserialize<ComicBookInfoFormat>(archive.Comment, JsonDefault |
| | 0 | 64 | | if (comicBookMetadata is null) |
| | | 65 | | { |
| | 0 | 66 | | _logger.LogError("ComicBookInfo deserialization failure: {Path}", info.Path); |
| | 0 | 67 | | return new MetadataResult<Book> { HasMetadata = false }; |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | return SaveMetadata(comicBookMetadata); |
| | | 71 | | } |
| | | 72 | | } |
| | 0 | 73 | | } |
| | 0 | 74 | | catch (Exception ex) |
| | | 75 | | { |
| | 0 | 76 | | _logger.LogError(ex, "failed to load ComicBookInfo metadata: {Path}", info.Path); |
| | 0 | 77 | | return new MetadataResult<Book> { HasMetadata = false }; |
| | | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public bool HasItemChanged(BaseItem item) |
| | | 83 | | { |
| | 0 | 84 | | var file = GetComicBookFile(item.Path); |
| | | 85 | | |
| | 0 | 86 | | if (file is null) |
| | | 87 | | { |
| | 0 | 88 | | return false; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return file.Exists && _fileSystem.GetLastWriteTimeUtc(file) > item.DateLastSaved; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | private MetadataResult<Book> SaveMetadata(ComicBookInfoFormat comic) |
| | | 95 | | { |
| | 0 | 96 | | if (comic.Metadata is null) |
| | | 97 | | { |
| | 0 | 98 | | return new MetadataResult<Book> { HasMetadata = false }; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | var book = ReadComicBookMetadata(comic.Metadata); |
| | | 102 | | |
| | 0 | 103 | | if (book is null) |
| | | 104 | | { |
| | 0 | 105 | | return new MetadataResult<Book> { HasMetadata = false }; |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | var metadataResult = new MetadataResult<Book> { Item = book, HasMetadata = true }; |
| | | 109 | | |
| | 0 | 110 | | if (comic.Metadata.Language is not null) |
| | | 111 | | { |
| | 0 | 112 | | metadataResult.ResultLanguage = ReadCultureInfoInto(comic.Metadata.Language); |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | if (comic.Metadata.Credits.Count > 0) |
| | | 116 | | { |
| | 0 | 117 | | ReadPeopleMetadata(comic.Metadata, metadataResult); |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | return metadataResult; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | private FileSystemMetadata? GetComicBookFile(string path) |
| | | 124 | | { |
| | 0 | 125 | | var fileInfo = _fileSystem.GetFileSystemInfo(path); |
| | | 126 | | |
| | 0 | 127 | | if (fileInfo.IsDirectory) |
| | | 128 | | { |
| | 0 | 129 | | return null; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | // only parse files that are known to have ComicBookInfo metadata |
| | 0 | 133 | | return fileInfo.Extension.Equals(".cbz", StringComparison.OrdinalIgnoreCase) ? fileInfo : null; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | private static Book? ReadComicBookMetadata(ComicBookInfoMetadata comic) |
| | | 137 | | { |
| | 0 | 138 | | var book = new Book(); |
| | 0 | 139 | | var hasFoundMetadata = false; |
| | | 140 | | |
| | 0 | 141 | | hasFoundMetadata |= ReadStringInto(comic.Title, title => book.Name = title); |
| | 0 | 142 | | hasFoundMetadata |= ReadStringInto(comic.Series, series => book.SeriesName = series); |
| | 0 | 143 | | hasFoundMetadata |= ReadStringInto(comic.Genre, genre => book.AddGenre(genre)); |
| | 0 | 144 | | hasFoundMetadata |= ReadStringInto(comic.Comments, overview => book.Overview = overview); |
| | 0 | 145 | | hasFoundMetadata |= ReadStringInto(comic.Publisher, publisher => book.SetStudios([publisher])); |
| | | 146 | | |
| | 0 | 147 | | if (comic.PublicationYear is not null) |
| | | 148 | | { |
| | 0 | 149 | | book.ProductionYear = comic.PublicationYear; |
| | 0 | 150 | | hasFoundMetadata = true; |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | if (comic.Issue is not null) |
| | | 154 | | { |
| | 0 | 155 | | book.IndexNumber = comic.Issue; |
| | 0 | 156 | | hasFoundMetadata = true; |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | if (comic.Tags.Count > 0) |
| | | 160 | | { |
| | 0 | 161 | | book.Tags = comic.Tags.ToArray(); |
| | 0 | 162 | | hasFoundMetadata = true; |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | if (comic.PublicationYear is not null && comic.PublicationMonth is not null) |
| | | 166 | | { |
| | 0 | 167 | | book.PremiereDate = ReadTwoPartDateInto(comic.PublicationYear.Value, comic.PublicationMonth.Value); |
| | 0 | 168 | | hasFoundMetadata = true; |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | return hasFoundMetadata ? book : null; |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | private static void ReadPeopleMetadata(ComicBookInfoMetadata comic, MetadataResult<Book> metadataResult) |
| | | 175 | | { |
| | 0 | 176 | | foreach (var person in comic.Credits) |
| | | 177 | | { |
| | 0 | 178 | | if (person.Person is null || person.Role is null) |
| | | 179 | | { |
| | | 180 | | continue; |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | if (person.Person.Contains(',', StringComparison.InvariantCultureIgnoreCase)) |
| | | 184 | | { |
| | 0 | 185 | | var name = person.Person.Split(','); |
| | 0 | 186 | | person.Person = name[1].Trim(' ') + " " + name[0].Trim(' '); |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | if (!Enum.TryParse(person.Role, out PersonKind personKind)) |
| | | 190 | | { |
| | 0 | 191 | | personKind = PersonKind.Unknown; |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | if (string.Equals("Colorer", person.Role, StringComparison.OrdinalIgnoreCase)) |
| | | 195 | | { |
| | 0 | 196 | | personKind = PersonKind.Colorist; |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | metadataResult.AddPerson(new PersonInfo { Name = person.Person, Type = personKind }); |
| | | 200 | | } |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | private static string? ReadCultureInfoInto(string language) |
| | | 204 | | { |
| | | 205 | | try |
| | | 206 | | { |
| | 0 | 207 | | return CultureInfo.GetCultureInfo(language).DisplayName; |
| | | 208 | | } |
| | 0 | 209 | | catch (CultureNotFoundException) |
| | | 210 | | { |
| | 0 | 211 | | return null; |
| | | 212 | | } |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | private static bool ReadStringInto(string? data, Action<string> commitResult) |
| | | 216 | | { |
| | 0 | 217 | | if (!string.IsNullOrWhiteSpace(data)) |
| | | 218 | | { |
| | 0 | 219 | | commitResult(data); |
| | 0 | 220 | | return true; |
| | | 221 | | } |
| | | 222 | | |
| | 0 | 223 | | return false; |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | private static DateTime? ReadTwoPartDateInto(int year, int month) |
| | | 227 | | { |
| | | 228 | | try |
| | | 229 | | { |
| | | 230 | | // use first day of the month because this format doesn't include a day |
| | 0 | 231 | | return new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Unspecified); |
| | | 232 | | } |
| | 0 | 233 | | catch (ArgumentOutOfRangeException) |
| | | 234 | | { |
| | 0 | 235 | | return null; |
| | | 236 | | } |
| | 0 | 237 | | } |
| | | 238 | | } |