2021-10-26 13:49:01 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2013-12-30 16:32:01 +00:00
|
|
|
using System.Collections.Generic;
|
2020-08-07 17:26:28 +00:00
|
|
|
using System.Globalization;
|
2013-12-30 16:32:01 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-07-29 21:47:25 +00:00
|
|
|
using System.Net.Http;
|
2013-12-30 16:32:01 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-06-19 16:02:33 +00:00
|
|
|
using Jellyfin.Extensions;
|
2020-08-31 17:05:21 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-01-13 19:26:31 +00: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 16:32:01 +00:00
|
|
|
|
2022-04-05 11:52:48 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.StudioImages
|
2013-12-30 16:32:01 +00:00
|
|
|
{
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Studio image provider.
|
|
|
|
/// </summary>
|
2014-01-28 18:37:01 +00:00
|
|
|
public class StudiosImageProvider : IRemoteImageProvider
|
2013-12-30 16:32:01 +00:00
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2020-08-17 17:46:40 +00:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2013-12-30 16:32:01 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="StudiosImageProvider"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="config">The <see cref="IServerConfigurationManager"/>.</param>
|
|
|
|
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
|
|
|
|
/// <param name="fileSystem">The <see cref="IFileSystem"/>.</param>
|
2020-08-17 17:46:40 +00:00
|
|
|
public StudiosImageProvider(IServerConfigurationManager config, IHttpClientFactory httpClientFactory, IFileSystem fileSystem)
|
2013-12-30 16:32:01 +00:00
|
|
|
{
|
|
|
|
_config = config;
|
2020-08-17 17:46:40 +00:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2013-12-30 16:32:01 +00:00
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <inheritdoc />
|
2021-01-12 20:35:06 +00:00
|
|
|
public string Name => "Artwork Repository";
|
2013-12-30 16:32:01 +00:00
|
|
|
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-12-30 16:32:01 +00:00
|
|
|
{
|
|
|
|
return item is Studio;
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2024-08-02 14:19:20 +00:00
|
|
|
return [ImageType.Thumb];
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <inheritdoc />
|
2022-03-08 15:36:05 +00:00
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-12-30 16:32:01 +00:00
|
|
|
{
|
2022-03-08 15:36:05 +00:00
|
|
|
var thumbsPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudiothumbs.txt");
|
2013-12-30 16:32:01 +00:00
|
|
|
|
2023-06-22 03:01:47 +00:00
|
|
|
await EnsureThumbsList(thumbsPath, cancellationToken).ConfigureAwait(false);
|
2013-12-30 16:32:01 +00:00
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2022-03-08 15:36:05 +00:00
|
|
|
var imageInfo = GetImage(item, thumbsPath, ImageType.Thumb, "thumb");
|
2013-12-30 16:32:01 +00:00
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
if (imageInfo is null)
|
2022-03-08 15:36:05 +00:00
|
|
|
{
|
2024-08-02 14:19:20 +00:00
|
|
|
return [];
|
2013-12-30 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 14:19:20 +00:00
|
|
|
return [imageInfo];
|
2013-12-30 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private RemoteImageInfo GetImage(BaseItem item, string filename, ImageType type, string remoteFilename)
|
2013-12-30 16:32:01 +00:00
|
|
|
{
|
2020-01-08 16:52:50 +00:00
|
|
|
var list = GetAvailableImages(filename);
|
2013-12-30 16:32:01 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var match = FindMatch(item, list);
|
2013-12-30 16:32:01 +00: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)
|
|
|
|
{
|
2022-04-05 11:52:48 +00:00
|
|
|
return string.Format(CultureInfo.InvariantCulture, "{0}/images/{1}/{2}.jpg", GetRepositoryUrl(), image, filename);
|
2013-12-30 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 03:01:47 +00:00
|
|
|
private Task EnsureThumbsList(string file, CancellationToken cancellationToken)
|
2013-12-30 16:32:01 +00:00
|
|
|
{
|
2022-04-05 11:52:48 +00:00
|
|
|
string url = string.Format(CultureInfo.InvariantCulture, "{0}/thumbs.txt", GetRepositoryUrl());
|
2013-12-30 16:32:01 +00:00
|
|
|
|
2020-08-17 17:46:40 +00:00
|
|
|
return EnsureList(url, file, _fileSystem, cancellationToken);
|
2013-12-30 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <inheritdoc />
|
2020-08-17 17:52:09 +00:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2020-08-31 17:05:21 +00:00
|
|
|
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
2020-08-17 17:52:09 +00:00
|
|
|
return httpClient.GetAsync(url, cancellationToken);
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-04-27 11:08:54 +00:00
|
|
|
/// Ensures the existence of a file listing.
|
2018-09-12 17:26:21 +00:00
|
|
|
/// </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>
|
2022-04-27 11:08:54 +00:00
|
|
|
/// <returns>A Task to ensure existence of a file listing.</returns>
|
2023-06-22 03:01:47 +00:00
|
|
|
public async Task EnsureList(string url, string file, IFileSystem fileSystem, CancellationToken cancellationToken)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var fileInfo = fileSystem.GetFileInfo(file);
|
|
|
|
|
|
|
|
if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
|
|
|
|
{
|
2020-08-31 17:05:21 +00:00
|
|
|
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2020-08-17 17:46:40 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(file));
|
2023-01-11 09:36:18 +00:00
|
|
|
var response = await httpClient.GetStreamAsync(url, cancellationToken).ConfigureAwait(false);
|
|
|
|
await using (response.ConfigureAwait(false))
|
|
|
|
{
|
|
|
|
var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
|
|
|
|
await using (fileStream.ConfigureAwait(false))
|
|
|
|
{
|
|
|
|
await response.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get matching image for an item.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The <see cref="BaseItem"/>.</param>
|
|
|
|
/// <param name="images">The enumerable of image strings.</param>
|
2022-04-27 11:08:54 +00:00
|
|
|
/// <returns>The matching image string.</returns>
|
2018-09-12 17:26:21 +00:00
|
|
|
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 14:12:47 +00: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 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 18:10:37 +00:00
|
|
|
/// <summary>
|
2022-04-27 11:08:54 +00:00
|
|
|
/// Get available image strings for a file.
|
2022-04-15 18:10:37 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="file">The file.</param>
|
2022-04-27 11:08:54 +00:00
|
|
|
/// <returns>All images strings of a file.</returns>
|
2020-01-08 16:52:50 +00:00
|
|
|
public IEnumerable<string> GetAvailableImages(string file)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2021-10-03 17:52:38 +00:00
|
|
|
using var fileStream = File.OpenRead(file);
|
2021-05-13 14:47:00 +00:00
|
|
|
using var reader = new StreamReader(fileStream);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2021-05-10 13:05:12 +00:00
|
|
|
foreach (var line in reader.ReadAllLines())
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2021-05-10 13:05:12 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(line))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2021-10-03 17:52:38 +00:00
|
|
|
yield return line;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 11:52:48 +00:00
|
|
|
|
|
|
|
private string GetRepositoryUrl()
|
|
|
|
=> Plugin.Instance.Configuration.RepositoryUrl;
|
2013-12-30 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|