| | | 1 | | using System.IO; |
| | | 2 | | using System.Threading; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using MediaBrowser.Controller.Entities; |
| | | 5 | | using MediaBrowser.Controller.Providers; |
| | | 6 | | using MediaBrowser.Model.IO; |
| | | 7 | | |
| | | 8 | | namespace MediaBrowser.LocalMetadata |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// The BaseXmlProvider. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="T">Type of provider.</typeparam> |
| | | 14 | | public abstract class BaseXmlProvider<T> : ILocalMetadataProvider<T>, IHasItemChangeMonitor, IHasOrder |
| | | 15 | | where T : BaseItem, new() |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="BaseXmlProvider{T}"/> class. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 21 | | protected BaseXmlProvider(IFileSystem fileSystem) |
| | | 22 | | { |
| | 42 | 23 | | this.FileSystem = fileSystem; |
| | 42 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | 0 | 27 | | public string Name => XmlProviderUtils.Name; |
| | | 28 | | |
| | | 29 | | /// After Nfo |
| | | 30 | | /// <inheritdoc /> |
| | 0 | 31 | | public virtual int Order => 1; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the IFileSystem. |
| | | 35 | | /// </summary> |
| | | 36 | | protected IFileSystem FileSystem { get; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets metadata for item. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="info">The item info.</param> |
| | | 42 | | /// <param name="directoryService">Instance of the <see cref="IDirectoryService"/> interface.</param> |
| | | 43 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 44 | | /// <returns>The metadata for item.</returns> |
| | | 45 | | public Task<MetadataResult<T>> GetMetadata( |
| | | 46 | | ItemInfo info, |
| | | 47 | | IDirectoryService directoryService, |
| | | 48 | | CancellationToken cancellationToken) |
| | | 49 | | { |
| | 0 | 50 | | var result = new MetadataResult<T>(); |
| | | 51 | | |
| | 0 | 52 | | var file = GetXmlFile(info, directoryService); |
| | | 53 | | |
| | 0 | 54 | | if (file is null) |
| | | 55 | | { |
| | 0 | 56 | | return Task.FromResult(result); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | var path = file.FullName; |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 0 | 63 | | result.Item = new T(); |
| | | 64 | | |
| | 0 | 65 | | Fetch(result, path, cancellationToken); |
| | 0 | 66 | | result.HasMetadata = true; |
| | 0 | 67 | | } |
| | 0 | 68 | | catch (FileNotFoundException) |
| | | 69 | | { |
| | 0 | 70 | | result.HasMetadata = false; |
| | 0 | 71 | | } |
| | 0 | 72 | | catch (IOException) |
| | | 73 | | { |
| | 0 | 74 | | result.HasMetadata = false; |
| | 0 | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return Task.FromResult(result); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Get metadata from path. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="result">Resulting metadata.</param> |
| | | 84 | | /// <param name="path">The path.</param> |
| | | 85 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 86 | | protected abstract void Fetch(MetadataResult<T> result, string path, CancellationToken cancellationToken); |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Get metadata from xml file. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="info">Item inf.</param> |
| | | 92 | | /// <param name="directoryService">Instance of the <see cref="IDirectoryService"/> interface.</param> |
| | | 93 | | /// <returns>The file system metadata.</returns> |
| | | 94 | | protected abstract FileSystemMetadata? GetXmlFile(ItemInfo info, IDirectoryService directoryService); |
| | | 95 | | |
| | | 96 | | /// <inheritdoc /> |
| | | 97 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | | 98 | | { |
| | 0 | 99 | | var file = GetXmlFile(new ItemInfo(item), directoryService); |
| | | 100 | | |
| | 0 | 101 | | if (file is null) |
| | | 102 | | { |
| | 0 | 103 | | return false; |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | return file.Exists && FileSystem.GetLastWriteTimeUtc(file) > item.DateLastSaved; |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | } |