Lots of cleanup

This commit is contained in:
Joseph Milazzo 2021-02-07 12:02:47 -06:00
parent bd5a1338c4
commit 077e5f798a
28 changed files with 80 additions and 55 deletions

View File

@ -1,18 +1,10 @@
using API.Helpers.Converters; using API.Helpers.Converters;
using Xunit; using Xunit;
using Xunit.Abstractions;
namespace API.Tests.Converters namespace API.Tests.Converters
{ {
public class CronConverterTests public class CronConverterTests
{ {
private readonly ITestOutputHelper _testOutputHelper;
public CronConverterTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Theory] [Theory]
[InlineData("daily", "0 0 * * *")] [InlineData("daily", "0 0 * * *")]
[InlineData("disabled", "0 0 31 2 *")] [InlineData("disabled", "0 0 31 2 *")]

View File

@ -1,5 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using API.Entities; using API.Entities.Enums;
using API.Parser; using API.Parser;
using Xunit; using Xunit;
using static API.Parser.Parser; using static API.Parser.Parser;

View File

@ -1,8 +1,4 @@
using API.Interfaces; using Xunit;
using API.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace API.Tests.Services namespace API.Tests.Services
{ {

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Constants; using API.Constants;
using API.Data.Migrations;
using API.DTOs; using API.DTOs;
using API.Entities; using API.Entities;
using API.Extensions; using API.Extensions;

View File

@ -1,6 +1,5 @@
using System.IO; using System.IO;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace API.Controllers namespace API.Controllers
{ {

View File

@ -18,7 +18,7 @@ namespace API.Controllers
_logger = logger; _logger = logger;
} }
[HttpPost] [HttpPost("restart")]
public ActionResult RestartServer() public ActionResult RestartServer()
{ {
_logger.LogInformation($"{User.GetUsername()} is restarting server from admin dashboard."); _logger.LogInformation($"{User.GetUsername()} is restarting server from admin dashboard.");

View File

@ -1,8 +1,9 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.DTOs; using API.DTOs;
using API.Entities; using API.Entities.Enums;
using API.Extensions; using API.Extensions;
using API.Helpers.Converters; using API.Helpers.Converters;
using API.Interfaces; using API.Interfaces;
@ -62,6 +63,13 @@ namespace API.Controllers
setting.Value = updateSettingsDto.TaskScan; setting.Value = updateSettingsDto.TaskScan;
_unitOfWork.SettingsRepository.Update(setting); _unitOfWork.SettingsRepository.Update(setting);
} }
if (setting.Key == ServerSettingKey.Port && updateSettingsDto.Port + "" != setting.Value)
{
setting.Value = updateSettingsDto.Port + "";
Environment.SetEnvironmentVariable("KAVITA_PORT", setting.Value);
_unitOfWork.SettingsRepository.Update(setting);
}
} }
if (_unitOfWork.HasChanges() && await _unitOfWork.Complete()) if (_unitOfWork.HasChanges() && await _unitOfWork.Complete())

View File

@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using API.Entities; using API.Entities.Enums;
namespace API.DTOs namespace API.DTOs
{ {

View File

@ -1,5 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using API.Entities; using API.Entities.Enums;
namespace API.DTOs namespace API.DTOs
{ {

View File

@ -1,4 +1,4 @@
using API.Entities; using API.Entities.Enums;
namespace API.DTOs namespace API.DTOs
{ {

View File

@ -6,5 +6,6 @@
public string TaskScan { get; set; } public string TaskScan { get; set; }
public string LoggingLevel { get; set; } public string LoggingLevel { get; set; }
public string TaskBackup { get; set; } public string TaskBackup { get; set; }
public int Port { get; set; }
} }
} }

View File

@ -1,4 +1,5 @@
 
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace API.DTOs namespace API.DTOs
@ -11,6 +12,9 @@ namespace API.DTOs
public byte[] CoverImage { get; set; } public byte[] CoverImage { get; set; }
public int Pages { get; set; } public int Pages { get; set; }
public int PagesRead { get; set; } public int PagesRead { get; set; }
public DateTime LastModified { get; set; }
public DateTime Created { get; set; }
public bool IsSpecial { get; set; }
public ICollection<ChapterDto> Chapters { get; set; } public ICollection<ChapterDto> Chapters { get; set; }
} }
} }

View File

@ -3,6 +3,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Constants; using API.Constants;
using API.Entities; using API.Entities;
using API.Entities.Enums;
using API.Services; using API.Services;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
@ -30,12 +31,15 @@ namespace API.Data
public static async Task SeedSettings(DataContext context) public static async Task SeedSettings(DataContext context)
{ {
context.Database.EnsureCreated(); await context.Database.EnsureCreatedAsync();
IList<ServerSetting> defaultSettings = new List<ServerSetting>() IList<ServerSetting> defaultSettings = new List<ServerSetting>()
{ {
new() {Key = ServerSettingKey.CacheDirectory, Value = CacheService.CacheDirectory}, new() {Key = ServerSettingKey.CacheDirectory, Value = CacheService.CacheDirectory},
new () {Key = ServerSettingKey.TaskScan, Value = "daily"} new () {Key = ServerSettingKey.TaskScan, Value = "daily"},
//new () {Key = ServerSettingKey.LoggingLevel, Value = "Information"},
//new () {Key = ServerSettingKey.TaskBackup, Value = "daily"},
new () {Key = ServerSettingKey.Port, Value = "5000"},
}; };
foreach (var defaultSetting in defaultSettings) foreach (var defaultSetting in defaultSettings)
@ -43,7 +47,7 @@ namespace API.Data
var existing = context.ServerSetting.FirstOrDefault(s => s.Key == defaultSetting.Key); var existing = context.ServerSetting.FirstOrDefault(s => s.Key == defaultSetting.Key);
if (existing == null) if (existing == null)
{ {
context.ServerSetting.Add(defaultSetting); await context.ServerSetting.AddAsync(defaultSetting);
} }
} }

View File

@ -3,6 +3,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.DTOs; using API.DTOs;
using API.Entities; using API.Entities;
using API.Entities.Enums;
using API.Interfaces; using API.Interfaces;
using AutoMapper; using AutoMapper;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;

View File

@ -1,6 +1,6 @@
using System.ComponentModel; using System.ComponentModel;
namespace API.Entities namespace API.Entities.Enums
{ {
public enum LibraryType public enum LibraryType
{ {

View File

@ -1,6 +1,6 @@
using System.ComponentModel; using System.ComponentModel;
namespace API.Entities namespace API.Entities.Enums
{ {
public enum MangaFormat public enum MangaFormat
{ {

View File

@ -1,10 +1,11 @@
namespace API.Entities namespace API.Entities.Enums
{ {
public enum ServerSettingKey public enum ServerSettingKey
{ {
TaskScan = 0, TaskScan = 0,
CacheDirectory = 1, CacheDirectory = 1,
TaskBackup = 2, TaskBackup = 2,
LoggingLevel = 3 LoggingLevel = 3,
Port = 4
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using API.Entities.Enums;
using API.Entities.Interfaces; using API.Entities.Interfaces;
namespace API.Entities namespace API.Entities

View File

@ -1,4 +1,6 @@
 
using API.Entities.Enums;
namespace API.Entities namespace API.Entities
{ {
public class MangaFile public class MangaFile

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using API.Entities.Enums;
using API.Entities.Interfaces; using API.Entities.Interfaces;
namespace API.Entities namespace API.Entities

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using API.DTOs; using API.DTOs;
using API.Entities; using API.Entities;
using API.Entities.Enums;
using AutoMapper; using AutoMapper;
namespace API.Helpers.Converters namespace API.Helpers.Converters
@ -26,7 +27,9 @@ namespace API.Helpers.Converters
case ServerSettingKey.TaskBackup: case ServerSettingKey.TaskBackup:
destination.TaskBackup = row.Value; destination.TaskBackup = row.Value;
break; break;
case ServerSettingKey.Port:
destination.Port = int.Parse(row.Value);
break;
} }
} }

View File

@ -2,6 +2,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using API.DTOs; using API.DTOs;
using API.Entities; using API.Entities;
using API.Entities.Enums;
namespace API.Interfaces namespace API.Interfaces
{ {

View File

@ -1,9 +1,8 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using API.Entities; using API.Entities.Enums;
namespace API.Parser namespace API.Parser
{ {

View File

@ -1,5 +1,4 @@
 using API.Entities.Enums;
using API.Entities;
namespace API.Parser namespace API.Parser
{ {

View File

@ -4,7 +4,6 @@ using API.Data;
using API.Entities; using API.Entities;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -14,12 +13,11 @@ namespace API
{ {
public class Program public class Program
{ {
private static readonly int HttpPort = 5000;
protected Program() protected Program()
{ {
} }
private static readonly int HttpPort = 5000; // TODO: Get from DB
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {
var host = CreateHostBuilder(args).Build(); var host = CreateHostBuilder(args).Build();
@ -41,7 +39,7 @@ namespace API
var logger = services.GetRequiredService < ILogger<Program>>(); var logger = services.GetRequiredService < ILogger<Program>>();
logger.LogError(ex, "An error occurred during migration"); logger.LogError(ex, "An error occurred during migration");
} }
await host.RunAsync(); await host.RunAsync();
} }
@ -49,26 +47,40 @@ namespace API
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
webBuilder.UseKestrel((builderContext, opts) => webBuilder.UseKestrel((opts) =>
{ {
opts.ListenAnyIP(HttpPort); opts.ListenAnyIP(HttpPort);
}); });
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();
}); });
private static string BuildUrl(string scheme, string bindAddress, int port) // private static void StartNewInstance()
{ // {
return $"{scheme}://{bindAddress}:{port}"; // //_logger.LogInformation("Starting new instance");
} //
// var module = options.RestartPath;
private static void ConfigureKestrelForHttps(KestrelServerOptions options) //
{ // if (string.IsNullOrWhiteSpace(module))
options.ListenAnyIP(HttpPort); // {
// options.ListenAnyIP(HttpsPort, listenOptions => // module = Environment.GetCommandLineArgs()[0];
// { // }
// listenOptions.Protocols = HttpProtocols.Http1AndHttp2; //
// //listenOptions.UseHttps(pfxFilePath, pfxPassword); // // string commandLineArgsString;
// }); // // if (options.RestartArgs != null)
} // // {
// // commandLineArgsString = options.RestartArgs ?? string.Empty;
// // }
// // else
// // {
// // commandLineArgsString = string.Join(
// // ' ',
// // Environment.GetCommandLineArgs().Skip(1).Select(NormalizeCommandLineArgument));
// // }
//
// //_logger.LogInformation("Executable: {0}", module);
// //_logger.LogInformation("Arguments: {0}", commandLineArgsString);
//
// Process.Start(module, Array.Empty<string>);
// }
} }
} }

View File

@ -129,6 +129,7 @@ namespace API.Services
try { try {
// TODO: In future, we need to take LibraryType into consideration for what extensions to allow (RAW should allow images) // TODO: In future, we need to take LibraryType into consideration for what extensions to allow (RAW should allow images)
// or we need to move this filtering to another area (Process) // or we need to move this filtering to another area (Process)
// or we can get all files and put a check in place during Process to abandon files
files = GetFilesWithCertainExtensions(currentDir, Parser.Parser.MangaFileExtensions) files = GetFilesWithCertainExtensions(currentDir, Parser.Parser.MangaFileExtensions)
.ToArray(); .ToArray();
} }

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Entities; using API.Entities;
using API.Entities.Enums;
using API.Interfaces; using API.Interfaces;
using API.Parser; using API.Parser;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;

View File

@ -1,5 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Entities; using API.Entities.Enums;
using API.Helpers.Converters; using API.Helpers.Converters;
using API.Interfaces; using API.Interfaces;
using Hangfire; using Hangfire;