CBL Import (#1834)
* Wrote my own step tracker and added a prev button. Works up to first conflict flow. * Everything but final import is hooked up in the UI. Polish still needed, but getting there. * Making more progress in the CBL import flow. * Ready for the last step * Cleaned up some logic to prepare for the last step and reset * Users like order to be starting at 1 * Fixed a few bugs around cbl import * CBL import is ready for some basic testing * Added a reading list hook on side nav * Fixed up unit tests * Added icons and color to the import flow * Tweaked some phrasing * Hooked up a loading variable but disabled the component as it didn't look good. * Styling it up * changed an icon to better fit --------- Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
This commit is contained in:
parent
57de661d71
commit
d88a4d5d0c
26 changed files with 1125 additions and 466 deletions
68
API/Controllers/CBLController.cs
Normal file
68
API/Controllers/CBLController.cs
Normal file
|
@ -0,0 +1,68 @@
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs.ReadingLists;
|
||||
using API.DTOs.ReadingLists.CBL;
|
||||
using API.Extensions;
|
||||
using API.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// Responsible for the CBL import flow
|
||||
/// </summary>
|
||||
public class CblController : BaseApiController
|
||||
{
|
||||
private readonly IReadingListService _readingListService;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
|
||||
public CblController(IReadingListService readingListService, IDirectoryService directoryService)
|
||||
{
|
||||
_readingListService = readingListService;
|
||||
_directoryService = directoryService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The first step in a cbl import. This validates the cbl file that if an import occured, would it be successful.
|
||||
/// If this returns errors, the cbl will always be rejected by Kavita.
|
||||
/// </summary>
|
||||
/// <param name="file">FormBody with parameter name of cbl</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("validate")]
|
||||
public async Task<ActionResult<CblImportSummaryDto>> ValidateCbl([FromForm(Name = "cbl")] IFormFile file)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
var cbl = await SaveAndLoadCblFile(userId, file);
|
||||
|
||||
var importSummary = await _readingListService.ValidateCblFile(userId, cbl);
|
||||
return Ok(importSummary);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Performs the actual import (assuming dryRun = false)
|
||||
/// </summary>
|
||||
/// <param name="file">FormBody with parameter name of cbl</param>
|
||||
/// <param name="dryRun">If true, will only emulate the import but not perform. This should be done to preview what will happen</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("import")]
|
||||
public async Task<ActionResult<CblImportSummaryDto>> ImportCbl([FromForm(Name = "cbl")] IFormFile file, [FromForm(Name = "dryRun")] bool dryRun = false)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
var cbl = await SaveAndLoadCblFile(userId, file);
|
||||
|
||||
return Ok(await _readingListService.CreateReadingListFromCbl(userId, cbl, dryRun));
|
||||
}
|
||||
|
||||
private async Task<CblReadingList> SaveAndLoadCblFile(int userId, IFormFile file)
|
||||
{
|
||||
var filename = Path.GetRandomFileName();
|
||||
var outputFile = Path.Join(_directoryService.TempDirectory, filename);
|
||||
await using var stream = System.IO.File.Create(outputFile);
|
||||
await file.CopyToAsync(stream);
|
||||
stream.Close();
|
||||
return ReadingListService.LoadCblFromPath(outputFile);
|
||||
}
|
||||
}
|
|
@ -484,22 +484,4 @@ public class ReadingListController : BaseApiController
|
|||
if (string.IsNullOrEmpty(name)) return true;
|
||||
return Ok(await _unitOfWork.ReadingListRepository.ReadingListExists(name));
|
||||
}
|
||||
|
||||
// [HttpPost("import-cbl")]
|
||||
// public async Task<ActionResult<CblImportSummaryDto>> ImportCbl([FromForm(Name = "cbl")] IFormFile file, [FromForm(Name = "dryRun")] bool dryRun = false)
|
||||
// {
|
||||
// var userId = User.GetUserId();
|
||||
// var filename = Path.GetRandomFileName();
|
||||
// var outputFile = Path.Join(_directoryService.TempDirectory, filename);
|
||||
//
|
||||
// await using var stream = System.IO.File.Create(outputFile);
|
||||
// await file.CopyToAsync(stream);
|
||||
// stream.Close();
|
||||
// var cbl = ReadingListService.LoadCblFromPath(outputFile);
|
||||
//
|
||||
// // We need to pass the temp file back
|
||||
//
|
||||
// var importSummary = await _readingListService.ValidateCblFile(userId, cbl);
|
||||
// return importSummary.Results.Any() ? Ok(importSummary) : Ok(await _readingListService.CreateReadingListFromCbl(userId, cbl, dryRun));
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using API.DTOs.ReadingLists.CBL;
|
||||
|
||||
namespace API.DTOs.ReadingLists;
|
||||
namespace API.DTOs.ReadingLists.CBL;
|
||||
|
||||
public enum CblImportResult {
|
||||
/// <summary>
|
||||
|
@ -64,10 +63,19 @@ public enum CblImportReason
|
|||
/// </summary>
|
||||
[Description("All Chapters Missing")]
|
||||
AllChapterMissing = 7,
|
||||
/// <summary>
|
||||
/// The Chapter was imported
|
||||
/// </summary>
|
||||
[Description("Success")]
|
||||
Success = 8,
|
||||
}
|
||||
|
||||
public class CblBookResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Order in the CBL
|
||||
/// </summary>
|
||||
public int Order { get; set; }
|
||||
public string Series { get; set; }
|
||||
public string Volume { get; set; }
|
||||
public string Number { get; set; }
|
||||
|
@ -95,10 +103,5 @@ public class CblImportSummaryDto
|
|||
public ICollection<CblBookResult> Results { get; set; }
|
||||
public CblImportResult Success { get; set; }
|
||||
public ICollection<CblBookResult> SuccessfulInserts { get; set; }
|
||||
/// <summary>
|
||||
/// A list of Series that are within the CBL but map to multiple libraries within Kavita
|
||||
/// </summary>
|
||||
public IList<SeriesDto> Conflicts { get; set; }
|
||||
public IList<CblConflictQuestion> Conflicts2 { get; set; }
|
||||
|
||||
}
|
||||
|
|
|
@ -117,7 +117,7 @@ public interface ISeriesRepository
|
|||
Task<bool> IsSeriesInWantToRead(int userId, int seriesId);
|
||||
Task<Series> GetSeriesByFolderPath(string folder, SeriesIncludes includes = SeriesIncludes.None);
|
||||
|
||||
Task<IEnumerable<Series>> GetAllSeriesByNameAsync(IEnumerable<string> normalizedNames,
|
||||
Task<IEnumerable<Series>> GetAllSeriesByNameAsync(IList<string> normalizedNames,
|
||||
int userId, SeriesIncludes includes = SeriesIncludes.None);
|
||||
Task<IEnumerable<SeriesDto>> GetAllSeriesDtosByNameAsync(IEnumerable<string> normalizedNames,
|
||||
int userId, SeriesIncludes includes = SeriesIncludes.None);
|
||||
|
@ -1213,14 +1213,14 @@ public class SeriesRepository : ISeriesRepository
|
|||
.SingleOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Series>> GetAllSeriesByNameAsync(IEnumerable<string> normalizedNames,
|
||||
public async Task<IEnumerable<Series>> GetAllSeriesByNameAsync(IList<string> normalizedNames,
|
||||
int userId, SeriesIncludes includes = SeriesIncludes.None)
|
||||
{
|
||||
var libraryIds = _context.Library.GetUserLibraries(userId);
|
||||
var userRating = await _context.AppUser.GetUserAgeRestriction(userId);
|
||||
|
||||
return await _context.Series
|
||||
.Where(s => normalizedNames.Contains(s.NormalizedName))
|
||||
.Where(s => normalizedNames.Contains(s.NormalizedName) || normalizedNames.Contains(s.NormalizedLocalizedName))
|
||||
.Where(s => libraryIds.Contains(s.LibraryId))
|
||||
.RestrictAgainstAgeRestriction(userRating)
|
||||
.Includes(includes)
|
||||
|
|
|
@ -355,13 +355,20 @@ public class ReadingListService : IReadingListService
|
|||
CblName = cblReading.Name,
|
||||
Success = CblImportResult.Success,
|
||||
Results = new List<CblBookResult>(),
|
||||
SuccessfulInserts = new List<CblBookResult>(),
|
||||
Conflicts = new List<SeriesDto>(),
|
||||
Conflicts2 = new List<CblConflictQuestion>()
|
||||
SuccessfulInserts = new List<CblBookResult>()
|
||||
};
|
||||
if (IsCblEmpty(cblReading, importSummary, out var readingListFromCbl)) return readingListFromCbl;
|
||||
|
||||
var uniqueSeries = cblReading.Books.Book.Select(b => Tasks.Scanner.Parser.Parser.Normalize(b.Series)).Distinct();
|
||||
// Is there another reading list with the same name?
|
||||
if (await _unitOfWork.ReadingListRepository.ReadingListExists(cblReading.Name))
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult()
|
||||
{
|
||||
Reason = CblImportReason.NameConflict
|
||||
});
|
||||
}
|
||||
|
||||
var uniqueSeries = cblReading.Books.Book.Select(b => Tasks.Scanner.Parser.Parser.Normalize(b.Series)).Distinct().ToList();
|
||||
var userSeries =
|
||||
(await _unitOfWork.SeriesRepository.GetAllSeriesByNameAsync(uniqueSeries, userId, SeriesIncludes.Chapters)).ToList();
|
||||
if (!userSeries.Any())
|
||||
|
@ -421,10 +428,11 @@ public class ReadingListService : IReadingListService
|
|||
SuccessfulInserts = new List<CblBookResult>()
|
||||
};
|
||||
|
||||
var uniqueSeries = cblReading.Books.Book.Select(b => Tasks.Scanner.Parser.Parser.Normalize(b.Series)).Distinct();
|
||||
var uniqueSeries = cblReading.Books.Book.Select(b => Tasks.Scanner.Parser.Parser.Normalize(b.Series)).Distinct().ToList();
|
||||
var userSeries =
|
||||
(await _unitOfWork.SeriesRepository.GetAllSeriesByNameAsync(uniqueSeries, userId, SeriesIncludes.Chapters)).ToList();
|
||||
var allSeries = userSeries.ToDictionary(s => Tasks.Scanner.Parser.Parser.Normalize(s.Name));
|
||||
var allSeriesLocalized = userSeries.ToDictionary(s => Tasks.Scanner.Parser.Parser.Normalize(s.LocalizedName));
|
||||
|
||||
var readingListNameNormalized = Tasks.Scanner.Parser.Parser.Normalize(cblReading.Name);
|
||||
// Get all the user's reading lists
|
||||
|
@ -452,38 +460,52 @@ public class ReadingListService : IReadingListService
|
|||
foreach (var (book, i) in cblReading.Books.Book.Select((value, i) => ( value, i )))
|
||||
{
|
||||
var normalizedSeries = Tasks.Scanner.Parser.Parser.Normalize(book.Series);
|
||||
if (!allSeries.TryGetValue(normalizedSeries, out var bookSeries))
|
||||
if (!allSeries.TryGetValue(normalizedSeries, out var bookSeries) && !allSeriesLocalized.TryGetValue(normalizedSeries, out bookSeries))
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult(book)
|
||||
{
|
||||
Reason = CblImportReason.SeriesMissing
|
||||
Reason = CblImportReason.SeriesMissing,
|
||||
Order = i
|
||||
});
|
||||
continue;
|
||||
}
|
||||
// Prioritize lookup by Volume then Chapter, but allow fallback to just Chapter
|
||||
var matchingVolume = bookSeries.Volumes.FirstOrDefault(v => book.Volume == v.Name) ?? bookSeries.Volumes.FirstOrDefault(v => v.Number == 0);
|
||||
var bookVolume = string.IsNullOrEmpty(book.Volume)
|
||||
? Tasks.Scanner.Parser.Parser.DefaultVolume
|
||||
: book.Volume;
|
||||
var matchingVolume = bookSeries.Volumes.FirstOrDefault(v => bookVolume == v.Name) ?? bookSeries.Volumes.FirstOrDefault(v => v.Number == 0);
|
||||
if (matchingVolume == null)
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult(book)
|
||||
{
|
||||
Reason = CblImportReason.VolumeMissing
|
||||
Reason = CblImportReason.VolumeMissing,
|
||||
Order = i
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
var chapter = matchingVolume.Chapters.FirstOrDefault(c => c.Number == book.Number);
|
||||
// We need to handle chapter 0 or empty string when it's just a volume
|
||||
var bookNumber = string.IsNullOrEmpty(book.Number)
|
||||
? Tasks.Scanner.Parser.Parser.DefaultChapter
|
||||
: book.Number;
|
||||
var chapter = matchingVolume.Chapters.FirstOrDefault(c => c.Number == bookNumber);
|
||||
if (chapter == null)
|
||||
{
|
||||
importSummary.Results.Add(new CblBookResult(book)
|
||||
{
|
||||
Reason = CblImportReason.ChapterMissing
|
||||
Reason = CblImportReason.ChapterMissing,
|
||||
Order = i
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// See if a matching item already exists
|
||||
ExistsOrAddReadingListItem(readingList, bookSeries.Id, matchingVolume.Id, chapter.Id);
|
||||
importSummary.SuccessfulInserts.Add(new CblBookResult(book));
|
||||
importSummary.SuccessfulInserts.Add(new CblBookResult(book)
|
||||
{
|
||||
Reason = CblImportReason.Success,
|
||||
Order = i
|
||||
});
|
||||
}
|
||||
|
||||
if (importSummary.SuccessfulInserts.Count != cblReading.Books.Book.Count || importSummary.Results.Count > 0)
|
||||
|
@ -491,9 +513,14 @@ public class ReadingListService : IReadingListService
|
|||
importSummary.Success = CblImportResult.Partial;
|
||||
}
|
||||
|
||||
if (importSummary.SuccessfulInserts.Count == 0 && importSummary.Results.Count == cblReading.Books.Book.Count)
|
||||
{
|
||||
importSummary.Success = CblImportResult.Fail;
|
||||
}
|
||||
|
||||
await CalculateReadingListAgeRating(readingList);
|
||||
|
||||
if (!dryRun) return importSummary;
|
||||
if (dryRun) return importSummary;
|
||||
|
||||
if (!_unitOfWork.HasChanges()) return importSummary;
|
||||
await _unitOfWork.CommitAsync();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue