Added cache clear button, added cache size estimation

This commit is contained in:
2025-11-20 22:02:28 +01:00
parent 116b47cdc0
commit baac2bce1d
5 changed files with 126 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
@using Berufsschule_HAM.Helpers
@using Berufsschule_HAM.Services
@using Microsoft.AspNetCore.Html
@using Microsoft.AspNetCore.Mvc.Localization
@@ -11,12 +12,13 @@
@{
ViewData["Title"] = T["Users"];
List<string> supportedBarcodeTypes = ["code128c", "ean13", "ean8", "upc", "itf14", "itf"];
string userImageCacheSize = ImageHelper.ToHumanReadableSize(ImageHelper.GetImageCacheSize());
}
<form id="updateSettings" style="margin-bottom: 4rem !important" method="post" asp-controller="Settings" asp-action="Admin">
<div class="row g-3">
<h4 class="fw-bold">@T["General settings"]</h4>
<div class="col-md-6">
<div class="col-md-3">
<label class="form-label" for="updateHashAlgorithm">@T["Default hash algorithm"]</label>
<select type="text" name="DefaultHashAlgorithm" id="updateHashAlgorithm" class="form-control">
@foreach (string algorithm in ldap.HashAlgorithms.Keys)
@@ -26,7 +28,7 @@
}
</select>
</div>
<div class="col-md-6">
<div class="col-md-3">
<label class="form-label" for="updateBarcodeType">@T["Barcode type"]</label>
<span
id="barcodeInfoIcon"
@@ -48,15 +50,15 @@
}
</select>
</div>
<div class="col-md-6">
<div class="col-md-3">
<label class="form-label" for="updateBarcodeText">@T["Barcode text"]</label>
<input type="text" name="BarcodeText" id="updateBarcodeText" class="form-control" value="@Model.BarcodeText" />
</div>
<div class="col-md-6">
<div class="col-md-4">
<label class="form-label" for="updateMaxDownloadableUserImageSize">@T["Max downloadable user image size"]</label>
<input type="number" id="updateMaxDownloadableUserImageSize" name="MaxDownloadableUserImageSize" class="form-control" value="@Model.MaxDownloadableUserImageSize" />
</div>
<div class="col-md-6">
<div class="col-md-3">
<label class="form-label" for="updateUserImagePreloadType">@T["User image preloading"]</label>
<select type="text" name="UserImagePreloadType" id="updateUserImagePreloadType" class="form-control" />
@foreach (UserImagePreloadType userImagePreloadType in Enum.GetValues(typeof(UserImagePreloadType)))
@@ -65,6 +67,10 @@
}
</select>
</div>
<div class="col-md-4">
<p class="form-label">@T["Current user image cache utilization:"] <span id="userImageCacheSize">@userImageCacheSize</span></p>
<button class="form-control btn btn-danger" type="button" id="clearCacheBtn">@T["Clear user image cache"]</button>
</div>
</div>
<div class="mt-3 p-2 bg border rounded d-flex justify-content-between align-items-center"
data-bs-toggle="collapse"
@@ -124,6 +130,40 @@
</form>
<script defer>
document.getElementById("clearCacheBtn").addEventListener("click", async () => {
try {
const response = await fetch("/Settings/ClearUserImageCache", {
method: "POST",
headers: {
"X-Requested-With": "XMLHttpRequest"
}
});
if (!response.ok) {
throw new Error("Request failed: " + response.status);
}
document.getElementById("userImageCacheSize").textContent = "0 B";
showToast('@T["Cache cleared successfully"]', 'success');
} catch (err) {
console.error(err);
showToast('@T["Error contacting server"]', 'danger');
}
});
setInterval(() => {
fetch('/Settings/UserImageCacheSize', {
method: 'POST'
})
.then(res => res.text())
.then(text => {
document.querySelector('#userImageCacheSize').textContent = text;
})
.catch(err => console.error('Cache-size fetch failed:', err));
}, 1000);
document.addEventListener('DOMContentLoaded', () => {
const updateForm = document.getElementById('updateSettings');
updateForm.addEventListener('submit', async e => {