This commit is contained in:
2020-09-03 18:30:00 +00:00
parent 3deaa13ce8
commit 01b93829f4
4 changed files with 101 additions and 12 deletions

View File

@@ -1,12 +1,13 @@
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System.Linq;
using System.IO; using System.IO;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using AyaNova.Models; using AyaNova.Models;
using AyaNova.Api.ControllerHelpers; using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz; using AyaNova.Biz;
@@ -292,21 +293,21 @@ namespace AyaNova.Api.Controllers
return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED)); return StatusCode(401, new ApiErrorResponse(ApiErrorCode.AUTHENTICATION_FAILED));
} }
if (!Authorized.HasModifyRole(DownloadUser.Roles, AyaType.Backup))//not technically modify but treating as such as a backup is very sensitive data // if (!Authorized.HasModifyRole(DownloadUser.Roles, AyaType.Backup))//not technically modify but treating as such as a backup is very sensitive data
{ // {
await Task.Delay(nFailedAuthDelay);//DOS protection // await Task.Delay(nFailedAuthDelay);//DOS protection
return StatusCode(403, new ApiNotAuthorizedResponse()); // return StatusCode(403, new ApiNotAuthorizedResponse());
} // }
if (!FileUtil.BackupFileExists(fileName)) if (!FileUtil.TemporaryFileExists(fileName))
{ {
await Task.Delay(nFailedAuthDelay);//fishing protection await Task.Delay(nFailedAuthDelay);//fishing protection
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
} }
string mimetype = fileName.EndsWith("zip") ? "application/zip" : "application/octet-stream";
var utilityFilePath = FileUtil.GetFullPathForBackupFile(fileName); var FilePath = FileUtil.GetFullPathForTemporaryFile(fileName);
await EventLogProcessor.LogEventToDatabaseAsync(new Event(DownloadUser.Id, 0, AyaType.NoType, AyaEvent.UtilityFileDownload, fileName), ct); // await EventLogProcessor.LogEventToDatabaseAsync(new Event(DownloadUser.Id, 0, AyaType.NoType, AyaEvent.UtilityFileDownload, fileName), ct);
return PhysicalFile(utilityFilePath, mimetype, fileName); return PhysicalFile(FilePath, "application/pdf");
} }

View File

@@ -209,7 +209,8 @@ namespace AyaNova.Biz
//JOB SWEEPER / AND USER COUNT CHECK //JOB SWEEPER / AND USER COUNT CHECK
await CoreJobSweeper.DoWorkAsync(); await CoreJobSweeper.DoWorkAsync();
//Clean temp folder
CoreJobTempFolderCleanup.DoWork();
log.LogDebug("Processing exclusive dynamic jobs"); log.LogDebug("Processing exclusive dynamic jobs");

View File

@@ -0,0 +1,43 @@
using System;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using AyaNova.Util;
using AyaNova.Models;
using Microsoft.EntityFrameworkCore;
namespace AyaNova.Biz
{
/// <summary>
/// called by Generator to keep temp folder squeaky clean
/// </summary>
internal static class CoreJobTempFolderCleanup
{
private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreJobTempFolderCleanup");
private static DateTime _lastRun = DateTime.UtcNow;
private static TimeSpan tsRunEvery = new TimeSpan(0, 0, 1);//every this minutes run the cleanup task
////////////////////////////////////////////////////////////////////////////////////////////////
//
//
public static void DoWork()
{
if (DateUtil.IsAfterDuration(_lastRun, tsRunEvery))
{
log.LogTrace("Temp cleanup now");
FileUtil.CleanTemporaryFilesFolder(new TimeSpan(0,5,0));//erase any files found to be older than 5 minutes
var now = DateTime.UtcNow;
_lastRun = now;
}
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons

View File

@@ -71,6 +71,17 @@ namespace AyaNova.Util
#endregion folder ensurance #endregion folder ensurance
#region Temporary files handling #region Temporary files handling
/// <summary>
/// Get a path combining supplied file name and backup files folder
/// </summary>
/// <returns></returns>
internal static string GetFullPathForTemporaryFile(string fileName)
{
return Path.Combine(TemporaryFilesFolder, fileName);
}
/// <summary> /// <summary>
/// Get backup file folder /// Get backup file folder
/// </summary> /// </summary>
@@ -96,6 +107,39 @@ namespace AyaNova.Util
} }
} }
/// <summary>
/// Confirm if a file exists in the temporary files folder
/// </summary>
/// <param name="fileName">name of temp folder file </param>
/// <returns>duh!</returns>
internal static bool TemporaryFileExists(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
return false;
var FilePath = GetFullPathForTemporaryFile(fileName);
return File.Exists(FilePath);
}
/// <summary>
/// Erase all files found to be older than age
/// </summary>
internal static void CleanTemporaryFilesFolder(TimeSpan age)
{
DateTime EraseIfOlderThan = DateTime.UtcNow - age;
System.IO.DirectoryInfo di = new DirectoryInfo(TemporaryFilesFolder);
foreach (FileInfo file in di.EnumerateFiles())
{
if (file.CreationTimeUtc < EraseIfOlderThan)
{
string s = "Erase, tora tora tora";
//file.Delete();
}
}
}
#endregion #endregion