aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.LocalMetadata/Images/InternalMetadataFolderImageProvider.cs
blob: 509b5d700d849a93677f8a51286fdf916822b62d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections.Generic;
using System.IO;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.LocalMetadata.Images
{
    /// <summary>
    /// Internal metadata folder image provider.
    /// </summary>
    public class InternalMetadataFolderImageProvider : ILocalImageProvider, IHasOrder
    {
        private readonly IServerConfigurationManager _config;
        private readonly IFileSystem _fileSystem;
        private readonly ILogger<InternalMetadataFolderImageProvider> _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="InternalMetadataFolderImageProvider"/> class.
        /// </summary>
        /// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
        /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
        /// <param name="logger">Instance of the <see cref="ILogger{InternalMetadataFolderImageProvider}"/> interface.</param>
        public InternalMetadataFolderImageProvider(
            IServerConfigurationManager config,
            IFileSystem fileSystem,
            ILogger<InternalMetadataFolderImageProvider> logger)
        {
            _config = config;
            _fileSystem = fileSystem;
            _logger = logger;
        }

        /// Make sure this is last so that all other locations are scanned first
        /// <inheritdoc />
        public int Order => 1000;

        /// <inheritdoc />
        public string Name => "Internal Images";

        /// <inheritdoc />
        public bool Supports(BaseItem item)
        {
            if (item is Photo)
            {
                return false;
            }

            if (!item.IsSaveLocalMetadataEnabled())
            {
                return true;
            }

            // Extracted images will be saved in here
            if (item is Audio)
            {
                return true;
            }

            if (item.SupportsLocalMetadata && !item.AlwaysScanInternalMetadataPath)
            {
                return false;
            }

            return true;
        }

        /// <inheritdoc />
        public List<LocalImageInfo> GetImages(BaseItem item, IDirectoryService directoryService)
        {
            var path = item.GetInternalMetadataPath();

            if (!Directory.Exists(path))
            {
                return new List<LocalImageInfo>();
            }

            try
            {
                return new LocalImageProvider(_fileSystem).GetImages(item, path, directoryService);
            }
            catch (IOException ex)
            {
                _logger.LogError(ex, "Error while getting images for {Library}", item.Name);
                return new List<LocalImageInfo>();
            }
        }
    }
}