StudiosImageProvider.cs 6.9 KB

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