Took care of SonarCloud code issues (#1041)

This commit is contained in:
Joseph Milazzo 2022-02-07 05:30:28 -08:00 committed by GitHub
parent 5b32a958dc
commit 0e2fafe396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 27 additions and 69 deletions

View File

@ -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()
{

View File

@ -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)

View File

@ -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];
}

View File

@ -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;";
}

View File

@ -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()

View File

@ -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>

View File

@ -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();
}

View File

@ -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;
// }
}
}

View File

@ -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;
// }
}
}

View File

@ -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;
}

View File

@ -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;
}