jellyfin/MediaBrowser.Providers/TV/EpisodeLocalImageProvider.cs

105 lines
3.4 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2014-02-08 15:38:02 -07:00
using System;
using System.Collections.Generic;
using System.IO;
2014-02-08 15:38:02 -07:00
using System.Linq;
namespace MediaBrowser.Providers.TV
{
2014-02-08 13:02:35 -07:00
public class EpisodeLocalLocalImageProvider : ILocalImageFileProvider
{
public string Name
{
get { return "Local Images"; }
}
public bool Supports(IHasImages item)
{
return item is Episode && item.LocationType == LocationType.FileSystem;
}
2014-02-08 15:38:02 -07:00
public List<LocalImageInfo> GetImages(IHasImages item, DirectoryService directoryService)
2014-02-08 13:02:35 -07:00
{
2014-02-08 15:38:02 -07:00
var parentPath = Path.GetDirectoryName(item.Path);
var parentPathFiles = directoryService.GetFileSystemEntries(parentPath);
var nameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path);
var files = GetFilesFromParentFolder(nameWithoutExtension, parentPathFiles);
if (files.Count > 0)
{
return files;
}
2014-02-08 13:02:35 -07:00
2014-02-08 15:38:02 -07:00
var metadataPath = Path.Combine(parentPath, "metadata");
2014-02-08 13:02:35 -07:00
2014-02-08 15:38:02 -07:00
if (parentPathFiles.Any(i => string.Equals(i.FullName, metadataPath, StringComparison.OrdinalIgnoreCase)))
2014-02-08 13:02:35 -07:00
{
2014-02-08 15:38:02 -07:00
return GetFilesFromParentFolder(nameWithoutExtension, directoryService.GetFiles(metadataPath));
2014-02-08 13:02:35 -07:00
}
2014-02-08 15:38:02 -07:00
return new List<LocalImageInfo>();
2014-02-08 13:02:35 -07:00
}
2014-02-08 15:38:02 -07:00
private List<LocalImageInfo> GetFilesFromParentFolder(string filenameWithoutExtension, IEnumerable<FileSystemInfo> parentPathFiles)
{
2014-02-08 15:38:02 -07:00
var thumbName = filenameWithoutExtension + "-thumb";
2014-02-08 15:38:02 -07:00
return parentPathFiles
.Where(i =>
{
if (BaseItem.SupportedImageExtensions.Contains(i.Extension))
{
var currentNameWithoutExtension = Path.GetFileNameWithoutExtension(i.Name);
2014-02-08 15:38:02 -07:00
if (string.Equals(filenameWithoutExtension, currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
return true;
}
2014-02-08 13:02:35 -07:00
2014-02-08 15:38:02 -07:00
if (string.Equals(thumbName, currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
2014-02-08 13:02:35 -07:00
2014-02-08 15:38:02 -07:00
return false;
})
.Select(i => new LocalImageInfo
{
FileInfo = (FileInfo)i,
Type = ImageType.Primary
})
.ToList();
}
2014-02-08 13:02:35 -07:00
2014-02-08 15:38:02 -07:00
private List<LocalImageInfo> GetFilesFromMetadataFolder(string filenameWithoutExtension, IEnumerable<FileInfo> metadataFiles)
{
return metadataFiles
.Where(i =>
{
if (BaseItem.SupportedImageExtensions.Contains(i.Extension))
{
if (string.Equals(filenameWithoutExtension, Path.GetFileNameWithoutExtension(i.Name), StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
2014-02-08 13:02:35 -07:00
2014-02-08 15:38:02 -07:00
return false;
})
.Select(i => new LocalImageInfo
{
FileInfo = i,
Type = ImageType.Primary
})
.ToList();
}
}
}