Kavita/API/Controllers/CBLController.cs
Joe Milazzo d88a4d5d0c
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>
2023-03-03 14:51:11 -08:00

69 lines
2.5 KiB
C#

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