2014-02-23 13:35:58 -07:00
|
|
|
using System;
|
2019-01-13 12:21:32 -07:00
|
|
|
using System.Collections.Generic;
|
2017-06-15 10:22:05 -07:00
|
|
|
using System.IO;
|
2017-11-16 14:25:18 -07:00
|
|
|
using System.Linq;
|
2019-01-13 12:21:32 -07:00
|
|
|
using Emby.Naming.AudioBook;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2017-11-16 14:25:18 -07:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2019-01-13 12:21:32 -07:00
|
|
|
using MediaBrowser.Controller.Resolvers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2017-11-16 14:25:18 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
2013-03-02 23:58:04 -07:00
|
|
|
|
2016-11-02 23:37:52 -07:00
|
|
|
namespace Emby.Server.Implementations.Library.Resolvers.Audio
|
2013-03-02 23:58:04 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class AudioResolver
|
|
|
|
/// </summary>
|
2017-11-16 14:25:18 -07:00
|
|
|
public class AudioResolver : ItemResolver<MediaBrowser.Controller.Entities.Audio.Audio>, IMultiItemResolver
|
2013-03-02 23:58:04 -07:00
|
|
|
{
|
2017-11-16 14:25:18 -07:00
|
|
|
private readonly ILibraryManager LibraryManager;
|
2014-11-16 13:44:08 -07:00
|
|
|
|
|
|
|
public AudioResolver(ILibraryManager libraryManager)
|
|
|
|
{
|
2017-11-16 14:25:18 -07:00
|
|
|
LibraryManager = libraryManager;
|
2014-11-16 13:44:08 -07:00
|
|
|
}
|
|
|
|
|
2013-03-02 23:58:04 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the priority.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The priority.</value>
|
2019-01-06 13:50:43 -07:00
|
|
|
public override ResolverPriority Priority => ResolverPriority.Fourth;
|
2013-03-02 23:58:04 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
public MultiItemResolverResult ResolveMultiple(Folder parent,
|
|
|
|
List<FileSystemMetadata> files,
|
|
|
|
string collectionType,
|
|
|
|
IDirectoryService directoryService)
|
|
|
|
{
|
|
|
|
var result = ResolveMultipleInternal(parent, files, collectionType, directoryService);
|
|
|
|
|
|
|
|
if (result != null)
|
|
|
|
{
|
|
|
|
foreach (var item in result.Items)
|
|
|
|
{
|
|
|
|
SetInitialItemValues((MediaBrowser.Controller.Entities.Audio.Audio)item, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private MultiItemResolverResult ResolveMultipleInternal(Folder parent,
|
|
|
|
List<FileSystemMetadata> files,
|
|
|
|
string collectionType,
|
|
|
|
IDirectoryService directoryService)
|
|
|
|
{
|
|
|
|
if (string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return ResolveMultipleAudio<AudioBook>(parent, files, directoryService, false, collectionType, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-03-02 23:58:04 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Resolves the specified args.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
/// <returns>Entities.Audio.Audio.</returns>
|
2016-11-02 23:37:52 -07:00
|
|
|
protected override MediaBrowser.Controller.Entities.Audio.Audio Resolve(ItemResolveArgs args)
|
2013-03-02 23:58:04 -07:00
|
|
|
{
|
|
|
|
// Return audio if the path is a file and has a matching extension
|
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
var libraryOptions = args.GetLibraryOptions();
|
|
|
|
var collectionType = args.GetCollectionType();
|
|
|
|
|
|
|
|
var isBooksCollectionType = string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
if (args.IsDirectory)
|
2013-03-02 23:58:04 -07:00
|
|
|
{
|
2017-11-16 14:25:18 -07:00
|
|
|
if (!isBooksCollectionType)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var files = args.FileSystemChildren
|
|
|
|
.Where(i => !LibraryManager.IgnoreFile(i, args.Parent))
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
if (isBooksCollectionType)
|
|
|
|
{
|
|
|
|
return FindAudio<AudioBook>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-12 22:49:00 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
if (LibraryManager.IsAudioFile(args.Path, libraryOptions))
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
var extension = Path.GetExtension(args.Path);
|
|
|
|
|
|
|
|
if (string.Equals(extension, ".cue", StringComparison.OrdinalIgnoreCase))
|
2013-03-02 23:58:04 -07:00
|
|
|
{
|
2017-11-16 14:25:18 -07:00
|
|
|
// if audio file exists of same name, return null
|
|
|
|
return null;
|
|
|
|
}
|
2017-06-15 10:22:05 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var isMixedCollectionType = string.IsNullOrEmpty(collectionType);
|
2017-06-15 10:22:05 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
// For conflicting extensions, give priority to videos
|
|
|
|
if (isMixedCollectionType && LibraryManager.IsVideoFile(args.Path, libraryOptions))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-23 13:35:58 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
MediaBrowser.Controller.Entities.Audio.Audio item = null;
|
2014-12-10 23:20:28 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
var isMusicCollectionType = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
|
2014-12-10 23:20:28 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
// Use regular audio type for mixed libraries, owned items and music
|
|
|
|
if (isMixedCollectionType ||
|
|
|
|
args.Parent == null ||
|
|
|
|
isMusicCollectionType)
|
|
|
|
{
|
|
|
|
item = new MediaBrowser.Controller.Entities.Audio.Audio();
|
|
|
|
}
|
2014-04-14 20:54:52 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
else if (isBooksCollectionType)
|
|
|
|
{
|
|
|
|
item = new AudioBook();
|
|
|
|
}
|
2016-12-11 22:49:19 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
if (item != null)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
item.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
item.IsInMixedFolder = true;
|
2013-03-02 23:58:04 -07:00
|
|
|
}
|
2017-11-16 14:25:18 -07:00
|
|
|
|
|
|
|
return item;
|
2013-03-02 23:58:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-11-16 14:25:18 -07:00
|
|
|
|
|
|
|
private T FindAudio<T>(ItemResolveArgs args, string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool parseName)
|
|
|
|
where T : MediaBrowser.Controller.Entities.Audio.Audio, new()
|
|
|
|
{
|
|
|
|
var multiDiscFolders = new List<FileSystemMetadata>();
|
|
|
|
|
|
|
|
var libraryOptions = args.GetLibraryOptions();
|
|
|
|
var filesFromOtherItems = new List<FileSystemMetadata>();
|
|
|
|
|
|
|
|
// TODO: Allow GetMultiDiscMovie in here
|
|
|
|
var supportsMultiVersion = false;
|
|
|
|
|
|
|
|
var result = ResolveMultipleAudio<T>(parent, fileSystemEntries, directoryService, supportsMultiVersion, collectionType, parseName) ??
|
|
|
|
new MultiItemResolverResult();
|
|
|
|
|
|
|
|
if (result.Items.Count == 1)
|
|
|
|
{
|
|
|
|
var videoPath = result.Items[0].Path;
|
|
|
|
|
|
|
|
// If we were supporting this we'd be checking filesFromOtherItems
|
|
|
|
var hasOtherItems = false;
|
|
|
|
|
|
|
|
if (!hasOtherItems)
|
|
|
|
{
|
|
|
|
var item = (T)result.Items[0];
|
|
|
|
item.IsInMixedFolder = false;
|
|
|
|
item.Name = Path.GetFileName(item.ContainingFolderPath);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
|
|
|
|
{
|
|
|
|
//return GetMultiDiscAudio<T>(multiDiscFolders, directoryService);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private MultiItemResolverResult ResolveMultipleAudio<T>(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, bool suppportMultiEditions, string collectionType, bool parseName)
|
|
|
|
where T : MediaBrowser.Controller.Entities.Audio.Audio, new()
|
|
|
|
{
|
|
|
|
var files = new List<FileSystemMetadata>();
|
|
|
|
var items = new List<BaseItem>();
|
|
|
|
var leftOver = new List<FileSystemMetadata>();
|
|
|
|
|
|
|
|
// Loop through each child file/folder and see if we find a video
|
|
|
|
foreach (var child in fileSystemEntries)
|
|
|
|
{
|
|
|
|
if (child.IsDirectory)
|
|
|
|
{
|
|
|
|
leftOver.Add(child);
|
|
|
|
}
|
|
|
|
else if (IsIgnored(child.Name))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
files.Add(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
|
|
|
|
|
|
|
|
var resolver = new AudioBookListResolver(namingOptions);
|
|
|
|
var resolverResult = resolver.Resolve(files).ToList();
|
|
|
|
|
|
|
|
var result = new MultiItemResolverResult
|
|
|
|
{
|
|
|
|
ExtraFiles = leftOver,
|
|
|
|
Items = items
|
|
|
|
};
|
|
|
|
|
|
|
|
var isInMixedFolder = resolverResult.Count > 1 || (parent != null && parent.IsTopParent);
|
|
|
|
|
|
|
|
foreach (var resolvedItem in resolverResult)
|
|
|
|
{
|
2017-11-19 17:20:12 -07:00
|
|
|
if (resolvedItem.Files.Count > 1)
|
|
|
|
{
|
|
|
|
// For now, until we sort out naming for multi-part books
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
var firstMedia = resolvedItem.Files.First();
|
2019-01-07 16:27:46 -07:00
|
|
|
|
2017-11-16 14:25:18 -07:00
|
|
|
var libraryItem = new T
|
|
|
|
{
|
|
|
|
Path = firstMedia.Path,
|
|
|
|
IsInMixedFolder = isInMixedFolder,
|
2017-11-19 17:20:12 -07:00
|
|
|
ProductionYear = resolvedItem.Year,
|
2017-11-16 14:25:18 -07:00
|
|
|
Name = parseName ?
|
|
|
|
resolvedItem.Name :
|
|
|
|
Path.GetFileNameWithoutExtension(firstMedia.Path),
|
|
|
|
//AdditionalParts = resolvedItem.Files.Skip(1).Select(i => i.Path).ToArray(),
|
|
|
|
//LocalAlternateVersions = resolvedItem.AlternateVersions.Select(i => i.Path).ToArray()
|
|
|
|
};
|
|
|
|
|
|
|
|
result.Items.Add(libraryItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool ContainsFile(List<AudioBookInfo> result, FileSystemMetadata file)
|
|
|
|
{
|
|
|
|
return result.Any(i => ContainsFile(i, file));
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool ContainsFile(AudioBookInfo result, FileSystemMetadata file)
|
|
|
|
{
|
|
|
|
return result.Files.Any(i => ContainsFile(i, file)) ||
|
|
|
|
result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
|
|
|
|
result.Extras.Any(i => ContainsFile(i, file));
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static bool ContainsFile(AudioBookFileInfo result, FileSystemMetadata file)
|
2017-11-16 14:25:18 -07:00
|
|
|
{
|
|
|
|
return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static bool IsIgnored(string filename)
|
2017-11-16 14:25:18 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-02 23:58:04 -07:00
|
|
|
}
|
|
|
|
}
|