Skip to content

Commit

Permalink
misc: move resolvers above ignore rules
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Apr 28, 2024
1 parent 6b8254c commit 62bcd8d
Showing 1 changed file with 139 additions and 139 deletions.
278 changes: 139 additions & 139 deletions Shokofin/Resolvers/ShokoResolveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,145 @@ private static bool TryGetIdsForPath(string path, [NotNullWhen(true)] out string

#endregion

#region Resolvers

public async Task<BaseItem?> ResolveSingle(Folder? parent, string? collectionType, FileSystemMetadata fileInfo)
{
if (!(collectionType == CollectionType.TvShows || collectionType == CollectionType.Movies || collectionType == null) || parent == null || fileInfo == null)
return null;

var root = LibraryManager.RootFolder;
if (root == null || parent == root)
return null;

try {
if (!Lookup.IsEnabledForItem(parent))
return null;

// Skip anything outside the VFS.
if (!fileInfo.FullName.StartsWith(Plugin.Instance.VirtualRoot))
return null;

if (parent.GetTopParent() is not Folder mediaFolder)
return null;

var vfsPath = await GenerateStructureInVFS(mediaFolder, fileInfo.FullName).ConfigureAwait(false);
if (string.IsNullOrEmpty(vfsPath))
return null;

if (parent.Id == mediaFolder.Id && fileInfo.IsDirectory) {
if (!fileInfo.Name.TryGetAttributeValue(ShokoSeriesId.Name, out var seriesId) || !int.TryParse(seriesId, out _))
return null;

return new TvSeries() {
Path = fileInfo.FullName,
};
}

return null;
}
catch (Exception ex) {
Logger.LogError(ex, "Threw unexpectedly; {Message}", ex.Message);
throw;
}
}

public async Task<MultiItemResolverResult?> ResolveMultiple(Folder? parent, string? collectionType, List<FileSystemMetadata> fileInfoList)
{
if (!(collectionType == CollectionType.TvShows || collectionType == CollectionType.Movies || collectionType == null) || parent == null)
return null;

var root = LibraryManager.RootFolder;
if (root == null || parent == root)
return null;

try {
if (!Lookup.IsEnabledForItem(parent))
return null;

if (parent.GetTopParent() is not Folder mediaFolder)
return null;

var vfsPath = await GenerateStructureInVFS(mediaFolder, parent.Path).ConfigureAwait(false);
if (string.IsNullOrEmpty(vfsPath))
return null;

// Redirect children of a VFS managed media folder to the VFS.
if (parent.IsTopParent) {
var createMovies = collectionType == CollectionType.Movies || (collectionType == null && Plugin.Instance.Configuration.SeparateMovies);
var items = FileSystem.GetDirectories(vfsPath)
.AsParallel()
.SelectMany(dirInfo => {
if (!dirInfo.Name.TryGetAttributeValue(ShokoSeriesId.Name, out var seriesId) || !int.TryParse(seriesId, out _))
return Array.Empty<BaseItem>();
var season = ApiManager.GetSeasonInfoForSeries(seriesId)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
if (season == null)
return Array.Empty<BaseItem>();
if (createMovies && season.Type == SeriesType.Movie) {
return FileSystem.GetFiles(dirInfo.FullName)
.AsParallel()
.Select(fileInfo => {
// Only allow the video files, since the subtitle files also have the ids set.
if (!NamingOptions.VideoFileExtensions.Contains(Path.GetExtension(fileInfo.Name)))
return null;
if (!TryGetIdsForPath(fileInfo.FullName, out seriesId, out var fileId))
return null;
// This will hopefully just re-use the pre-cached entries from the cache, but it may
// also get it from remote if the cache was emptied for whatever reason.
var file = ApiManager.GetFileInfo(fileId, seriesId)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
// Abort if the file was not recognised.
if (file == null || file.ExtraType != null)
return null;
return new Movie() {
Path = fileInfo.FullName,
} as BaseItem;
})
.ToArray();
}
return new BaseItem[1] {
new TvSeries() {
Path = dirInfo.FullName,
},
};
})
.OfType<BaseItem>()
.ToList();

// TODO: uncomment the code snippet once the PR is in stable JF.
// return new() { Items = items, ExtraFiles = new() };

// TODO: Remove these two hacks once we have proper support for adding multiple series at once.
if (!items.Any(i => i is Movie) && items.Count > 0) {
fileInfoList.Clear();
fileInfoList.AddRange(items.OrderBy(s => int.Parse(s.Path.GetAttributeValue(ShokoSeriesId.Name)!)).Select(s => FileSystem.GetFileSystemInfo(s.Path)));
}

return new() { Items = items.Where(i => i is Movie).ToList(), ExtraFiles = items.OfType<TvSeries>().Select(s => FileSystem.GetFileSystemInfo(s.Path)).ToList() };
}

return null;
}
catch (Exception ex) {
Logger.LogError(ex, "Threw unexpectedly; {Message}", ex.Message);
throw;
}
}

#endregion

#region Ignore Rule

public async Task<bool> ShouldFilterItem(Folder? parent, FileSystemMetadata fileInfo)
Expand Down Expand Up @@ -1319,143 +1458,4 @@ private async Task<bool> ShouldFilterFile(string partialPath, string fullPath, b
}

#endregion

#region Resolvers

public async Task<BaseItem?> ResolveSingle(Folder? parent, string? collectionType, FileSystemMetadata fileInfo)
{
if (!(collectionType == CollectionType.TvShows || collectionType == CollectionType.Movies || collectionType == null) || parent == null || fileInfo == null)
return null;

var root = LibraryManager.RootFolder;
if (root == null || parent == root)
return null;

try {
if (!Lookup.IsEnabledForItem(parent))
return null;

// Skip anything outside the VFS.
if (!fileInfo.FullName.StartsWith(Plugin.Instance.VirtualRoot))
return null;

if (parent.GetTopParent() is not Folder mediaFolder)
return null;

var vfsPath = await GenerateStructureInVFS(mediaFolder, fileInfo.FullName).ConfigureAwait(false);
if (string.IsNullOrEmpty(vfsPath))
return null;

if (parent.Id == mediaFolder.Id && fileInfo.IsDirectory) {
if (!fileInfo.Name.TryGetAttributeValue(ShokoSeriesId.Name, out var seriesId) || !int.TryParse(seriesId, out _))
return null;

return new TvSeries() {
Path = fileInfo.FullName,
};
}

return null;
}
catch (Exception ex) {
Logger.LogError(ex, "Threw unexpectedly; {Message}", ex.Message);
throw;
}
}

public async Task<MultiItemResolverResult?> ResolveMultiple(Folder? parent, string? collectionType, List<FileSystemMetadata> fileInfoList)
{
if (!(collectionType == CollectionType.TvShows || collectionType == CollectionType.Movies || collectionType == null) || parent == null)
return null;

var root = LibraryManager.RootFolder;
if (root == null || parent == root)
return null;

try {
if (!Lookup.IsEnabledForItem(parent))
return null;

if (parent.GetTopParent() is not Folder mediaFolder)
return null;

var vfsPath = await GenerateStructureInVFS(mediaFolder, parent.Path).ConfigureAwait(false);
if (string.IsNullOrEmpty(vfsPath))
return null;

// Redirect children of a VFS managed media folder to the VFS.
if (parent.IsTopParent) {
var createMovies = collectionType == CollectionType.Movies || (collectionType == null && Plugin.Instance.Configuration.SeparateMovies);
var items = FileSystem.GetDirectories(vfsPath)
.AsParallel()
.SelectMany(dirInfo => {
if (!dirInfo.Name.TryGetAttributeValue(ShokoSeriesId.Name, out var seriesId) || !int.TryParse(seriesId, out _))
return Array.Empty<BaseItem>();
var season = ApiManager.GetSeasonInfoForSeries(seriesId)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
if (season == null)
return Array.Empty<BaseItem>();
if (createMovies && season.Type == SeriesType.Movie) {
return FileSystem.GetFiles(dirInfo.FullName)
.AsParallel()
.Select(fileInfo => {
// Only allow the video files, since the subtitle files also have the ids set.
if (!NamingOptions.VideoFileExtensions.Contains(Path.GetExtension(fileInfo.Name)))
return null;
if (!TryGetIdsForPath(fileInfo.FullName, out seriesId, out var fileId))
return null;
// This will hopefully just re-use the pre-cached entries from the cache, but it may
// also get it from remote if the cache was emptied for whatever reason.
var file = ApiManager.GetFileInfo(fileId, seriesId)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
// Abort if the file was not recognised.
if (file == null || file.ExtraType != null)
return null;
return new Movie() {
Path = fileInfo.FullName,
} as BaseItem;
})
.ToArray();
}
return new BaseItem[1] {
new TvSeries() {
Path = dirInfo.FullName,
},
};
})
.OfType<BaseItem>()
.ToList();

// TODO: uncomment the code snippet once the PR is in stable JF.
// return new() { Items = items, ExtraFiles = new() };

// TODO: Remove these two hacks once we have proper support for adding multiple series at once.
if (!items.Any(i => i is Movie) && items.Count > 0) {
fileInfoList.Clear();
fileInfoList.AddRange(items.OrderBy(s => int.Parse(s.Path.GetAttributeValue(ShokoSeriesId.Name)!)).Select(s => FileSystem.GetFileSystemInfo(s.Path)));
}

return new() { Items = items.Where(i => i is Movie).ToList(), ExtraFiles = items.OfType<TvSeries>().Select(s => FileSystem.GetFileSystemInfo(s.Path)).ToList() };
}

return null;
}
catch (Exception ex) {
Logger.LogError(ex, "Threw unexpectedly; {Message}", ex.Message);
throw;
}
}

#endregion
}

0 comments on commit 62bcd8d

Please sign in to comment.