aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Helpers/ItemHelper.cs
blob: 622eb0b9fe577d3640eef7d66c68f05f36a4e155 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Linq;
using Jellyfin.Api.Models.UserDtos;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;

namespace Jellyfin.Api.Helpers
{
    /// <summary>
    /// Item helper.
    /// </summary>
    public static class ItemHelper
    {
        /// <summary>
        /// Opens lyrics file, converts to a List of Lyrics, and returns it.
        /// </summary>
        /// <param name="item">Requested Item.</param>
        /// <returns>Collection of Lyrics.</returns>
        internal static object? GetLyricData(BaseItem item)
        {
            List<ILyricsProvider> providerList = new List<ILyricsProvider>();

            // Find all classes that implement ILyricsProvider Interface
            var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly()
                .GetTypes()
                .Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface);

            if (!foundLyricProviders.Any())
            {
                return null;
            }

            foreach (var provider in foundLyricProviders)
            {
                providerList.Add((ILyricsProvider)Activator.CreateInstance(provider));
            }

            foreach (ILyricsProvider provider in providerList)
            {
                provider.Process(item);
                if (provider.HasData)
                {
                    return provider.Data;
                }
            }

            return null;
        }

        /// <summary>
        /// Checks if requested item has a matching lyric file.
        /// </summary>
        /// <param name="itemPath">Path of requested item.</param>
        /// <returns>True if item has a matching lyrics file.</returns>
        public static string? GetLyricFilePath(string itemPath)
        {
            List<string> supportedLyricFileExtensions = new List<string>();

            // Find all classes that implement ILyricsProvider Interface
            var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly()
                .GetTypes()
                .Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface);

            if (!foundLyricProviders.Any())
            {
                return null;
            }

            // Iterate over all found lyric providers
            foreach (var provider in foundLyricProviders)
            {
                var foundProvider = (ILyricsProvider)Activator.CreateInstance(provider);
                if (foundProvider?.FileExtensions is null)
                {
                    continue;
                }

                if (foundProvider.FileExtensions.Any())
                {
                    // Gather distinct list of handled file extentions
                    foreach (string lyricFileExtension in foundProvider.FileExtensions)
                    {
                        if (!supportedLyricFileExtensions.Contains(lyricFileExtension))
                        {
                            supportedLyricFileExtensions.Add(lyricFileExtension);
                        }
                    }
                }
            }

            foreach (string lyricFileExtension in supportedLyricFileExtensions)
            {
                string lyricFilePath = @Path.ChangeExtension(itemPath, lyricFileExtension);
                if (System.IO.File.Exists(lyricFilePath))
                {
                    return lyricFilePath;
                }
            }

            return null;
        }


        /// <summary>
        /// Checks if requested item has a matching local lyric file.
        /// </summary>
        /// <param name="itemPath">Path of requested item.</param>
        /// <returns>True if item has a matching lyrics file; otherwise false.</returns>
        public static bool HasLyricFile(string itemPath)
        {
            string? lyricFilePath = GetLyricFilePath(itemPath);
            return !string.IsNullOrEmpty(lyricFilePath);
        }
    }
}