Skip to content

Commit

Permalink
Lots of Bugfixes (Kareadita#2960)
Browse files Browse the repository at this point in the history
Co-authored-by: Samuel Martins <[email protected]>
Co-authored-by: Robbie Davis <[email protected]>
  • Loading branch information
3 people authored May 22, 2024
1 parent 97ffdd0 commit b50fa0f
Show file tree
Hide file tree
Showing 45 changed files with 563 additions and 282 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- name: Install Swashbuckle CLI
shell: powershell
run: dotnet tool install -g --version 6.5.0 Swashbuckle.AspNetCore.Cli
run: dotnet tool install -g Swashbuckle.AspNetCore.Cli

- name: Install dependencies
run: dotnet restore
Expand Down
2 changes: 1 addition & 1 deletion API.Tests/API.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="21.0.2" />
Expand Down
4 changes: 2 additions & 2 deletions API.Tests/Services/WordCountAnalysisTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task ReadingTimeShouldBeNonZero()

var cacheService = new CacheHelper(new FileService());
var service = new WordCountAnalyzerService(Substitute.For<ILogger<WordCountAnalyzerService>>(), _unitOfWork,
Substitute.For<IEventHub>(), cacheService, _readerService);
Substitute.For<IEventHub>(), cacheService, _readerService, Substitute.For<IMediaErrorService>());


await service.ScanSeries(1, 1);
Expand Down Expand Up @@ -126,7 +126,7 @@ public async Task ReadingTimeShouldIncreaseWhenNewBookAdded()

var cacheService = new CacheHelper(new FileService());
var service = new WordCountAnalyzerService(Substitute.For<ILogger<WordCountAnalyzerService>>(), _unitOfWork,
Substitute.For<IEventHub>(), cacheService, _readerService);
Substitute.For<IEventHub>(), cacheService, _readerService, Substitute.For<IMediaErrorService>());
await service.ScanSeries(1, 1);

var chapter2 = new ChapterBuilder("2")
Expand Down
12 changes: 6 additions & 6 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<LangVersion>latestmajor</LangVersion>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="Build" Condition=" '$(Configuration)' == 'Debug' ">
<Exec Command="swagger tofile --output ../openapi.json bin/$(Configuration)/$(TargetFramework)/$(AssemblyName).dll v1" />
</Target>
<!-- <Target Name="PostBuild" AfterTargets="Build" Condition=" '$(Configuration)' == 'Debug' ">-->
<!-- <Exec Command="swagger tofile &#45;&#45;output ../openapi.json bin/$(Configuration)/$(TargetFramework)/$(AssemblyName).dll v1" />-->
<!-- </Target>-->

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>false</DebugSymbols>
Expand Down Expand Up @@ -95,13 +95,13 @@
<PackageReference Include="Serilog.Sinks.SignalR.Core" Version="0.1.2" />
<PackageReference Include="SharpCompress" Version="0.37.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.24.0.89429">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.25.0.90414">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.2" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.2" />
<PackageReference Include="System.IO.Abstractions" Version="21.0.2" />
<PackageReference Include="System.Drawing.Common" Version="8.0.4" />
<PackageReference Include="VersOne.Epub" Version="3.3.1" />
Expand Down
48 changes: 48 additions & 0 deletions API/Data/ManualMigrations/ManualMigrateSwitchToWal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Threading.Tasks;
using API.Entities;
using Kavita.Common.EnvironmentInfo;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace API.Data.ManualMigrations;

/// <summary>
/// v0.8.2 switches Default Kavita installs to WAL
/// </summary>
public static class ManualMigrateSwitchToWal
{
public static async Task Migrate(DataContext context, ILogger<Program> logger)
{
if (await context.ManualMigrationHistory.AnyAsync(m => m.Name == "ManualMigrateSwitchToWal"))
{
return;
}

logger.LogCritical("Running ManualMigrateSwitchToWal migration - Please be patient, this may take some time. This is not an error");
try
{
var connection = context.Database.GetDbConnection();
await connection.OpenAsync();
await using var command = connection.CreateCommand();
command.CommandText = "PRAGMA journal_mode=WAL;";
await command.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
logger.LogError(ex, "Error setting WAL");
/* Swallow */
}

await context.ManualMigrationHistory.AddAsync(new ManualMigrationHistory()
{
Name = "ManualMigrateSwitchToWal",
ProductVersion = BuildInfo.Version.ToString(),
RanAt = DateTime.UtcNow
});
await context.SaveChangesAsync();

logger.LogCritical("Running ManualMigrateSwitchToWal migration - Completed. This is not an error");
}

}
49 changes: 49 additions & 0 deletions API/Data/ManualMigrations/ManualMigrateThemeDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using API.Entities;
using Kavita.Common.EnvironmentInfo;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace API.Data.ManualMigrations;

