Add API Controller method to clean up temp files and unlinked thumbnails.
This commit is contained in:
@@ -562,6 +562,28 @@ namespace CarCareTracker.Controllers
|
|||||||
}
|
}
|
||||||
[Authorize(Roles = nameof(UserData.IsRootUser))]
|
[Authorize(Roles = nameof(UserData.IsRootUser))]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
[Route("/api/cleanup")]
|
||||||
|
public IActionResult CleanUp(bool deepClean = false)
|
||||||
|
{
|
||||||
|
var jsonResponse = new Dictionary<string, string>();
|
||||||
|
//Clear out temp folder
|
||||||
|
var tempFilesDeleted = _fileHelper.ClearTempFolder();
|
||||||
|
jsonResponse.Add("Temp Files Deleted", tempFilesDeleted.ToString());
|
||||||
|
if (deepClean)
|
||||||
|
{
|
||||||
|
//clear out unused vehicle thumbnails
|
||||||
|
var vehicles = _dataAccess.GetVehicles();
|
||||||
|
var vehicleImages = vehicles.Select(x => x.ImageLocation).Where(x => x.StartsWith("/images/")).Select(x=>Path.GetFileName(x)).ToList();
|
||||||
|
if (vehicleImages.Any())
|
||||||
|
{
|
||||||
|
var thumbnailsDeleted = _fileHelper.ClearUnlinkedThumbnails(vehicleImages);
|
||||||
|
jsonResponse.Add("Unlinked Thumbnails Deleted", thumbnailsDeleted.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Json(jsonResponse);
|
||||||
|
}
|
||||||
|
[Authorize(Roles = nameof(UserData.IsRootUser))]
|
||||||
|
[HttpGet]
|
||||||
[Route("/api/demo/restore")]
|
[Route("/api/demo/restore")]
|
||||||
public IActionResult RestoreDemo()
|
public IActionResult RestoreDemo()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ namespace CarCareTracker.Helper
|
|||||||
bool RestoreBackup(string fileName, bool clearExisting = false);
|
bool RestoreBackup(string fileName, bool clearExisting = false);
|
||||||
string MakeAttachmentsExport(List<GenericReportModel> exportData);
|
string MakeAttachmentsExport(List<GenericReportModel> exportData);
|
||||||
List<string> GetLanguages();
|
List<string> GetLanguages();
|
||||||
|
int ClearTempFolder();
|
||||||
|
int ClearUnlinkedThumbnails(List<string> linkedImages);
|
||||||
}
|
}
|
||||||
public class FileHelper : IFileHelper
|
public class FileHelper : IFileHelper
|
||||||
{
|
{
|
||||||
@@ -314,5 +316,38 @@ namespace CarCareTracker.Helper
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public int ClearTempFolder()
|
||||||
|
{
|
||||||
|
int filesDeleted = 0;
|
||||||
|
var tempPath = GetFullFilePath("temp", false);
|
||||||
|
if (Directory.Exists(tempPath))
|
||||||
|
{
|
||||||
|
var files = Directory.GetFiles(tempPath);
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
File.Delete(file);
|
||||||
|
filesDeleted++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filesDeleted;
|
||||||
|
}
|
||||||
|
public int ClearUnlinkedThumbnails(List<string> linkedImages)
|
||||||
|
{
|
||||||
|
int filesDeleted = 0;
|
||||||
|
var imagePath = GetFullFilePath("images", false);
|
||||||
|
if (Directory.Exists(imagePath))
|
||||||
|
{
|
||||||
|
var files = Directory.GetFiles(imagePath);
|
||||||
|
foreach(var file in files)
|
||||||
|
{
|
||||||
|
if (!linkedImages.Contains(Path.GetFileName(file)))
|
||||||
|
{
|
||||||
|
File.Delete(file);
|
||||||
|
filesDeleted++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filesDeleted;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user