2021-05-20 21:28:18 +02:00
|
|
|
#nullable disable
|
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2016-12-12 06:49:19 +01:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2021-12-20 13:31:07 +01:00
|
|
|
using Jellyfin.Extensions;
|
2016-12-12 06:49:19 +01:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Library.Resolvers.Books
|
|
|
|
{
|
|
|
|
public class BookResolver : MediaBrowser.Controller.Resolvers.ItemResolver<Book>
|
|
|
|
{
|
2020-12-12 03:13:28 +01:00
|
|
|
private readonly string[] _validExtensions = { ".azw", ".azw3", ".cb7", ".cbr", ".cbt", ".cbz", ".epub", ".mobi", ".pdf" };
|
2016-12-12 06:49:19 +01:00
|
|
|
|
2021-03-14 19:56:45 +01:00
|
|
|
public override Book Resolve(ItemResolveArgs args)
|
2016-12-12 06:49:19 +01:00
|
|
|
{
|
|
|
|
var collectionType = args.GetCollectionType();
|
|
|
|
|
|
|
|
// Only process items that are in a collection folder containing books
|
|
|
|
if (!string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
|
2020-06-20 11:12:36 +02:00
|
|
|
{
|
2016-12-12 06:49:19 +01:00
|
|
|
return null;
|
2020-06-20 11:12:36 +02:00
|
|
|
}
|
2017-10-06 21:45:05 +02:00
|
|
|
|
2016-12-12 06:49:19 +01:00
|
|
|
if (args.IsDirectory)
|
|
|
|
{
|
|
|
|
return GetBook(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
var extension = Path.GetExtension(args.Path);
|
|
|
|
|
2021-12-20 13:31:07 +01:00
|
|
|
if (extension != null && _validExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
|
2016-12-12 06:49:19 +01:00
|
|
|
{
|
|
|
|
// It's a book
|
|
|
|
return new Book
|
|
|
|
{
|
|
|
|
Path = args.Path,
|
|
|
|
IsInMixedFolder = true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Book GetBook(ItemResolveArgs args)
|
|
|
|
{
|
|
|
|
var bookFiles = args.FileSystemChildren.Where(f =>
|
|
|
|
{
|
2021-10-02 19:59:58 +02:00
|
|
|
var fileExtension = Path.GetExtension(f.FullName)
|
|
|
|
?? string.Empty;
|
2016-12-12 06:49:19 +01:00
|
|
|
|
2020-10-12 20:05:11 +02:00
|
|
|
return _validExtensions.Contains(
|
|
|
|
fileExtension,
|
2021-10-02 19:59:58 +02:00
|
|
|
StringComparer.OrdinalIgnoreCase);
|
2016-12-12 06:49:19 +01:00
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
// Don't return a Book if there is more (or less) than one document in the directory
|
|
|
|
if (bookFiles.Count != 1)
|
2020-06-20 11:12:36 +02:00
|
|
|
{
|
2016-12-12 06:49:19 +01:00
|
|
|
return null;
|
2020-06-20 11:12:36 +02:00
|
|
|
}
|
2016-12-12 06:49:19 +01:00
|
|
|
|
|
|
|
return new Book
|
2017-10-06 21:45:05 +02:00
|
|
|
{
|
|
|
|
Path = bookFiles[0].FullName
|
|
|
|
};
|
2016-12-12 06:49:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|