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