Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Took care of SonarCloud code issues (#1041)

Joseph Milazzo0e2fafe39631
diff --git a/API.Benchmark/ParserBenchmarks.cs b/API.Benchmark/ParserBenchmarks.cs
index ef12331cc..493af5e4d 100644
--- a/API.Benchmark/ParserBenchmarks.cs
+++ b/API.Benchmark/ParserBenchmarks.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
using System.Text.RegularExpressions;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
@@ -29,37 +30,26 @@ namespace API.Benchmark
Console.WriteLine($"Performing benchmark on {_names.Count} series");
}
- private static void NormalizeOriginal(string name)
- {
- Regex.Replace(name.ToLower(), "[^a-zA-Z0-9]", string.Empty);
- }
-
- private static void NormalizeNew(string name)
+ private static string Normalize(string name)
{
// ReSharper disable once UnusedVariable
var ret = NormalizeRegex.Replace(name, string.Empty).ToLower();
+ var normalized = NormalizeRegex.Replace(name, string.Empty).ToLower();
+ return string.IsNullOrEmpty(normalized) ? name : normalized;
}
+
[Benchmark]
public void TestNormalizeName()
{
foreach (var name in _names)
{
- NormalizeOriginal(name);
+ Normalize(name);
}
}
- [Benchmark]
- public void TestNormalizeName_New()
- {
- foreach (var name in _names)
- {
- NormalizeNew(name);
- }
- }
-
[Benchmark]
public void TestIsEpub()
{
diff --git a/API.Tests/Parser/ParserTest.cs b/API.Tests/Parser/ParserTest.cs
index 3ac183937..02cd81aa4 100644
--- a/API.Tests/Parser/ParserTest.cs
+++ b/API.Tests/Parser/ParserTest.cs
@@ -151,6 +151,7 @@ namespace API.Tests.Parser
[InlineData("Darker Than_Black", "darkerthanblack")]
[InlineData("Citrus", "citrus")]
[InlineData("Citrus+", "citrus+")]
+ [InlineData("Again!!!!", "again")]
[InlineData("카비타", "카비타")]
[InlineData("", "")]
public void NormalizeTest(string input, string expected)
diff --git a/API/Controllers/BookController.cs b/API/Controllers/BookController.cs
index 521c763d4..4e55aeef1 100644
--- a/API/Controllers/BookController.cs
+++ b/API/Controllers/BookController.cs
@@ -155,7 +155,7 @@ namespace API.Controllers
if (!string.IsNullOrEmpty(key) && mappings.ContainsKey(key))
{
var part = string.Empty;
- if (anchor.Attributes["href"].Value.Contains("#"))
+ if (anchor.Attributes["href"].Value.Contains('#'))
{
part = anchor.Attributes["href"].Value.Split("#")[1];
}
diff --git a/API/Controllers/OPDSController.cs b/API/Controllers/OPDSController.cs
index 6ddd67271..21e184e33 100644
--- a/API/Controllers/OPDSController.cs
+++ b/API/Controllers/OPDSController.cs
@@ -618,7 +618,7 @@ public class OpdsController : BaseApiController
private static void AddPagination(Feed feed, PagedList<SeriesDto> list, string href)
{
var url = href;
- if (href.Contains("?"))
+ if (href.Contains('?'))
{
url += "&amp;";
}
diff --git a/API/Data/DbFactory.cs b/API/Data/DbFactory.cs
index d5b3434f7..81750fb72 100644
--- a/API/Data/DbFactory.cs
+++ b/API/Data/DbFactory.cs
@@ -30,11 +30,6 @@ namespace API.Data
};
}
- public static SeriesMetadata SeriesMetadata(ComicInfo info)
- {
- return SeriesMetadata(Array.Empty<CollectionTag>());
- }
-
public static Volume Volume(string volumeNumber)
{
return new Volume()
@@ -61,6 +56,11 @@ namespace API.Data
};
}
+ public static SeriesMetadata SeriesMetadata(ComicInfo info)
+ {
+ return SeriesMetadata(Array.Empty<CollectionTag>());
+ }
+
public static SeriesMetadata SeriesMetadata(ICollection<CollectionTag> collectionTags)
{
return new SeriesMetadata()
diff --git a/API/Data/MigrateBookmarks.cs b/API/Data/MigrateBookmarks.cs
index 043b3e0a4..43aa14bee 100644
--- a/API/Data/MigrateBookmarks.cs
+++ b/API/Data/MigrateBookmarks.cs
@@ -9,11 +9,10 @@ using Microsoft.Extensions.Logging;
namespace API.Data;
/// <summary>
-/// Responsible to migrate existing bookmarks to files
+/// Responsible to migrate existing bookmarks to files. Introduced in v0.4.9.27
/// </summary>
public static class MigrateBookmarks
{
- private static readonly Version VersionBookmarksChanged = new Version(0, 4, 9, 27);
/// <summary>
/// This will migrate existing bookmarks to bookmark folder based
/// </summary>
diff --git a/API/Data/Seed.cs b/API/Data/Seed.cs
index 131c0c1ec..3dd8ecc5f 100644
--- a/API/Data/Seed.cs
+++ b/API/Data/Seed.cs
@@ -93,12 +93,9 @@ namespace API.Data
await context.Database.EnsureCreatedAsync();
var users = await context.AppUser.ToListAsync();
- foreach (var user in users)
+ foreach (var user in users.Where(user => string.IsNullOrEmpty(user.ApiKey)))
{
- if (string.IsNullOrEmpty(user.ApiKey))
- {
- user.ApiKey = HashUtil.ApiKey();
- }
+ user.ApiKey = HashUtil.ApiKey();
}
await context.SaveChangesAsync();
}
diff --git a/API/Extensions/ParserInfoListExtensions.cs b/API/Extensions/ParserInfoListExtensions.cs
index 262a8012a..1bca8787b 100644
--- a/API/Extensions/ParserInfoListExtensions.cs
+++ b/API/Extensions/ParserInfoListExtensions.cs
@@ -29,16 +29,5 @@ namespace API.Extensions
return chapter.IsSpecial ? infos.Any(v => v.Filename == chapter.Range)
: infos.Any(v => v.Chapters == chapter.Range);
}
-
- // /// <summary>
- // /// Returns the MangaFormat that is common to all the files. Unknown if files are mixed (should never happen) or no infos
- // /// </summary>
- // /// <param name="infos"></param>
- // /// <returns></returns>
- // public static MangaFormat GetFormat(this IList<ParserInfo> infos)
- // {
- // if (infos.Count == 0) return MangaFormat.Unknown;
- // return infos.DistinctBy(x => x.Format).First().Format;
- // }
}
}
diff --git a/API/Helpers/Converters/CronConverter.cs b/API/Helpers/Converters/CronConverter.cs
index 32df46753..4f8305e01 100644
--- a/API/Helpers/Converters/CronConverter.cs
+++ b/API/Helpers/Converters/CronConverter.cs
@@ -25,17 +25,5 @@ namespace API.Helpers.Converters
return destination;
}
-
- // public static string ConvertFromCronNotation(string cronNotation)
- // {
- // var destination = string.Empty;
- // destination = cronNotation.ToLower() switch
- // {
- // "0 0 31 2 *" => "disabled",
- // _ => destination
- // };
- //
- // return destination;
- // }
}
}
diff --git a/API/Parser/Parser.cs b/API/Parser/Parser.cs
index 01bb8b4e6..45a1e7757 100644
--- a/API/Parser/Parser.cs
+++ b/API/Parser/Parser.cs
@@ -659,20 +659,17 @@ namespace API.Parser
private static string FormatValue(string value, bool hasPart)
{
- if (!value.Contains("-"))
+ if (!value.Contains('-'))
{
return RemoveLeadingZeroes(hasPart ? AddChapterPart(value) : value);
}
var tokens = value.Split("-");
var from = RemoveLeadingZeroes(tokens[0]);
- if (tokens.Length == 2)
- {
- var to = RemoveLeadingZeroes(hasPart ? AddChapterPart(tokens[1]) : tokens[1]);
- return $"{@from}-{to}";
- }
+ if (tokens.Length != 2) return from;
- return @from;
+ var to = RemoveLeadingZeroes(hasPart ? AddChapterPart(tokens[1]) : tokens[1]);
+ return $"{from}-{to}";
}
public static string ParseChapter(string filename)
@@ -696,7 +693,7 @@ namespace API.Parser
private static string AddChapterPart(string value)
{
- if (value.Contains("."))
+ if (value.Contains('.'))
{
return value;
}
@@ -876,13 +873,10 @@ namespace API.Parser
/// <returns>A zero padded number</returns>
public static string PadZeros(string number)
{
- if (number.Contains("-"))
- {
- var tokens = number.Split("-");
- return $"{PerformPadding(tokens[0])}-{PerformPadding(tokens[1])}";
- }
+ if (!number.Contains('-')) return PerformPadding(number);
- return PerformPadding(number);
+ var tokens = number.Split("-");
+ return $"{PerformPadding(tokens[0])}-{PerformPadding(tokens[1])}";
}
private static string PerformPadding(string number)
@@ -964,7 +958,6 @@ namespace API.Parser
public static string Normalize(string name)
{
- // TODO: This can eat upto 100MB on a file scan. Look into optimizing
var normalized = NormalizeRegex.Replace(name, string.Empty).ToLower();
return string.IsNullOrEmpty(normalized) ? name : normalized;
}
diff --git a/API/Services/ImageService.cs b/API/Services/ImageService.cs
index 29a528e71..cc852b5bb 100644
--- a/API/Services/ImageService.cs
+++ b/API/Services/ImageService.cs
@@ -14,6 +14,7 @@ public interface IImageService
/// Creates a Thumbnail version of a base64 image
/// </summary>
/// <param name="encodedImage">base64 encoded image</param>
+ /// <param name="fileName"></param>
/// <returns>File name with extension of the file. This will always write to <see cref="DirectoryService.CoverImageDirectory"/></returns>
string CreateThumbnailFromBase64(string encodedImage, string fileName);
@@ -88,7 +89,7 @@ public class ImageService : IImageService
try
{
_directoryService.FileSystem.File.Delete(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
- } catch (Exception ex) {/* Swallow exception */}
+ } catch (Exception) {/* Swallow exception */}
thumbnail.WriteToFile(_directoryService.FileSystem.Path.Join(outputDirectory, filename));
return filename;
}