From 8ab9444f3bb2beea658be34593db1787b78449be Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Sat, 30 Mar 2024 01:27:27 +0100 Subject: [PATCH] cleanup: prittify get attribute value - Prittified how we get the attribute value. --- Shokofin/API/ShokoAPIManager.cs | 10 +++------- Shokofin/Resolvers/ShokoResolveManager.cs | 12 +++++------- Shokofin/StringExtensions.cs | 5 ++++- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Shokofin/API/ShokoAPIManager.cs b/Shokofin/API/ShokoAPIManager.cs index e07d047b..857ba855 100644 --- a/Shokofin/API/ShokoAPIManager.cs +++ b/Shokofin/API/ShokoAPIManager.cs @@ -346,13 +346,11 @@ internal void AddFileLookupIds(string path, string fileId, string seriesId, IEnu // Fast-path for VFS. if (path.StartsWith(Plugin.Instance.VirtualRoot + Path.DirectorySeparatorChar)) { var fileName = Path.GetFileNameWithoutExtension(path); - if (!int.TryParse(fileName.GetAttributeValue("shoko-series"), out var seriesIdRaw)) + if (!fileName.TryGetAttributeValue("shoko-series", out var sI) || !int.TryParse(sI, out _)) return (null, null, null); - if (!int.TryParse(fileName.GetAttributeValue("shoko-file"), out var fileIdRaw)) + if (!fileName.TryGetAttributeValue("shoko-file", out var fI) || !int.TryParse(fI, out _)) return (null, null, null); - var sI = seriesIdRaw.ToString(); - var fI = fileIdRaw.ToString(); var fileInfo = await GetFileInfo(fI, sI).ConfigureAwait(false); if (fileInfo == null) return (null, null, null); @@ -686,11 +684,9 @@ public bool TryGetDefaultSeriesIdForSeriesId(string seriesId, out string? defaul // Fast-path for VFS. if (path.StartsWith(Plugin.Instance.VirtualRoot + Path.DirectorySeparatorChar)) { - var seriesSegment = Path.GetFileName(path).GetAttributeValue("shoko-series"); - if (!int.TryParse(seriesSegment, out var seriesIdRaw)) + if (!Path.GetFileName(path).TryGetAttributeValue("shoko-series", out seriesId) || !int.TryParse(seriesId, out _)) return null; - seriesId = seriesIdRaw.ToString(); PathToSeriesIdDictionary[path] = seriesId; SeriesIdToPathDictionary.TryAdd(seriesId, path); diff --git a/Shokofin/Resolvers/ShokoResolveManager.cs b/Shokofin/Resolvers/ShokoResolveManager.cs index 81f9156d..b1aa0975 100644 --- a/Shokofin/Resolvers/ShokoResolveManager.cs +++ b/Shokofin/Resolvers/ShokoResolveManager.cs @@ -595,8 +595,7 @@ private async Task ScanFile(string partialPath, string fullPath, bool shou return null; if (parent.Id == mediaFolder.Id && fileInfo.IsDirectory) { - var seriesSegment = fileInfo.Name.GetAttributeValue("shoko-series"); - if (!int.TryParse(seriesSegment, out var seriesId)) + if (!fileInfo.Name.TryGetAttributeValue("shoko-series", out var seriesId) || !int.TryParse(seriesId, out _)) return null; return new TvSeries() @@ -644,11 +643,10 @@ private async Task ScanFile(string partialPath, string fullPath, bool shou var items = FileSystem.GetDirectories(vfsPath) .AsParallel() .SelectMany(dirInfo => { - var seriesSegment = dirInfo.Name.GetAttributeValue("shoko-series"); - if (!int.TryParse(seriesSegment, out var seriesId)) + if (!dirInfo.Name.TryGetAttributeValue("shoko-series", out var seriesId) || !int.TryParse(seriesId, out _)) return Array.Empty(); - var season = ApiManager.GetSeasonInfoForSeries(seriesId.ToString()) + var season = ApiManager.GetSeasonInfoForSeries(seriesId) .ConfigureAwait(false) .GetAwaiter() .GetResult(); @@ -659,12 +657,12 @@ private async Task ScanFile(string partialPath, string fullPath, bool shou return FileSystem.GetFiles(dirInfo.FullName) .AsParallel() .Select(fileInfo => { - if (!int.TryParse(fileInfo.Name.GetAttributeValue("shoko-file"), out var fileId)) + if (!fileInfo.Name.TryGetAttributeValue("shoko-file", out var fileId) || !int.TryParse(fileId, out _)) 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.ToString(), seriesId.ToString()) + var file = ApiManager.GetFileInfo(fileId, seriesId) .ConfigureAwait(false) .GetAwaiter() .GetResult(); diff --git a/Shokofin/StringExtensions.cs b/Shokofin/StringExtensions.cs index f9c5cf1f..7842f946 100644 --- a/Shokofin/StringExtensions.cs +++ b/Shokofin/StringExtensions.cs @@ -67,7 +67,7 @@ public static string ReplaceInvalidPathCharacters(this string path) .Replace(@"?", "\uff1f") // ? (FULL WIDTH QUESTION MARK) .Replace(@".", "\u2024") // ․ (ONE DOT LEADER) .Trim(); - + /// /// Gets the attribute value for in . /// @@ -117,4 +117,7 @@ public static string ReplaceInvalidPathCharacters(this string path) return null; } + + public static bool TryGetAttributeValue(this string text, string attribute, out string? value) + => !string.IsNullOrEmpty(value = GetAttributeValue(text, attribute)); } \ No newline at end of file