mirror of
				https://github.com/Kareadita/Kavita.git
				synced 2025-11-03 19:17:05 -05:00 
			
		
		
		
	* Refactored all files to have Interfaces within the same file. Started moving over to file-scoped namespaces. * Refactored common methods for getting underlying file's cover, pages, and extracting into 1 interface. * More refactoring around removing dependence on explicit filetype testing for getting information. * Code is buildable, tests are broken. Huge refactor (not completed) which makes most of DirectoryService testable with a mock filesystem (and thus the services that utilize it). * Finished porting DirectoryService to use mocked filesystem implementation. * Added a null check * Added a null check * Finished all unit tests for DirectoryService. * Some misc cleanup on the code * Fixed up some bugs from refactoring scan loop. * Implemented CleanupService testing and refactored more of DirectoryService to be non-static. Fixed a bug where cover file cleanup wasn't properly finding files due to a regex bug. * Fixed an issue in CleanupBackup() where we weren't properly selecting database files older than 30 days. Finished CleanupService Tests. * Refactored Flatten and RemoveNonImages to directory service to allow CacheService to be testable. * Finally have CacheService tested. Rewrote GetCachedPagePath() to be much more straightforward & performant. * Updated DefaultParserTests.cs to contain all existing tests and follow new test layout format. * All tests fixed up
		
			
				
	
	
		
			169 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			169 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Linq;
 | 
						|
using System.Net.Http;
 | 
						|
using System.Threading.Tasks;
 | 
						|
using API.DTOs.Update;
 | 
						|
using API.SignalR;
 | 
						|
using API.SignalR.Presence;
 | 
						|
using Flurl.Http;
 | 
						|
using Flurl.Http.Configuration;
 | 
						|
using Kavita.Common.EnvironmentInfo;
 | 
						|
using MarkdownDeep;
 | 
						|
using Microsoft.AspNetCore.SignalR;
 | 
						|
using Microsoft.Extensions.Hosting;
 | 
						|
using Microsoft.Extensions.Logging;
 | 
						|
 | 
						|
namespace API.Services.Tasks;
 | 
						|
 | 
						|
internal class GithubReleaseMetadata
 | 
						|
{
 | 
						|
    /// <summary>
 | 
						|
    /// Name of the Tag
 | 
						|
    /// <example>v0.4.3</example>
 | 
						|
    /// </summary>
 | 
						|
    // ReSharper disable once InconsistentNaming
 | 
						|
    public string Tag_Name { get; init; }
 | 
						|
    /// <summary>
 | 
						|
    /// Name of the Release
 | 
						|
    /// </summary>
 | 
						|
    public string Name { get; init; }
 | 
						|
    /// <summary>
 | 
						|
    /// Body of the Release
 | 
						|
    /// </summary>
 | 
						|
    public string Body { get; init; }
 | 
						|
    /// <summary>
 | 
						|
    /// Url of the release on Github
 | 
						|
    /// </summary>
 | 
						|
    // ReSharper disable once InconsistentNaming
 | 
						|
    public string Html_Url { get; init; }
 | 
						|
    /// <summary>
 | 
						|
    /// Date Release was Published
 | 
						|
    /// </summary>
 | 
						|
    // ReSharper disable once InconsistentNaming
 | 
						|
    public string Published_At { get; init; }
 | 
						|
}
 | 
						|
 | 
						|
