2020-06-19 11:24:13 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2013-12-30 09:32:01 -07:00
|
|
|
using System.Collections.Generic;
|
2020-08-07 10:26:28 -07:00
|
|
|
using System.Globalization;
|
2013-12-30 09:32:01 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-07-29 14:47:25 -07:00
|
|
|
using System.Net.Http;
|
2013-12-30 09:32:01 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-06-19 09:02:33 -07:00
|
|
|
using Jellyfin.Extensions;
|
2020-08-31 10:05:21 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-12-30 09:32:01 -07:00
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
namespace MediaBrowser.Providers.Studios
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
2014-01-28 11:37:01 -07:00
|
|
|
public class StudiosImageProvider : IRemoteImageProvider
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2020-08-17 10:46:40 -07:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2013-12-30 09:32:01 -07:00
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
2020-08-17 10:46:40 -07:00
|
|
|
public StudiosImageProvider(IServerConfigurationManager config, IHttpClientFactory httpClientFactory, IFileSystem fileSystem)
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
|
|
|
_config = config;
|
2020-08-17 10:46:40 -07:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2013-12-30 09:32:01 -07:00
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:31:14 -07:00
|
|
|
public string Name => "Emby Designs";
|
2013-12-30 09:32:01 -07:00
|
|
|
|
2020-09-08 07:12:47 -07:00
|
|
|
public int Order => 0;
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public bool Supports(BaseItem item)
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
|
|
|
return item is Studio;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
2019-01-07 16:27:46 -07:00
|
|
|
ImageType.Primary,
|
2014-01-28 11:37:01 -07:00
|
|
|
ImageType.Thumb
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
|
|
|
return GetImages(item, true, true, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, bool posters, bool thumbs, CancellationToken cancellationToken)
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
|
|
if (posters)
|
|
|
|
{
|
|
|
|
var posterPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudioposters.txt");
|
|
|
|
|
2017-04-28 23:22:33 -07:00
|
|
|
posterPath = await EnsurePosterList(posterPath, cancellationToken).ConfigureAwait(false);
|
2013-12-30 09:32:01 -07:00
|
|
|
|
|
|
|
list.Add(GetImage(item, posterPath, ImageType.Primary, "folder"));
|
|
|
|
}
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2013-12-30 09:50:57 -07:00
|
|
|
if (thumbs)
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
|
|
|
var thumbsPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudiothumbs.txt");
|
|
|
|
|
2017-04-28 23:22:33 -07:00
|
|
|
thumbsPath = await EnsureThumbsList(thumbsPath, cancellationToken).ConfigureAwait(false);
|
2013-12-30 09:32:01 -07:00
|
|
|
|
|
|
|
list.Add(GetImage(item, thumbsPath, ImageType.Thumb, "thumb"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.Where(i => i != null);
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private RemoteImageInfo GetImage(BaseItem item, string filename, ImageType type, string remoteFilename)
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
2020-01-08 09:52:50 -07:00
|
|
|
var list = GetAvailableImages(filename);
|
2013-12-30 09:32:01 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var match = FindMatch(item, list);
|
2013-12-30 09:32:01 -07:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(match))
|
|
|
|
{
|
|
|
|
var url = GetUrl(match, remoteFilename);
|
|
|
|
|
|
|
|
return new RemoteImageInfo
|
|
|
|
{
|
|
|
|
ProviderName = Name,
|
|
|
|
Type = type,
|
|
|
|
Url = url
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetUrl(string image, string filename)
|
|
|
|
{
|
2020-08-07 10:26:28 -07:00
|
|
|
return string.Format(CultureInfo.InvariantCulture, "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studios/{0}/{1}.jpg", image, filename);
|
2013-12-30 09:32:01 -07:00
|
|
|
}
|
|
|
|
|
2017-04-28 23:22:33 -07:00
|
|
|
private Task<string> EnsureThumbsList(string file, CancellationToken cancellationToken)
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
|
|
|
const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studiothumbs.txt";
|
|
|
|
|
2020-08-17 10:46:40 -07:00
|
|
|
return EnsureList(url, file, _fileSystem, cancellationToken);
|
2013-12-30 09:32:01 -07:00
|
|
|
}
|
|
|
|
|
2017-04-28 23:22:33 -07:00
|
|
|
private Task<string> EnsurePosterList(string file, CancellationToken cancellationToken)
|
2013-12-30 09:32:01 -07:00
|
|
|
{
|
|
|
|
const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studioposters.txt";
|
|
|
|
|
2020-08-17 10:46:40 -07:00
|
|
|
return EnsureList(url, file, _fileSystem, cancellationToken);
|
2013-12-30 09:32:01 -07:00
|
|
|
}
|
|
|
|
|
2020-08-17 10:52:09 -07:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2020-08-31 10:05:21 -07:00
|
|
|
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
2020-08-17 10:52:09 -07:00
|
|
|
return httpClient.GetAsync(url, cancellationToken);
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Ensures the list.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="url">The URL.</param>
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2020-08-17 10:46:40 -07:00
|
|
|
public async Task<string> EnsureList(string url, string file, IFileSystem fileSystem, CancellationToken cancellationToken)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
var fileInfo = fileSystem.GetFileInfo(file);
|
|
|
|
|
|
|
|
if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
|
|
|
|
{
|
2020-08-31 10:05:21 -07:00
|
|
|
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2020-08-17 10:46:40 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(file));
|
2021-02-15 06:19:08 -07:00
|
|
|
await using var response = await httpClient.GetStreamAsync(url, cancellationToken).ConfigureAwait(false);
|
2020-08-17 10:46:40 -07:00
|
|
|
await using var fileStream = new FileStream(file, FileMode.Create);
|
|
|
|
await response.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string FindMatch(BaseItem item, IEnumerable<string> images)
|
|
|
|
{
|
|
|
|
var name = GetComparableName(item.Name);
|
|
|
|
|
|
|
|
return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase));
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetComparableName(string name)
|
|
|
|
{
|
2020-09-08 07:12:47 -07:00
|
|
|
return name.Replace(" ", string.Empty, StringComparison.Ordinal)
|
|
|
|
.Replace(".", string.Empty, StringComparison.Ordinal)
|
|
|
|
.Replace("&", string.Empty, StringComparison.Ordinal)
|
|
|
|
.Replace("!", string.Empty, StringComparison.Ordinal)
|
|
|
|
.Replace(",", string.Empty, StringComparison.Ordinal)
|
|
|
|
.Replace("/", string.Empty, StringComparison.Ordinal);
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:52:50 -07:00
|
|
|
public IEnumerable<string> GetAvailableImages(string file)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2021-05-13 07:47:00 -07:00
|
|
|
using var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
|
|
using var reader = new StreamReader(fileStream);
|
2021-02-25 18:38:18 -07:00
|
|
|
var lines = new List<string>();
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2021-05-10 06:05:12 -07:00
|
|
|
foreach (var line in reader.ReadAllLines())
|
2021-02-25 18:38:18 -07:00
|
|
|
{
|
2021-05-10 06:05:12 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(line))
|
2021-02-25 18:38:18 -07:00
|
|
|
{
|
2021-05-10 06:05:12 -07:00
|
|
|
lines.Add(line);
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 18:38:18 -07:00
|
|
|
|
|
|
|
return lines;
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
2013-12-30 09:32:01 -07:00
|
|
|
}
|
|
|
|
}
|