2015-04-08 07:38:02 -07:00
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-02-12 22:11:54 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-08-28 21:06:30 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-12 22:11:54 -07:00
|
|
|
|
using System;
|
2014-08-28 21:06:30 -07:00
|
|
|
|
using System.IO;
|
2014-02-12 22:11:54 -07:00
|
|
|
|
using System.Linq;
|
2016-07-10 21:57:22 -07:00
|
|
|
|
using CommonIO;
|
2016-08-12 22:49:00 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-02-12 22:11:54 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library.Resolvers
|
|
|
|
|
{
|
|
|
|
|
public class PhotoResolver : ItemResolver<Photo>
|
|
|
|
|
{
|
2015-04-08 07:38:02 -07:00
|
|
|
|
private readonly IImageProcessor _imageProcessor;
|
2016-07-10 21:57:22 -07:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
|
|
public PhotoResolver(IImageProcessor imageProcessor, ILibraryManager libraryManager)
|
2015-04-08 07:38:02 -07:00
|
|
|
|
{
|
|
|
|
|
_imageProcessor = imageProcessor;
|
2016-07-10 21:57:22 -07:00
|
|
|
|
_libraryManager = libraryManager;
|
2015-04-08 07:38:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-12 22:11:54 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves the specified args.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
|
/// <returns>Trailer.</returns>
|
|
|
|
|
protected override Photo Resolve(ItemResolveArgs args)
|
|
|
|
|
{
|
2016-07-10 21:57:22 -07:00
|
|
|
|
if (!args.IsDirectory)
|
2014-02-12 22:11:54 -07:00
|
|
|
|
{
|
2016-07-10 21:57:22 -07:00
|
|
|
|
// Must be an image file within a photo collection
|
|
|
|
|
var collectionType = args.GetCollectionType();
|
|
|
|
|
|
|
|
|
|
if (string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
|
2014-02-12 22:11:54 -07:00
|
|
|
|
{
|
2016-07-10 21:57:22 -07:00
|
|
|
|
if (IsImageFile(args.Path, _imageProcessor))
|
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(args.Path);
|
|
|
|
|
|
|
|
|
|
// Make sure the image doesn't belong to a video file
|
2016-08-12 22:49:00 -07:00
|
|
|
|
if (args.DirectoryService.GetFiles(Path.GetDirectoryName(args.Path)).Any(i => IsOwnedByMedia(args.GetLibraryOptions(), i, filename)))
|
2016-07-10 21:57:22 -07:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Photo
|
|
|
|
|
{
|
|
|
|
|
Path = args.Path
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-12 22:11:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 22:49:00 -07:00
|
|
|
|
private bool IsOwnedByMedia(LibraryOptions libraryOptions, FileSystemMetadata file, string imageFilename)
|
2016-07-10 21:57:22 -07:00
|
|
|
|
{
|
2016-08-12 22:49:00 -07:00
|
|
|
|
if (_libraryManager.IsVideoFile(file.FullName, libraryOptions) && imageFilename.StartsWith(Path.GetFileNameWithoutExtension(file.Name), StringComparison.OrdinalIgnoreCase))
|
2016-07-10 21:57:22 -07:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-11 18:46:02 -07:00
|
|
|
|
private static readonly string[] IgnoreFiles =
|
|
|
|
|
{
|
|
|
|
|
"folder",
|
|
|
|
|
"thumb",
|
|
|
|
|
"landscape",
|
|
|
|
|
"fanart",
|
|
|
|
|
"backdrop",
|
2016-07-10 21:57:22 -07:00
|
|
|
|
"poster",
|
|
|
|
|
"cover"
|
2014-10-11 18:46:02 -07:00
|
|
|
|
};
|
|
|
|
|
|
2015-04-08 07:38:02 -07:00
|
|
|
|
internal static bool IsImageFile(string path, IImageProcessor imageProcessor)
|
2014-02-12 22:11:54 -07:00
|
|
|
|
{
|
2014-10-11 18:46:02 -07:00
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(path) ?? string.Empty;
|
2014-08-28 21:06:30 -07:00
|
|
|
|
|
2016-01-27 10:34:23 -07:00
|
|
|
|
if (IgnoreFiles.Contains(filename, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IgnoreFiles.Any(i => filename.IndexOf("-" + i, StringComparison.OrdinalIgnoreCase) != -1))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return imageProcessor.SupportedInputFormats.Contains((Path.GetExtension(path) ?? string.Empty).TrimStart('.'), StringComparer.OrdinalIgnoreCase);
|
2014-02-12 22:11:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|