/// <summary>
/// v0.8.2 introduced Theme repo viewer, this adds Description to existing SiteTheme defaults
/// </summary>
public static class ManualMigrateThemeDescription
{
public static async Task Migrate(DataContext context, ILogger<Program> logger)
{
if (await context.ManualMigrationHistory.AnyAsync(m => m.Name == "ManualMigrateThemeDescription"))
{
return;
}

logger.LogCritical("Running ManualMigrateThemeDescription migration - Please be patient, this may take some time. This is not an error");

var theme = await context.SiteTheme.FirstOrDefaultAsync(t => t.Name == "Dark");
if (theme != null)
{
theme.Description = Seed.DefaultThemes.First().Description;
}

if (context.ChangeTracker.HasChanges())
{
await context.SaveChangesAsync();
}




await context.ManualMigrationHistory.AddAsync(new ManualMigrationHistory()
{
Name = "ManualMigrateThemeDescription",
ProductVersion = BuildInfo.Version.ToString(),
RanAt = DateTime.UtcNow
});
await context.SaveChangesAsync();

logger.LogCritical("Running ManualMigrateThemeDescription migration - Completed. This is not an error");
}
}
4 changes: 2 additions & 2 deletions API/Data/Repositories/SeriesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,12 +1748,12 @@ public async Task<IList<Series>> RemoveSeriesNotInList(IList<ParsedSeries> seenS
{
// This is due to v0.5.6 introducing bugs where we could have multiple series get duplicated and no way to delete them
// This here will delete the 2nd one as the first is the one to likely be used.
var sId = _context.Series
var sId = await _context.Series
.Where(s => s.Format == parsedSeries.Format && s.NormalizedName == parsedSeries.NormalizedName &&
s.LibraryId == libraryId)
.Select(s => s.Id)
.OrderBy(s => s)
.Last();
.LastAsync();
if (sId > 0)
{
ids.Add(sId);
Expand Down
1 change: 1 addition & 0 deletions API/Data/Seed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ..new List<SiteTheme>
Provider = ThemeProvider.System,
FileName = "dark.scss",
IsDefault = true,
Description = "Default theme shipped with Kavita"
}
}.ToArray()
];
Expand Down
5 changes: 4 additions & 1 deletion API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ public static async Task Main(string[] args)
// Apply all migrations on startup
logger.LogInformation("Running Migrations");

// v0.7.14
try
{
// v0.7.14
await MigrateWantToReadExport.Migrate(context, directoryService, logger);

// v0.8.2
await ManualMigrateSwitchToWal.Migrate(context, logger);
}
catch (Exception ex)
{
Expand Down
3 changes: 2 additions & 1 deletion API/Services/BookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public class BookService : IBookService
{
PackageReaderOptions = new PackageReaderOptions
{
IgnoreMissingToc = true
IgnoreMissingToc = true,
SkipInvalidManifestItems = true
}
};

Expand Down
40 changes: 31 additions & 9 deletions API/Services/CacheService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using API.Data;
using API.DTOs.Reader;
Expand Down Expand Up @@ -51,6 +53,8 @@ public class CacheService : ICacheService
private readonly IReadingItemService _readingItemService;
private readonly IBookmarkService _bookmarkService;

private static readonly ConcurrentDictionary<int, SemaphoreSlim> ExtractLocks = new();

public CacheService(ILogger<CacheService> logger, IUnitOfWork unitOfWork,
IDirectoryService directoryService, IReadingItemService readingItemService,
IBookmarkService bookmarkService)
Expand Down Expand Up @@ -166,11 +170,19 @@ public string GetCachedFile(Chapter chapter)
var chapter = await _unitOfWork.ChapterRepository.GetChapterAsync(chapterId);
var extractPath = GetCachePath(chapterId);

if (_directoryService.Exists(extractPath)) return chapter;
var files = chapter?.Files.ToList();
ExtractChapterFiles(extractPath, files, extractPdfToImages);
SemaphoreSlim extractLock = ExtractLocks.GetOrAdd(chapterId, id => new SemaphoreSlim(1,1));

await extractLock.WaitAsync();
try {
if(_directoryService.Exists(extractPath)) return chapter;

return chapter;
var files = chapter?.Files.ToList();
ExtractChapterFiles(extractPath, files, extractPdfToImages);
} finally {
extractLock.Release();
}

return chapter;
}

/// <summary>
Expand All @@ -191,15 +203,25 @@ public void ExtractChapterFiles(string extractPath, IReadOnlyList<MangaFile>? fi

if (files.Count > 0 && files[0].Format == MangaFormat.Image)
{
foreach (var file in files)
// Check if all the files are Images. If so, do a directory copy, else do the normal copy
if (files.All(f => f.Format == MangaFormat.Image))
{
if (fileCount > 1)
_directoryService.ExistOrCreate(extractPath);
_directoryService.CopyFilesToDirectory(files.Select(f => f.FilePath), extractPath);
}
else
{
foreach (var file in files)
{
extraPath = file.Id + string.Empty;
if (fileCount > 1)
{
extraPath = file.Id + string.Empty;
}
_readingItemService.Extract(file.FilePath, Path.Join(extractPath, extraPath), MangaFormat.Image, files.Count);
}
_readingItemService.Extract(file.FilePath, Path.Join(extractPath, extraPath), MangaFormat.Image, files.Count);
_directoryService.Flatten(extractDi.FullName);
}
_directoryService.Flatten(extractDi.FullName);

}

foreach (var file in files)
Expand Down
7 changes: 1 addition & 6 deletions API/Services/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,18 +439,13 @@ public static void CreateMergedImage(IList<string> coverImages, CoverImageSize s
rows = 1;
cols = 2;
}
else if (coverImages.Count == 3)
{
rows = 2;
cols = 2;
}
else
{
// Default to 2x2 layout for more than 3 images
rows = 2;
cols = 2;
}


var image = Image.Black(dims.Width, dims.Height);

var thumbnailWidth = image.Width / cols;
Expand Down
7 changes: 2 additions & 5 deletions API/Services/Plus/ScrobblingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public interface IScrobblingService
public class ScrobblingService : IScrobblingService
{
private readonly IUnitOfWork _unitOfWork;
private readonly ITokenService _tokenService;
private readonly IEventHub _eventHub;
private readonly ILogger<ScrobblingService> _logger;
private readonly ILicenseService _licenseService;
Expand Down Expand Up @@ -99,12 +98,10 @@ public class ScrobblingService : IScrobblingService
private const string AccessTokenErrorMessage = "Access Token needs to be rotated to continue scrobbling";


public ScrobblingService(IUnitOfWork unitOfWork, ITokenService tokenService,
IEventHub eventHub, ILogger<ScrobblingService> logger, ILicenseService licenseService,
ILocalizationService localizationService)
public ScrobblingService(IUnitOfWork unitOfWork, IEventHub eventHub, ILogger<ScrobblingService> logger,
ILicenseService licenseService, ILocalizationService localizationService)
{
_unitOfWork = unitOfWork;
_tokenService = tokenService;
_eventHub = eventHub;
_logger = logger;
_licenseService = licenseService;
Expand Down
17 changes: 16 additions & 1 deletion API/Services/Plus/SmartCollectionSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,15 @@ private async Task SyncCollection(AppUserCollection collection)
// For everything that's not there, link it up for this user.
_logger.LogInformation("Starting Sync on {CollectionName} with {SeriesCount} Series", info.Title, info.TotalItems);

await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress,
MessageFactory.SmartCollectionProgressEvent(info.Title, string.Empty, 0, info.TotalItems, ProgressEventType.Started));

var missingCount = 0;
var missingSeries = new StringBuilder();
var counter = -1;
foreach (var seriesInfo in info.Series.OrderBy(s => s.SeriesName))
{
counter++;
try
{
// Normalize series name and localized name
Expand All @@ -164,7 +169,12 @@ private async Task SyncCollection(AppUserCollection collection)
s.NormalizedLocalizedName == normalizedSeriesName)
&& formats.Contains(s.Format));

if (existingSeries != null) continue;
if (existingSeries != null)
{
await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress,
MessageFactory.SmartCollectionProgressEvent(info.Title, seriesInfo.SeriesName, counter, info.TotalItems, ProgressEventType.Updated));
continue;
}

// Series not found in the collection, try to find it in the server
var newSeries = await _unitOfWork.SeriesRepository.GetSeriesByAnyName(seriesInfo.SeriesName,
Expand Down Expand Up @@ -196,6 +206,8 @@ private async Task SyncCollection(AppUserCollection collection)
missingSeries.Append("<br/>");
}

await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress,
MessageFactory.SmartCollectionProgressEvent(info.Title, seriesInfo.SeriesName, counter, info.TotalItems, ProgressEventType.Updated));
}

// At this point, all series in the info have been checked and added if necessary
Expand All @@ -213,6 +225,9 @@ private async Task SyncCollection(AppUserCollection collection)

await _unitOfWork.CollectionTagRepository.UpdateCollectionAgeRating(collection);

await _eventHub.SendMessageAsync(MessageFactory.NotificationProgress,
MessageFactory.SmartCollectionProgressEvent(info.Title, string.Empty, info.TotalItems, info.TotalItems, ProgressEventType.Ended));

await _eventHub.SendMessageAsync(MessageFactory.CollectionUpdated,
MessageFactory.CollectionUpdatedEvent(collection.Id), false);

Expand Down
Loading

0 comments on commit b50fa0f

Please sign in to comment.