StudiosImageProvider.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Jellyfin.Extensions;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Controller.Configuration;
  13. using MediaBrowser.Controller.Entities;
  14. using MediaBrowser.Controller.Providers;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.Providers;
  18. namespace MediaBrowser.Providers.Studios
  19. {
  20. public class StudiosImageProvider : IRemoteImageProvider
  21. {
  22. private readonly IServerConfigurationManager _config;
  23. private readonly IHttpClientFactory _httpClientFactory;
  24. private readonly IFileSystem _fileSystem;
  25. public StudiosImageProvider(IServerConfigurationManager config, IHttpClientFactory httpClientFactory, IFileSystem fileSystem)
  26. {
  27. _config = config;
  28. _httpClientFactory = httpClientFactory;
  29. _fileSystem = fileSystem;
  30. }
  31. public string Name => "Emby Designs";
  32. public int Order => 0;
  33. public bool Supports(BaseItem item)
  34. {
  35. return item is Studio;
  36. }
  37. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  38. {
  39. return new List<ImageType>
  40. {
  41. ImageType.Primary,
  42. ImageType.Thumb
  43. };
  44. }
  45. public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  46. {
  47. return GetImages(item, true, true, cancellationToken);
  48. }
  49. private async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, bool posters, bool thumbs, CancellationToken cancellationToken)
  50. {
  51. var list = new List<RemoteImageInfo>();
  52. if (posters)
  53. {
  54. var posterPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudioposters.txt");
  55. posterPath = await EnsurePosterList(posterPath, cancellationToken).ConfigureAwait(false);
  56. list.Add(GetImage(item, posterPath, ImageType.Primary, "folder"));
  57. }
  58. cancellationToken.ThrowIfCancellationRequested();
  59. if (thumbs)
  60. {
  61. var thumbsPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudiothumbs.txt");
  62. thumbsPath = await EnsureThumbsList(thumbsPath, cancellationToken).ConfigureAwait(false);
  63. list.Add(GetImage(item, thumbsPath, ImageType.Thumb, "thumb"));
  64. }
  65. return list.Where(i => i != null);
  66. }
  67. private RemoteImageInfo GetImage(BaseItem item, string filename, ImageType type, string remoteFilename)
  68. {
  69. var list = GetAvailableImages(filename);
  70. var match = FindMatch(item, list);
  71. if (!string.IsNullOrEmpty(match))
  72. {
  73. var url = GetUrl(match, remoteFilename);
  74. return new RemoteImageInfo
  75. {
  76. ProviderName = Name,
  77. Type = type,
  78. Url = url
  79. };
  80. }
  81. return null;
  82. }
  83. private string GetUrl(string image, string filename)
  84. {
  85. return string.Format(CultureInfo.InvariantCulture, "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studios/{0}/{1}.jpg", image, filename);
  86. }
  87. private Task<string> EnsureThumbsList(string file, CancellationToken cancellationToken)
  88. {
  89. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studiothumbs.txt";
  90. return EnsureList(url, file, _fileSystem, cancellationToken);
  91. }
  92. private Task<string> EnsurePosterList(string file, CancellationToken cancellationToken)
  93. {
  94. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studioposters.txt";
  95. return EnsureList(url, file, _fileSystem, cancellationToken);
  96. }
  97. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  98. {
  99. var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
  100. return httpClient.GetAsync(url, cancellationToken);
  101. }
  102. /// <summary>
  103. /// Ensures the list.
  104. /// </summary>
  105. /// <param name="url">The URL.</param>
  106. /// <param name="file">The file.</param>
  107. /// <param name="fileSystem">The file system.</param>
  108. /// <param name="cancellationToken">The cancellation token.</param>
  109. /// <returns>Task.</returns>
  110. public async Task<string> EnsureList(string url, string file, IFileSystem fileSystem, CancellationToken cancellationToken)
  111. {
  112. var fileInfo = fileSystem.GetFileInfo(file);
  113. if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
  114. {
  115. var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
  116. Directory.CreateDirectory(Path.GetDirectoryName(file));
  117. await using var response = await httpClient.GetStreamAsync(url, cancellationToken).ConfigureAwait(false);
  118. await using var fileStream = new FileStream(file, FileMode.Create);
  119. await response.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
  120. }
  121. return file;
  122. }
  123. public string FindMatch(BaseItem item, IEnumerable<string> images)
  124. {
  125. var name = GetComparableName(item.Name);
  126. return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase));
  127. }
  128. private string GetComparableName(string name)
  129. {
  130. return name.Replace(" ", string.Empty, StringComparison.Ordinal)
  131. .Replace(".", string.Empty, StringComparison.Ordinal)
  132. .Replace("&", string.Empty, StringComparison.Ordinal)
  133. .Replace("!", string.Empty, StringComparison.Ordinal)
  134. .Replace(",", string.Empty, StringComparison.Ordinal)
  135. .Replace("/", string.Empty, StringComparison.Ordinal);
  136. }
  137. public IEnumerable<string> GetAvailableImages(string file)
  138. {
  139. using var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
  140. using var reader = new StreamReader(fileStream);
  141. var lines = new List<string>();
  142. foreach (var line in reader.ReadAllLines())
  143. {
  144. if (!string.IsNullOrWhiteSpace(line))
  145. {
  146. lines.Add(line);
  147. }
  148. }
  149. return lines;
  150. }
  151. }
  152. }