This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using AyaNova.Models;
|
||||
using AyaNova.Api.ControllerHelpers;
|
||||
using AyaNova.Biz;
|
||||
@@ -292,21 +293,21 @@ namespace AyaNova.Api.Controllers
|
||||
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
|
||||
{
|
||||
await Task.Delay(nFailedAuthDelay);//DOS protection
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
}
|
||||
// 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
|
||||
// return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
// }
|
||||
|
||||
if (!FileUtil.BackupFileExists(fileName))
|
||||
if (!FileUtil.TemporaryFileExists(fileName))
|
||||
{
|
||||
await Task.Delay(nFailedAuthDelay);//fishing protection
|
||||
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||
}
|
||||
string mimetype = fileName.EndsWith("zip") ? "application/zip" : "application/octet-stream";
|
||||
var utilityFilePath = FileUtil.GetFullPathForBackupFile(fileName);
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(DownloadUser.Id, 0, AyaType.NoType, AyaEvent.UtilityFileDownload, fileName), ct);
|
||||
return PhysicalFile(utilityFilePath, mimetype, fileName);
|
||||
|
||||
var FilePath = FileUtil.GetFullPathForTemporaryFile(fileName);
|
||||
// await EventLogProcessor.LogEventToDatabaseAsync(new Event(DownloadUser.Id, 0, AyaType.NoType, AyaEvent.UtilityFileDownload, fileName), ct);
|
||||
return PhysicalFile(FilePath, "application/pdf");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -209,7 +209,8 @@ namespace AyaNova.Biz
|
||||
//JOB SWEEPER / AND USER COUNT CHECK
|
||||
await CoreJobSweeper.DoWorkAsync();
|
||||
|
||||
|
||||
//Clean temp folder
|
||||
CoreJobTempFolderCleanup.DoWork();
|
||||
|
||||
log.LogDebug("Processing exclusive dynamic jobs");
|
||||
|
||||
|
||||
43
server/AyaNova/generator/CoreJobTempFolderCleanup.cs
Normal file
43
server/AyaNova/generator/CoreJobTempFolderCleanup.cs
Normal 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
|
||||
|
||||
@@ -71,6 +71,17 @@ namespace AyaNova.Util
|
||||
#endregion folder ensurance
|
||||
|
||||
#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>
|
||||
/// Get backup file folder
|
||||
/// </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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user