Implemented the ability to send images to frontend with some contextual information.

This commit is contained in:
Joseph Milazzo 2021-01-08 15:17:39 -06:00
parent 7ab7e8acc4
commit 7bf04dcdac
6 changed files with 74 additions and 10 deletions

View File

@ -1,7 +1,9 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs;
using API.Entities;
using API.Interfaces;
using Microsoft.AspNetCore.Mvc;
@ -38,9 +40,23 @@ namespace API.Controllers
return BadRequest("There file is no longer there or has no images. Please rescan.");
}
// NOTE: I'm starting to think this should actually cache the information about Volume/Manga file in the DB.
// It will be updated each time this is called which is on open of a manga.
return Ok(_directoryService.ListFiles(extractPath).Count());
}
[HttpGet("image")]
public async Task<ActionResult<ImageDto>> GetImage(int volumeId, int page)
{
// Temp let's iterate the directory each call to get next image
var files = _directoryService.ListFiles(_directoryService.GetExtractPath(volumeId));
var path = files.ElementAt(page);
var file = await _directoryService.ReadImageAsync(path);
file.Page = page;
return Ok(file);
//return File(await _directoryService.ReadImageAsync(path), "image/jpg", filename);
}
}
}

13
API/DTOs/ImageDto.cs Normal file
View File

@ -0,0 +1,13 @@
namespace API.DTOs
{
public class ImageDto
{
public int Page { get; set; }
public string Filename { get; init; }
public string FullPath { get; init; }
public int Width { get; init; }
public int Height { get; init; }
public string Format { get; init; }
public byte[] Content { get; init; }
}
}

View File

@ -18,7 +18,11 @@ namespace API.Data
foreach (var role in roles)
{
await roleManager.CreateAsync(role);
var exists = await roleManager.RoleExistsAsync(role.Name);
if (!exists)
{
await roleManager.CreateAsync(role);
}
}
}
}

View File

@ -5,6 +5,7 @@ namespace API.Entities
{
public int Id { get; set; }
public string FilePath { get; set; }
// Should I just store information related to FilePath here? Reset it on anytime FilePath changes?
// Relationship Mapping
public Volume Volume { get; set; }

View File

@ -1,7 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using API.DTOs;
namespace API.Interfaces
{
// TODO: Refactor this into IDiskService to encapsulate all disk based IO
public interface IDirectoryService
{
/// <summary>
@ -36,6 +39,14 @@ namespace API.Interfaces
/// <param name="volumeId">Id of volume being extracted.</param>
/// <returns></returns>
string ExtractArchive(string archivePath, int volumeId);
/// <summary>
/// Returns the path a volume would be extracted to.
/// </summary>
/// <param name="volumeId"></param>
/// <returns></returns>
string GetExtractPath(int volumeId);
Task<ImageDto> ReadImageAsync(string imagePath);
}
}

View File

@ -9,12 +9,13 @@ using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using API.DTOs;
using API.Entities;
using API.Interfaces;
using API.IO;
using API.Parser;
using Hangfire;
using Microsoft.Extensions.Logging;
using NetVips;
namespace API.Services
{
@ -244,8 +245,11 @@ namespace API.Services
_scannedSeries = null;
Console.WriteLine("Processed {0} files in {1} milliseconds", library.Name, sw.ElapsedMilliseconds);
}
public string GetExtractPath(int volumeId)
{
return Path.Join(Directory.GetCurrentDirectory(), $"../cache/{volumeId}/");
}
public string ExtractArchive(string archivePath, int volumeId)
{
@ -255,7 +259,7 @@ namespace API.Services
return "";
}
var extractPath = Path.Join(Directory.GetCurrentDirectory(), $"../cache/{volumeId}/");
var extractPath = GetExtractPath(volumeId);
if (Directory.Exists(extractPath))
{
@ -273,11 +277,26 @@ namespace API.Services
return extractPath;
}
public async Task<ImageDto> ReadImageAsync(string imagePath)
{
using var image = Image.NewFromFile(imagePath);
return new ImageDto
{
Content = await File.ReadAllBytesAsync(imagePath),
Filename = Path.GetFileNameWithoutExtension(imagePath),
FullPath = Path.GetFullPath(imagePath),
Width = image.Width,
Height = image.Height,
Format = image.Format
};
}
private static void TraverseTreeParallelForEach(string root, Action<string> action)
{
//Count of files traversed and timer for diagnostic output
int fileCount = 0;
var sw = Stopwatch.StartNew();
//var sw = Stopwatch.StartNew();
// Determine whether to parallelize file processing on each folder based on processor count.
int procCount = Environment.ProcessorCount;
@ -366,7 +385,7 @@ namespace API.Services
}
// For diagnostic purposes.
Console.WriteLine("Processed {0} files in {1} milliseconds", fileCount, sw.ElapsedMilliseconds);
//Console.WriteLine("Processed {0} files in {1} milliseconds", fileCount, sw.ElapsedMilliseconds);
}
}