Implemented download log files (not in service). Refactored backupservice to handle log file splitting. Improved a few interfaces and added some unit tests around them.
This commit is contained in:
parent
30352403cf
commit
bbb4240e20
22 changed files with 292 additions and 46 deletions
|
@ -142,7 +142,7 @@ namespace API.Controllers
|
|||
public async Task<ActionResult> Bookmark(BookmarkDto bookmarkDto)
|
||||
{
|
||||
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
|
||||
_logger.LogInformation("Saving {UserName} progress for Chapter {ChapterId} to page {PageNum}", user.UserName, bookmarkDto.ChapterId, bookmarkDto.PageNum);
|
||||
_logger.LogDebug("Saving {UserName} progress for Chapter {ChapterId} to page {PageNum}", user.UserName, bookmarkDto.ChapterId, bookmarkDto.PageNum);
|
||||
|
||||
// Don't let user bookmark past total pages.
|
||||
var chapter = await _unitOfWork.VolumeRepository.GetChapterAsync(bookmarkDto.ChapterId);
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
using API.Extensions;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using API.Extensions;
|
||||
using API.Interfaces.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
@ -11,11 +16,16 @@ namespace API.Controllers
|
|||
{
|
||||
private readonly IHostApplicationLifetime _applicationLifetime;
|
||||
private readonly ILogger<ServerController> _logger;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
|
||||
public ServerController(IHostApplicationLifetime applicationLifetime, ILogger<ServerController> logger)
|
||||
public ServerController(IHostApplicationLifetime applicationLifetime, ILogger<ServerController> logger, IConfiguration config,
|
||||
IDirectoryService directoryService)
|
||||
{
|
||||
_applicationLifetime = applicationLifetime;
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_directoryService = directoryService;
|
||||
}
|
||||
|
||||
[HttpPost("restart")]
|
||||
|
@ -26,5 +36,44 @@ namespace API.Controllers
|
|||
_applicationLifetime.StopApplication();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpGet("logs")]
|
||||
public async Task<ActionResult> GetLogs()
|
||||
{
|
||||
// TODO: Zip up the log files
|
||||
var maxRollingFiles = int.Parse(_config.GetSection("Logging").GetSection("File").GetSection("MaxRollingFiles").Value);
|
||||
var loggingSection = _config.GetSection("Logging").GetSection("File").GetSection("Path").Value;
|
||||
|
||||
var multipleFileRegex = maxRollingFiles > 0 ? @"\d*" : string.Empty;
|
||||
FileInfo fi = new FileInfo(loggingSection);
|
||||
|
||||
var files = _directoryService.GetFilesWithExtension(Directory.GetCurrentDirectory(), $@"{fi.Name}{multipleFileRegex}\.log");
|
||||
Console.WriteLine(files);
|
||||
|
||||
var logFile = Path.Join(Directory.GetCurrentDirectory(), loggingSection);
|
||||
_logger.LogInformation("Fetching download of logs: {LogFile}", logFile);
|
||||
|
||||
// First, copy the file to temp
|
||||
|
||||
var originalFile = new FileInfo(logFile);
|
||||
var tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");
|
||||
_directoryService.ExistOrCreate(tempDirectory);
|
||||
var tempLocation = Path.Join(tempDirectory, originalFile.Name);
|
||||
originalFile.CopyTo(tempLocation); // TODO: Make this unique based on date
|
||||
|
||||
// Read into memory
|
||||
await using var memory = new MemoryStream();
|
||||
// We need to copy it else it will throw an exception
|
||||
await using (var stream = new FileStream(tempLocation, FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
await stream.CopyToAsync(memory);
|
||||
}
|
||||
memory.Position = 0;
|
||||
|
||||
// Delete temp
|
||||
(new FileInfo(tempLocation)).Delete();
|
||||
|
||||
return File(memory, "text/plain", Path.GetFileName(logFile));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue