jellyfin/MediaBrowser.Providers/TV/EpisodeLocalImageProvider.cs
2014-02-10 13:39:41 -05:00

105 lines
3.4 KiB
C#

using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Providers.TV
{
public class EpisodeLocalLocalImageProvider : ILocalImageFileProvider
{
public string Name
{
get { return "Local Images"; }
}
public bool Supports(IHasImages item)
{
return item is Episode && item.LocationType == LocationType.FileSystem;
}
public List<LocalImageInfo> GetImages(IHasImages item, IDirectoryService directoryService)
{
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;
}
var metadataPath = Path.Combine(parentPath, "metadata");
if (parentPathFiles.Any(i => string.Equals(i.FullName, metadataPath, StringComparison.OrdinalIgnoreCase)))
{
return GetFilesFromParentFolder(nameWithoutExtension, directoryService.GetFiles(metadataPath));
}
return new List<LocalImageInfo>();
}
private List<LocalImageInfo> GetFilesFromParentFolder(string filenameWithoutExtension, IEnumerable<FileSystemInfo> parentPathFiles)
{
var thumbName = filenameWithoutExtension + "-thumb";
return parentPathFiles
.Where(i =>
{
if (BaseItem.SupportedImageExtensions.Contains(i.Extension))
{
var currentNameWithoutExtension = Path.GetFileNameWithoutExtension(i.Name);
if (string.Equals(filenameWithoutExtension, currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (string.Equals(thumbName, currentNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
})
.Select(i => new LocalImageInfo
{
FileInfo = (FileInfo)i,
Type = ImageType.Primary
})
.ToList();
}
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;
}
}
return false;
})
.Select(i => new LocalImageInfo
{
FileInfo = i,
Type = ImageType.Primary
})
.ToList();
}
}
}