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