public class UntrustedCertClientFactory : DefaultHttpClientFactory
 | 
						|
{
 | 
						|
    public override HttpMessageHandler CreateMessageHandler() {
 | 
						|
        return new HttpClientHandler {
 | 
						|
            ServerCertificateCustomValidationCallback = (_, _, _, _) => true
 | 
						|
        };
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
public interface IVersionUpdaterService
 | 
						|
{
 | 
						|
    Task<UpdateNotificationDto> CheckForUpdate();
 | 
						|
    Task PushUpdate(UpdateNotificationDto update);
 | 
						|
    Task<IEnumerable<UpdateNotificationDto>> GetAllReleases();
 | 
						|
}
 | 
						|
 | 
						|
public class VersionUpdaterService : IVersionUpdaterService
 | 
						|
{
 | 
						|
    private readonly ILogger<VersionUpdaterService> _logger;
 | 
						|
    private readonly IHubContext<MessageHub> _messageHub;
 | 
						|
    private readonly IPresenceTracker _tracker;
 | 
						|
    private readonly Markdown _markdown = new MarkdownDeep.Markdown();
 | 
						|
#pragma warning disable S1075
 | 
						|
    private static readonly string GithubLatestReleasesUrl = "https://api.github.com/repos/Kareadita/Kavita/releases/latest";
 | 
						|
    private static readonly string GithubAllReleasesUrl = "https://api.github.com/repos/Kareadita/Kavita/releases";
 | 
						|
#pragma warning restore S1075
 | 
						|
 | 
						|
    public VersionUpdaterService(ILogger<VersionUpdaterService> logger, IHubContext<MessageHub> messageHub, IPresenceTracker tracker)
 | 
						|
    {
 | 
						|
        _logger = logger;
 | 
						|
        _messageHub = messageHub;
 | 
						|
        _tracker = tracker;
 | 
						|
 | 
						|
        FlurlHttp.ConfigureClient(GithubLatestReleasesUrl, cli =>
 | 
						|
            cli.Settings.HttpClientFactory = new UntrustedCertClientFactory());
 | 
						|
        FlurlHttp.ConfigureClient(GithubAllReleasesUrl, cli =>
 | 
						|
            cli.Settings.HttpClientFactory = new UntrustedCertClientFactory());
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Fetches the latest release from Github
 | 
						|
    /// </summary>
 | 
						|
    public async Task<UpdateNotificationDto> CheckForUpdate()
 | 
						|
    {
 | 
						|
        var update = await GetGithubRelease();
 | 
						|
        return CreateDto(update);
 | 
						|
    }
 | 
						|
 | 
						|
    public async Task<IEnumerable<UpdateNotificationDto>> GetAllReleases()
 | 
						|
    {
 | 
						|
        var updates = await GetGithubReleases();
 | 
						|
        return updates.Select(CreateDto);
 | 
						|
    }
 | 
						|
 | 
						|
    private UpdateNotificationDto CreateDto(GithubReleaseMetadata update)
 | 
						|
    {
 | 
						|
        if (update == null || string.IsNullOrEmpty(update.Tag_Name)) return null;
 | 
						|
        var updateVersion = new Version(update.Tag_Name.Replace("v", string.Empty));
 | 
						|
        var currentVersion = BuildInfo.Version.ToString();
 | 
						|
 | 
						|
        if (updateVersion.Revision == -1)
 | 
						|
        {
 | 
						|
            currentVersion = currentVersion.Substring(0, currentVersion.LastIndexOf(".", StringComparison.Ordinal));
 | 
						|
        }
 | 
						|
 | 
						|
        return new UpdateNotificationDto()
 | 
						|
        {
 | 
						|
            CurrentVersion = currentVersion,
 | 
						|
            UpdateVersion = updateVersion.ToString(),
 | 
						|
            UpdateBody = _markdown.Transform(update.Body.Trim()),
 | 
						|
            UpdateTitle = update.Name,
 | 
						|
            UpdateUrl = update.Html_Url,
 | 
						|
            IsDocker = new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker,
 | 
						|
            PublishDate = update.Published_At
 | 
						|
        };
 | 
						|
    }
 | 
						|
 | 
						|
    public async Task PushUpdate(UpdateNotificationDto update)
 | 
						|
    {
 | 
						|
        if (update == null) return;
 | 
						|
 | 
						|
        var admins = await _tracker.GetOnlineAdmins();
 | 
						|
        var updateVersion = new Version(update.CurrentVersion);
 | 
						|
 | 
						|
        if (BuildInfo.Version < updateVersion)
 | 
						|
        {
 | 
						|
            _logger.LogInformation("Server is out of date. Current: {CurrentVersion}. Available: {AvailableUpdate}", BuildInfo.Version, updateVersion);
 | 
						|
            await SendEvent(update, admins);
 | 
						|
        }
 | 
						|
        else if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development)
 | 
						|
        {
 | 
						|
            _logger.LogInformation("Server is up to date. Current: {CurrentVersion}", BuildInfo.Version);
 | 
						|
            await SendEvent(update, admins);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    private async Task SendEvent(UpdateNotificationDto update, IReadOnlyList<string> admins)
 | 
						|
    {
 | 
						|
        await _messageHub.Clients.Users(admins).SendAsync(SignalREvents.UpdateAvailable, MessageFactory.UpdateVersionEvent(update));
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    private static async Task<GithubReleaseMetadata> GetGithubRelease()
 | 
						|
    {
 | 
						|
        var update = await GithubLatestReleasesUrl
 | 
						|
            .WithHeader("Accept", "application/json")
 | 
						|
            .WithHeader("User-Agent", "Kavita")
 | 
						|
            .GetJsonAsync<GithubReleaseMetadata>();
 | 
						|
 | 
						|
        return update;
 | 
						|
    }
 | 
						|
 | 
						|
    private static async Task<IEnumerable<GithubReleaseMetadata>> GetGithubReleases()
 | 
						|
    {
 | 
						|
        var update = await GithubAllReleasesUrl
 | 
						|
            .WithHeader("Accept", "application/json")
 | 
						|
            .WithHeader("User-Agent", "Kavita")
 | 
						|
            .GetJsonAsync<IEnumerable<GithubReleaseMetadata>>();
 | 
						|
 | 
						|
        return update;
 | 
						|
    }
 | 
						|
}
 |