Remove existing images when applying identify

This commit is contained in:
Joe Rogers 2022-01-03 23:41:13 +01:00
parent ce66df2c92
commit c81d2e9dec
No known key found for this signature in database
GPG Key ID: 0074AD57B8FDBBB4
4 changed files with 63 additions and 0 deletions

View File

@ -30,6 +30,7 @@ namespace MediaBrowser.Controller.Providers
ReplaceAllImages = copy.ReplaceAllImages; ReplaceAllImages = copy.ReplaceAllImages;
ReplaceImages = copy.ReplaceImages; ReplaceImages = copy.ReplaceImages;
SearchResult = copy.SearchResult; SearchResult = copy.SearchResult;
IsIdentify = copy.IsIdentify;
if (copy.RefreshPaths != null && copy.RefreshPaths.Length > 0) if (copy.RefreshPaths != null && copy.RefreshPaths.Length > 0)
{ {

View File

@ -61,6 +61,30 @@ namespace MediaBrowser.Providers.Manager
_fileSystem = fileSystem; _fileSystem = fileSystem;
} }
/// <summary>
/// Removes all existing images from the provided item.
/// </summary>
/// <param name="item">The <see cref="BaseItem"/> to remove images from.</param>
/// <returns><c>true</c> if changes were made to the item; otherwise <c>false</c>.</returns>
public bool RemoveImages(BaseItem item)
{
var singular = new List<ItemImageInfo>();
for (var i = 0; i < _singularImages.Length; i++)
{
var currentImage = item.GetImageInfo(_singularImages[i], 0);
if (currentImage != null)
{
singular.Add(currentImage);
}
}
var oldBackdropImages = item.GetImages(ImageType.Backdrop).ToArray();
var toRemove = singular.Concat(oldBackdropImages).ToArray();
PruneImages(item, toRemove);
return toRemove.Length > 0;
}
/// <summary> /// <summary>
/// Verifies existing images have valid paths and adds any new local images provided. /// Verifies existing images have valid paths and adds any new local images provided.
/// </summary> /// </summary>

View File

@ -98,6 +98,15 @@ namespace MediaBrowser.Providers.Manager
var allImageProviders = ((ProviderManager)ProviderManager).GetImageProviders(item, refreshOptions).ToList(); var allImageProviders = ((ProviderManager)ProviderManager).GetImageProviders(item, refreshOptions).ToList();
// If replacing images with identify purge existing images.
if (refreshOptions.IsIdentify && refreshOptions.ReplaceAllImages)
{
if (ImageProvider.RemoveImages(item))
{
updateType |= ItemUpdateType.ImageUpdate;
}
}
// Start by validating images // Start by validating images
try try
{ {

View File

@ -171,6 +171,35 @@ namespace Jellyfin.Providers.Tests.Manager
} }
} }
[Theory]
[InlineData(ImageType.Primary, 0)]
[InlineData(ImageType.Primary, 1)]
[InlineData(ImageType.Backdrop, 2)]
public void RemoveImages_DeletesImages_WhenFound(ImageType imageType, int imageCount)
{
var item = GetItemWithImages(imageType, imageCount, false);
var mockFileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
if (imageCount > 0)
{
mockFileSystem.Setup(fs => fs.DeleteFile("invalid path 0"))
.Verifiable();
}
if (imageCount > 1)
{
mockFileSystem.Setup(fs => fs.DeleteFile("invalid path 1"))
.Verifiable();
}
var itemImageProvider = GetItemImageProvider(Mock.Of<IProviderManager>(), mockFileSystem);
var result = itemImageProvider.RemoveImages(item);
Assert.Equal(imageCount != 0, result);
Assert.Empty(item.GetImages(imageType));
mockFileSystem.Verify();
}
[Theory] [Theory]
[InlineData(ImageType.Primary, 1, false)] [InlineData(ImageType.Primary, 1, false)]
[InlineData(ImageType.Backdrop, 2, false)] [InlineData(ImageType.Backdrop, 2, false)]