| | | 1 | | using System.IO; |
| | | 2 | | using System.Threading; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using System.Xml; |
| | | 5 | | using MediaBrowser.Controller.Entities; |
| | | 6 | | using MediaBrowser.Controller.Providers; |
| | | 7 | | using MediaBrowser.Model.IO; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | |
| | | 10 | | namespace MediaBrowser.Providers.Books.OpenPackagingFormat |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides metadata for book items that have an OPF file in the same directory. Supports the standard |
| | | 14 | | /// content.opf filename, bespoke metadata.opf name from Calibre libraries, and OPF files that have the |
| | | 15 | | /// same name as their respective books for directories with several books. |
| | | 16 | | /// </summary> |
| | | 17 | | public class OpfProvider : ILocalMetadataProvider<Book>, IHasItemChangeMonitor |
| | | 18 | | { |
| | | 19 | | private const string StandardOpfFile = "content.opf"; |
| | | 20 | | private const string CalibreOpfFile = "metadata.opf"; |
| | | 21 | | |
| | | 22 | | private readonly IFileSystem _fileSystem; |
| | | 23 | | |
| | | 24 | | private readonly ILogger<OpfProvider> _logger; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="OpfProvider"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 30 | | /// <param name="logger">Instance of the <see cref="ILogger{OpfProvider}"/> interface.</param> |
| | | 31 | | public OpfProvider(IFileSystem fileSystem, ILogger<OpfProvider> logger) |
| | | 32 | | { |
| | 21 | 33 | | _fileSystem = fileSystem; |
| | 21 | 34 | | _logger = logger; |
| | 21 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | 0 | 38 | | public string Name => "Open Packaging Format"; |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | | 42 | | { |
| | 0 | 43 | | var file = GetXmlFile(item.Path); |
| | | 44 | | |
| | 0 | 45 | | return file.Exists && _fileSystem.GetLastWriteTimeUtc(file) > item.DateLastSaved; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public Task<MetadataResult<Book>> GetMetadata(ItemInfo info, IDirectoryService directoryService, CancellationTok |
| | | 50 | | { |
| | 0 | 51 | | var path = GetXmlFile(info.Path).FullName; |
| | | 52 | | |
| | | 53 | | try |
| | | 54 | | { |
| | 0 | 55 | | return Task.FromResult(ReadOpfData(path, cancellationToken)); |
| | | 56 | | } |
| | 0 | 57 | | catch (FileNotFoundException) |
| | | 58 | | { |
| | 0 | 59 | | return Task.FromResult(new MetadataResult<Book> { HasMetadata = false }); |
| | | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | private FileSystemMetadata GetXmlFile(string path) |
| | | 64 | | { |
| | 0 | 65 | | var fileInfo = _fileSystem.GetFileSystemInfo(path); |
| | 0 | 66 | | var directoryInfo = fileInfo.IsDirectory ? fileInfo : _fileSystem.GetDirectoryInfo(Path.GetDirectoryName(pat |
| | | 67 | | |
| | | 68 | | // check for OPF with matching name first since it's the most specific filename |
| | 0 | 69 | | var specificFile = Path.Combine(directoryInfo.FullName, Path.GetFileNameWithoutExtension(path) + ".opf"); |
| | 0 | 70 | | var file = _fileSystem.GetFileInfo(specificFile); |
| | | 71 | | |
| | 0 | 72 | | if (file.Exists) |
| | | 73 | | { |
| | 0 | 74 | | return file; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | file = _fileSystem.GetFileInfo(Path.Combine(directoryInfo.FullName, StandardOpfFile)); |
| | | 78 | | |
| | | 79 | | // check metadata.opf last since it's really only used by Calibre |
| | 0 | 80 | | return file.Exists ? file : _fileSystem.GetFileInfo(Path.Combine(directoryInfo.FullName, CalibreOpfFile)); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | private MetadataResult<Book> ReadOpfData(string file, CancellationToken cancellationToken) |
| | | 84 | | { |
| | 0 | 85 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 86 | | |
| | 0 | 87 | | var doc = new XmlDocument(); |
| | 0 | 88 | | doc.Load(file); |
| | | 89 | | |
| | 0 | 90 | | var utilities = new OpfReader<OpfProvider>(doc, _logger); |
| | 0 | 91 | | return utilities.ReadOpfData(cancellationToken); |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |