StudiosImageProvider.cs 6.9 KB

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