2018-03-14 20:41:36 +00:00
|
|
|
using System;
|
2013-06-13 01:37:05 +00:00
|
|
|
using System.Collections.Generic;
|
2013-03-04 05:53:02 +00:00
|
|
|
using System.IO;
|
2020-10-07 20:45:04 +00:00
|
|
|
using System.Linq;
|
2013-07-05 05:16:49 +00:00
|
|
|
using System.Net;
|
2019-10-14 20:21:00 +00:00
|
|
|
using System.Threading;
|
2013-03-04 05:53:02 +00:00
|
|
|
using NLog;
|
2020-10-07 20:45:04 +00:00
|
|
|
using NzbDrone.Common;
|
2014-01-06 06:20:08 +00:00
|
|
|
using NzbDrone.Common.Disk;
|
2013-06-28 00:04:52 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2014-05-11 04:37:07 +00:00
|
|
|
using NzbDrone.Common.Http;
|
2014-01-02 06:56:19 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-09-14 06:36:07 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2018-03-14 20:41:36 +00:00
|
|
|
using NzbDrone.Core.Movies;
|
|
|
|
using NzbDrone.Core.Movies.Events;
|
2013-03-04 05:53:02 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaCover
|
|
|
|
{
|
2014-01-22 05:22:09 +00:00
|
|
|
public interface IMapCoversToLocal
|
|
|
|
{
|
2020-10-07 20:45:04 +00:00
|
|
|
Dictionary<string, FileInfo> GetCoverFileInfos();
|
|
|
|
void ConvertToLocalUrls(int movieId, IEnumerable<MediaCover> covers, Dictionary<string, FileInfo> fileInfos = null);
|
2021-02-03 21:26:10 +00:00
|
|
|
void ConvertToLocalUrls(IEnumerable<Tuple<int, IEnumerable<MediaCover>>> items, Dictionary<string, FileInfo> coverFileInfos);
|
2023-05-22 17:28:18 +00:00
|
|
|
string GetCoverPath(int movieId, MediaCoverTypes coverType, int? height = null);
|
2014-01-22 05:22:09 +00:00
|
|
|
}
|
|
|
|
|
2013-03-05 19:49:34 +00:00
|
|
|
public class MediaCoverService :
|
2016-12-29 16:38:54 +00:00
|
|
|
IHandleAsync<MovieUpdatedEvent>,
|
2020-06-05 03:35:02 +00:00
|
|
|
IHandleAsync<MoviesDeletedEvent>,
|
2013-06-13 01:37:05 +00:00
|
|
|
IMapCoversToLocal
|
2013-03-04 05:53:02 +00:00
|
|
|
{
|
2020-08-15 03:58:42 +00:00
|
|
|
private readonly IMediaCoverProxy _mediaCoverProxy;
|
2015-01-22 22:12:35 +00:00
|
|
|
private readonly IImageResizer _resizer;
|
2014-09-11 23:49:41 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
2013-05-10 23:53:50 +00:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-05-29 04:10:23 +00:00
|
|
|
private readonly ICoverExistsSpecification _coverExistsSpecification;
|
2014-01-02 06:56:19 +00:00
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
2014-01-26 07:14:55 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2013-03-04 05:53:02 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
private readonly string _coverRootFolder;
|
|
|
|
|
2019-10-14 20:21:00 +00:00
|
|
|
// ImageSharp is slow on ARM (no hardware acceleration on mono yet)
|
|
|
|
// So limit the number of concurrent resizing tasks
|
|
|
|
private static SemaphoreSlim _semaphore = new SemaphoreSlim((int)Math.Ceiling(Environment.ProcessorCount / 2.0));
|
|
|
|
|
2020-08-15 03:58:42 +00:00
|
|
|
public MediaCoverService(IMediaCoverProxy mediaCoverProxy,
|
|
|
|
IImageResizer resizer,
|
2015-01-22 22:12:35 +00:00
|
|
|
IHttpClient httpClient,
|
2014-01-26 07:14:55 +00:00
|
|
|
IDiskProvider diskProvider,
|
|
|
|
IAppFolderInfo appFolderInfo,
|
2014-09-11 23:49:41 +00:00
|
|
|
ICoverExistsSpecification coverExistsSpecification,
|
|
|
|
IConfigFileProvider configFileProvider,
|
2014-01-26 07:14:55 +00:00
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
Logger logger)
|
2013-03-04 05:53:02 +00:00
|
|
|
{
|
2020-08-15 03:58:42 +00:00
|
|
|
_mediaCoverProxy = mediaCoverProxy;
|
2015-01-22 22:12:35 +00:00
|
|
|
_resizer = resizer;
|
2014-09-11 23:49:41 +00:00
|
|
|
_httpClient = httpClient;
|
2013-03-04 05:53:02 +00:00
|
|
|
_diskProvider = diskProvider;
|
2013-05-29 04:10:23 +00:00
|
|
|
_coverExistsSpecification = coverExistsSpecification;
|
2014-01-02 06:56:19 +00:00
|
|
|
_configFileProvider = configFileProvider;
|
2014-01-26 07:14:55 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2013-03-04 05:53:02 +00:00
|
|
|
_logger = logger;
|
|
|
|
|
2013-07-05 04:43:28 +00:00
|
|
|
_coverRootFolder = appFolderInfo.GetMediaCoverPath();
|
2013-03-04 05:53:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-22 17:28:18 +00:00
|
|
|
public string GetCoverPath(int movieId, MediaCoverTypes coverType, int? height = null)
|
2013-03-04 05:53:02 +00:00
|
|
|
{
|
2015-01-22 22:12:35 +00:00
|
|
|
var heightSuffix = height.HasValue ? "-" + height.ToString() : "";
|
|
|
|
|
2023-05-22 17:28:18 +00:00
|
|
|
return Path.Combine(GetMovieCoverPath(movieId), coverType.ToString().ToLower() + heightSuffix + GetExtension(coverType));
|
2014-01-22 05:22:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 20:45:04 +00:00
|
|
|
public Dictionary<string, FileInfo> GetCoverFileInfos()
|
|
|
|
{
|
|
|
|
if (!_diskProvider.FolderExists(_coverRootFolder))
|
|
|
|
{
|
|
|
|
return new Dictionary<string, FileInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _diskProvider
|
|
|
|
.GetFileInfos(_coverRootFolder, SearchOption.AllDirectories)
|
|
|
|
.ToDictionary(x => x.FullName, PathEqualityComparer.Instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ConvertToLocalUrls(int movieId, IEnumerable<MediaCover> covers, Dictionary<string, FileInfo> fileInfos = null)
|
2014-01-22 05:22:09 +00:00
|
|
|
{
|
2020-08-15 03:58:42 +00:00
|
|
|
if (movieId == 0)
|
2014-01-22 05:22:09 +00:00
|
|
|
{
|
2020-08-15 03:58:42 +00:00
|
|
|
// Movie isn't in Radarr yet, map via a proxy to circument referrer issues
|
|
|
|
foreach (var mediaCover in covers)
|
|
|
|
{
|
|
|
|
mediaCover.RemoteUrl = mediaCover.Url;
|
|
|
|
mediaCover.Url = _mediaCoverProxy.RegisterUrl(mediaCover.RemoteUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (var mediaCover in covers)
|
|
|
|
{
|
2023-05-22 17:28:18 +00:00
|
|
|
if (mediaCover.CoverType == MediaCoverTypes.Unknown)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-15 03:58:42 +00:00
|
|
|
var filePath = GetCoverPath(movieId, mediaCover.CoverType);
|
2014-01-22 05:22:09 +00:00
|
|
|
|
2020-08-15 03:58:42 +00:00
|
|
|
mediaCover.RemoteUrl = mediaCover.Url;
|
2023-05-22 17:28:18 +00:00
|
|
|
mediaCover.Url = _configFileProvider.UrlBase + @"/MediaCover/" + movieId + "/" + mediaCover.CoverType.ToString().ToLower() + GetExtension(mediaCover.CoverType);
|
2014-01-22 05:22:09 +00:00
|
|
|
|
2020-10-07 20:45:04 +00:00
|
|
|
FileInfo file;
|
|
|
|
var fileExists = false;
|
|
|
|
if (fileInfos != null)
|
|
|
|
{
|
|
|
|
fileExists = fileInfos.TryGetValue(filePath, out file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file = _diskProvider.GetFileInfo(filePath);
|
|
|
|
fileExists = file.Exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileExists)
|
2020-08-15 03:58:42 +00:00
|
|
|
{
|
2020-10-07 20:45:04 +00:00
|
|
|
var lastWrite = file.LastWriteTimeUtc;
|
2020-08-15 03:58:42 +00:00
|
|
|
mediaCover.Url += "?lastWrite=" + lastWrite.Ticks;
|
|
|
|
}
|
2019-12-30 15:00:34 +00:00
|
|
|
}
|
2014-01-22 05:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:26:10 +00:00
|
|
|
public void ConvertToLocalUrls(IEnumerable<Tuple<int, IEnumerable<MediaCover>>> items, Dictionary<string, FileInfo> coverFileInfos)
|
|
|
|
{
|
|
|
|
foreach (var movie in items)
|
|
|
|
{
|
|
|
|
ConvertToLocalUrls(movie.Item1, movie.Item2, coverFileInfos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
private string GetMovieCoverPath(int movieId)
|
2014-01-22 05:22:09 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
return Path.Combine(_coverRootFolder, movieId.ToString());
|
2013-03-04 05:53:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 21:29:15 +00:00
|
|
|
private bool EnsureCovers(Movie movie)
|
2016-12-29 16:38:54 +00:00
|
|
|
{
|
2023-05-23 10:52:39 +00:00
|
|
|
var updated = false;
|
2019-10-14 20:21:00 +00:00
|
|
|
var toResize = new List<Tuple<MediaCover, bool>>();
|
|
|
|
|
2022-03-20 15:55:47 +00:00
|
|
|
foreach (var cover in movie.MovieMetadata.Value.Images)
|
2016-12-29 16:38:54 +00:00
|
|
|
{
|
2023-05-22 16:00:36 +00:00
|
|
|
if (cover.CoverType == MediaCoverTypes.Unknown)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-29 16:38:54 +00:00
|
|
|
var fileName = GetCoverPath(movie.Id, cover.CoverType);
|
|
|
|
var alreadyExists = false;
|
2023-05-22 16:00:36 +00:00
|
|
|
|
2016-12-29 16:38:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
alreadyExists = _coverExistsSpecification.AlreadyExists(cover.Url, fileName);
|
2023-05-22 16:00:36 +00:00
|
|
|
|
2016-12-29 16:38:54 +00:00
|
|
|
if (!alreadyExists)
|
|
|
|
{
|
|
|
|
DownloadCover(movie, cover);
|
2019-12-19 21:29:10 +00:00
|
|
|
updated = true;
|
2016-12-29 16:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 03:58:42 +00:00
|
|
|
catch (HttpException e)
|
|
|
|
{
|
|
|
|
_logger.Warn("Couldn't download media cover for {0}. {1}", movie, e.Message);
|
|
|
|
}
|
2016-12-29 16:38:54 +00:00
|
|
|
catch (WebException e)
|
|
|
|
{
|
2019-12-30 15:00:34 +00:00
|
|
|
_logger.Warn("Couldn't download media cover for {0}. {1}", movie, e.Message);
|
2016-12-29 16:38:54 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2019-09-01 14:52:01 +00:00
|
|
|
_logger.Error(e, "Couldn't download media cover for {0}", movie);
|
2016-12-29 16:38:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 20:21:00 +00:00
|
|
|
toResize.Add(Tuple.Create(cover, alreadyExists));
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_semaphore.Wait();
|
|
|
|
|
|
|
|
foreach (var tuple in toResize)
|
|
|
|
{
|
|
|
|
EnsureResizedCovers(movie, tuple.Item1, !tuple.Item2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_semaphore.Release();
|
2016-12-29 16:38:54 +00:00
|
|
|
}
|
2019-12-19 21:29:10 +00:00
|
|
|
|
|
|
|
return updated;
|
2016-12-29 16:38:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
private void DownloadCover(Movie movie, MediaCover cover)
|
2016-12-29 16:38:54 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
var fileName = GetCoverPath(movie.Id, cover.CoverType);
|
2016-12-29 16:38:54 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Info("Downloading {0} for {1} {2}", cover.CoverType, movie, cover.Url);
|
2016-12-29 16:38:54 +00:00
|
|
|
_httpClient.DownloadFile(cover.Url, fileName);
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
private void EnsureResizedCovers(Movie movie, MediaCover cover, bool forceResize)
|
2016-12-29 16:38:54 +00:00
|
|
|
{
|
|
|
|
int[] heights;
|
|
|
|
|
|
|
|
switch (cover.CoverType)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case MediaCoverTypes.Poster:
|
|
|
|
case MediaCoverTypes.Headshot:
|
|
|
|
heights = new[] { 500, 250 };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MediaCoverTypes.Banner:
|
|
|
|
heights = new[] { 70, 35 };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MediaCoverTypes.Fanart:
|
|
|
|
case MediaCoverTypes.Screenshot:
|
|
|
|
heights = new[] { 360, 180 };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var height in heights)
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
var mainFileName = GetCoverPath(movie.Id, cover.CoverType);
|
|
|
|
var resizeFileName = GetCoverPath(movie.Id, cover.CoverType, height);
|
2016-12-29 16:38:54 +00:00
|
|
|
|
|
|
|
if (forceResize || !_diskProvider.FileExists(resizeFileName) || _diskProvider.GetFileSize(resizeFileName) == 0)
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Debug("Resizing {0}-{1} for {2}", cover.CoverType, height, movie);
|
2016-12-29 16:38:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_resizer.Resize(mainFileName, resizeFileName, height);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Debug("Couldn't resize media cover {0}-{1} for {2}, using full size image instead.", cover.CoverType, height, movie);
|
2016-12-29 16:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-22 17:28:18 +00:00
|
|
|
private string GetExtension(MediaCoverTypes coverType)
|
|
|
|
{
|
|
|
|
return coverType switch
|
|
|
|
{
|
|
|
|
MediaCoverTypes.Clearlogo => ".png",
|
|
|
|
_ => ".jpg"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-29 16:38:54 +00:00
|
|
|
public void HandleAsync(MovieUpdatedEvent message)
|
|
|
|
{
|
2019-12-19 21:29:10 +00:00
|
|
|
var updated = EnsureCovers(message.Movie);
|
2020-03-12 02:09:08 +00:00
|
|
|
|
|
|
|
_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie, updated));
|
2016-12-29 16:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 03:35:02 +00:00
|
|
|
public void HandleAsync(MoviesDeletedEvent message)
|
2013-03-05 19:49:34 +00:00
|
|
|
{
|
2020-06-05 03:35:02 +00:00
|
|
|
foreach (var movie in message.Movies)
|
2013-03-05 19:49:34 +00:00
|
|
|
{
|
2020-06-05 03:35:02 +00:00
|
|
|
var path = GetMovieCoverPath(movie.Id);
|
|
|
|
if (_diskProvider.FolderExists(path))
|
|
|
|
{
|
|
|
|
_diskProvider.DeleteFolder(path, true);
|
|
|
|
}
|
2013-03-05 19:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-04 05:53:02 +00:00
|
|
|
}
|
|
|
|
}
|