ImageUtils.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Entities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Common.IO;
  10. using MediaBrowser.Controller.IO;
  11. using MediaBrowser.Model.IO;
  12. namespace MediaBrowser.Providers.ImagesByName
  13. {
  14. public static class ImageUtils
  15. {
  16. /// <summary>
  17. /// Ensures the list.
  18. /// </summary>
  19. /// <param name="url">The URL.</param>
  20. /// <param name="file">The file.</param>
  21. /// <param name="httpClient">The HTTP client.</param>
  22. /// <param name="fileSystem">The file system.</param>
  23. /// <param name="semaphore">The semaphore.</param>
  24. /// <param name="cancellationToken">The cancellation token.</param>
  25. /// <returns>Task.</returns>
  26. public static async Task EnsureList(string url, string file, IHttpClient httpClient, IFileSystem fileSystem, SemaphoreSlim semaphore, CancellationToken cancellationToken)
  27. {
  28. var fileInfo = fileSystem.GetFileInfo(file);
  29. if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
  30. {
  31. await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  32. try
  33. {
  34. var temp = await httpClient.GetTempFile(new HttpRequestOptions
  35. {
  36. CancellationToken = cancellationToken,
  37. Progress = new Progress<double>(),
  38. Url = url
  39. }).ConfigureAwait(false);
  40. fileSystem.CreateDirectory(Path.GetDirectoryName(file));
  41. fileSystem.CopyFile(temp, file, true);
  42. }
  43. finally
  44. {
  45. semaphore.Release();
  46. }
  47. }
  48. }
  49. public static string FindMatch(IHasImages item, IEnumerable<string> images)
  50. {
  51. var name = GetComparableName(item.Name);
  52. return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase));
  53. }
  54. private static string GetComparableName(string name)
  55. {
  56. return name.Replace(" ", string.Empty)
  57. .Replace(".", string.Empty)
  58. .Replace("&", string.Empty)
  59. .Replace("!", string.Empty)
  60. .Replace(",", string.Empty)
  61. .Replace("/", string.Empty);
  62. }
  63. public static IEnumerable<string> GetAvailableImages(string file, IFileSystem fileSystem)
  64. {
  65. using (var fileStream = fileSystem.GetFileStream(file, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
  66. {
  67. using (var reader = new StreamReader(fileStream))
  68. {
  69. var lines = new List<string>();
  70. while (!reader.EndOfStream)
  71. {
  72. var text = reader.ReadLine();
  73. if (!string.IsNullOrWhiteSpace(text))
  74. {
  75. lines.Add(text);
  76. }
  77. }
  78. return lines;
  79. }
  80. }
  81. }
  82. }
  83. }