You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs

241 lines
8.2 KiB

#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.IO;
7 years ago
using System.Linq;
using Emby.Naming.Audio;
using Emby.Naming.AudioBook;
using Emby.Naming.Common;
using Emby.Naming.Video;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
7 years ago
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
7 years ago
using MediaBrowser.Model.IO;
namespace Emby.Server.Implementations.Library.Resolvers.Audio
{
/// <summary>
/// Class AudioResolver.
/// </summary>
7 years ago
public class AudioResolver : ItemResolver<MediaBrowser.Controller.Entities.Audio.Audio>, IMultiItemResolver
{
private readonly NamingOptions _namingOptions;
public AudioResolver(NamingOptions namingOptions)
{
_namingOptions = namingOptions;
}
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override ResolverPriority Priority => ResolverPriority.Fifth;
public MultiItemResolverResult ResolveMultiple(
Folder parent,
7 years ago
List<FileSystemMetadata> files,
string collectionType,
IDirectoryService directoryService)
{
var result = ResolveMultipleInternal(parent, files, collectionType);
7 years ago
if (result is not null)
7 years ago
{
foreach (var item in result.Items)
{
SetInitialItemValues((MediaBrowser.Controller.Entities.Audio.Audio)item, null);
}
}
return result;
}
private MultiItemResolverResult ResolveMultipleInternal(
Folder parent,
7 years ago
List<FileSystemMetadata> files,
string collectionType)
7 years ago
{
if (string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
{
return ResolveMultipleAudio(parent, files, true);
7 years ago
}
return null;
}
/// <summary>
/// Resolves the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>Entities.Audio.Audio.</returns>
protected override MediaBrowser.Controller.Entities.Audio.Audio Resolve(ItemResolveArgs args)
{
// Return audio if the path is a file and has a matching extension
7 years ago
var collectionType = args.GetCollectionType();
var isBooksCollectionType = string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase);
if (args.IsDirectory)
{
7 years ago
if (!isBooksCollectionType)
{
return null;
}
return FindAudioBook(args, false);
7 years ago
}
if (AudioFileParser.IsAudioFile(args.Path, _namingOptions))
7 years ago
{
var extension = Path.GetExtension(args.Path.AsSpan());
if (extension.Equals(".cue", StringComparison.OrdinalIgnoreCase))
{
7 years ago
// if audio file exists of same name, return null
return null;
}
var isMixedCollectionType = string.IsNullOrEmpty(collectionType);
7 years ago
// For conflicting extensions, give priority to videos
if (isMixedCollectionType && VideoResolver.IsVideoFile(args.Path, _namingOptions))
7 years ago
{
return null;
}
7 years ago
MediaBrowser.Controller.Entities.Audio.Audio item = null;
7 years ago
var isMusicCollectionType = string.Equals(collectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase);
7 years ago
// Use regular audio type for mixed libraries, owned items and music
if (isMixedCollectionType ||
args.Parent is null ||
7 years ago
isMusicCollectionType)
{
item = new MediaBrowser.Controller.Entities.Audio.Audio();
}
else if (isBooksCollectionType)
{
item = new AudioBook();
}
if (item is not null)
7 years ago
{
item.IsShortcut = extension.Equals(".strm", StringComparison.OrdinalIgnoreCase);
7 years ago
item.IsInMixedFolder = true;
}
7 years ago
return item;
}
return null;
}
7 years ago
private AudioBook FindAudioBook(ItemResolveArgs args, bool parseName)
7 years ago
{
// TODO: Allow GetMultiDiscMovie in here
var result = ResolveMultipleAudio(args.Parent, args.GetActualFileSystemChildren(), parseName);
7 years ago
if (result is null || result.Items.Count != 1 || result.Items[0] is not AudioBook item)
7 years ago
{
return null;
7 years ago
}
// If we were supporting this we'd be checking filesFromOtherItems
item.IsInMixedFolder = false;
item.Name = Path.GetFileName(item.ContainingFolderPath);
return item;
7 years ago
}
private MultiItemResolverResult ResolveMultipleAudio(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, bool parseName)
7 years ago
{
var files = new List<FileSystemMetadata>();
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
7 years ago
{
files.Add(child);
}
}
var resolver = new AudioBookListResolver(_namingOptions);
7 years ago
var resolverResult = resolver.Resolve(files).ToList();
var result = new MultiItemResolverResult
{
ExtraFiles = leftOver,
Items = new List<BaseItem>()
7 years ago
};
var isInMixedFolder = resolverResult.Count > 1 || (parent is not null && parent.IsTopParent);
7 years ago
foreach (var resolvedItem in resolverResult)
{
7 years ago
if (resolvedItem.Files.Count > 1)
{
// For now, until we sort out naming for multi-part books
continue;
}
// Until multi-part books are handled letting files stack hides them from browsing in the client
if (resolvedItem.Files.Count == 0 || resolvedItem.Extras.Count > 0 || resolvedItem.AlternateVersions.Count > 0)
{
continue;
}
var firstMedia = resolvedItem.Files[0];
var libraryItem = new AudioBook
7 years ago
{
Path = firstMedia.Path,
IsInMixedFolder = isInMixedFolder,
7 years ago
ProductionYear = resolvedItem.Year,
7 years ago
Name = parseName ?
resolvedItem.Name :
Path.GetFileNameWithoutExtension(firstMedia.Path),
4 years ago
// AdditionalParts = resolvedItem.Files.Skip(1).Select(i => i.Path).ToArray(),
// LocalAlternateVersions = resolvedItem.AlternateVersions.Select(i => i.Path).ToArray()
7 years ago
};
result.Items.Add(libraryItem);
}
result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
return result;
}
private static bool ContainsFile(IEnumerable<AudioBookInfo> result, FileSystemMetadata file)
7 years ago
{
return result.Any(i => ContainsFile(i, file));
}
private static bool ContainsFile(AudioBookInfo result, FileSystemMetadata file)
7 years ago
{
return result.Files.Any(i => ContainsFile(i, file)) ||
result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
result.Extras.Any(i => ContainsFile(i, file));
}
private static bool ContainsFile(AudioBookFileInfo result, FileSystemMetadata file)
7 years ago
{
return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
}
}
}