| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using MediaBrowser.Controller.Entities; |
| | | 8 | | using MediaBrowser.Controller.Providers; |
| | | 9 | | using MediaBrowser.Model.IO; |
| | | 10 | | using MediaBrowser.XbmcMetadata.Savers; |
| | | 11 | | |
| | | 12 | | namespace MediaBrowser.XbmcMetadata.Providers |
| | | 13 | | { |
| | | 14 | | public abstract class BaseNfoProvider<T> : ILocalMetadataProvider<T>, IHasItemChangeMonitor |
| | | 15 | | where T : BaseItem, new() |
| | | 16 | | { |
| | | 17 | | private readonly IFileSystem _fileSystem; |
| | | 18 | | |
| | | 19 | | protected BaseNfoProvider(IFileSystem fileSystem) |
| | | 20 | | { |
| | 189 | 21 | | _fileSystem = fileSystem; |
| | 189 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 0 | 25 | | public string Name => BaseNfoSaver.SaverName; |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public Task<MetadataResult<T>> GetMetadata( |
| | | 29 | | ItemInfo info, |
| | | 30 | | IDirectoryService directoryService, |
| | | 31 | | CancellationToken cancellationToken) |
| | | 32 | | { |
| | 0 | 33 | | var result = new MetadataResult<T>(); |
| | | 34 | | |
| | 0 | 35 | | var file = GetXmlFile(info, directoryService); |
| | | 36 | | |
| | 0 | 37 | | if (file is null) |
| | | 38 | | { |
| | 0 | 39 | | return Task.FromResult(result); |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | var path = file.FullName; |
| | | 43 | | |
| | | 44 | | try |
| | | 45 | | { |
| | 0 | 46 | | result.Item = new T |
| | 0 | 47 | | { |
| | 0 | 48 | | IndexNumber = info.IndexNumber |
| | 0 | 49 | | }; |
| | | 50 | | |
| | 0 | 51 | | Fetch(result, path, cancellationToken); |
| | 0 | 52 | | result.HasMetadata = true; |
| | 0 | 53 | | } |
| | 0 | 54 | | catch (FileNotFoundException) |
| | | 55 | | { |
| | 0 | 56 | | result.HasMetadata = false; |
| | 0 | 57 | | } |
| | 0 | 58 | | catch (IOException) |
| | | 59 | | { |
| | 0 | 60 | | result.HasMetadata = false; |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | return Task.FromResult(result); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | | 68 | | { |
| | 0 | 69 | | var file = GetXmlFile(new ItemInfo(item), directoryService); |
| | | 70 | | |
| | 0 | 71 | | if (file?.Exists is not true) |
| | | 72 | | { |
| | 0 | 73 | | return false; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | var fileTime = _fileSystem.GetLastWriteTimeUtc(file); |
| | | 77 | | |
| | | 78 | | // 1 minute tolerance to avoid detecting our own file writes |
| | 0 | 79 | | return (fileTime - item.DateLastSaved) > TimeSpan.FromMinutes(1); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | protected abstract void Fetch(MetadataResult<T> result, string path, CancellationToken cancellationToken); |
| | | 83 | | |
| | | 84 | | protected abstract FileSystemMetadata? GetXmlFile(ItemInfo info, IDirectoryService directoryService); |
| | | 85 | | } |
| | | 86 | | } |