2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2016-12-11 22:49:19 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Library.Resolvers.Books
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-01-07 16:27:46 -07:00
|
|
|
///
|
2016-12-11 22:49:19 -07:00
|
|
|
/// </summary>
|
|
|
|
public class BookResolver : MediaBrowser.Controller.Resolvers.ItemResolver<Book>
|
|
|
|
{
|
2017-10-06 12:45:05 -07:00
|
|
|
private readonly string[] _validExtensions = { ".pdf", ".epub", ".mobi", ".cbr", ".cbz", ".azw3" };
|
2016-12-11 22:49:19 -07:00
|
|
|
|
|
|
|
/// <summary>
|
2019-01-07 16:27:46 -07:00
|
|
|
///
|
2016-12-11 22:49:19 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="args"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
protected override Book Resolve(ItemResolveArgs args)
|
|
|
|
{
|
|
|
|
var collectionType = args.GetCollectionType();
|
|
|
|
|
|
|
|
// Only process items that are in a collection folder containing books
|
|
|
|
if (!string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
|
|
|
|
return null;
|
2017-10-06 12:45:05 -07:00
|
|
|
|
2016-12-11 22:49:19 -07:00
|
|
|
if (args.IsDirectory)
|
|
|
|
{
|
|
|
|
return GetBook(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
var extension = Path.GetExtension(args.Path);
|
|
|
|
|
|
|
|
if (extension != null && _validExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
// It's a book
|
|
|
|
return new Book
|
|
|
|
{
|
|
|
|
Path = args.Path,
|
|
|
|
IsInMixedFolder = true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-07 16:27:46 -07:00
|
|
|
///
|
2016-12-11 22:49:19 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="args"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
private Book GetBook(ItemResolveArgs args)
|
|
|
|
{
|
|
|
|
var bookFiles = args.FileSystemChildren.Where(f =>
|
|
|
|
{
|
|
|
|
var fileExtension = Path.GetExtension(f.FullName) ??
|
|
|
|
string.Empty;
|
|
|
|
|
|
|
|
return _validExtensions.Contains(fileExtension,
|
|
|
|
StringComparer
|
|
|
|
.OrdinalIgnoreCase);
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
// Don't return a Book if there is more (or less) than one document in the directory
|
|
|
|
if (bookFiles.Count != 1)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return new Book
|
2017-10-06 12:45:05 -07:00
|
|
|
{
|
|
|
|
Path = bookFiles[0].FullName
|
|
|
|
};
|
2016-12-11 22:49:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|