now keep indefinitely is a checkbox for password-protected files in the file view

This commit is contained in:
Rostislav Raykov
2024-12-02 14:03:00 +02:00
parent 98636bace5
commit bde49cfce1
3 changed files with 48 additions and 6 deletions

View File

@@ -135,8 +135,22 @@ public class FileViewController {
}
@PostMapping("/keep-indefinitely/{id}")
public String updateKeepIndefinitely(@PathVariable Long id, @RequestParam(required = false, defaultValue = "false") boolean keepIndefinitely, HttpServletRequest request) {
public String updateKeepIndefinitely(@PathVariable Long id, @RequestParam(required = false, defaultValue = "false") boolean keepIndefinitely, HttpServletRequest request, Model model) {
// Check for admin password
if (!applicationSettingsService.checkForAdminPassword(request)) {
// Check for file password
String filePassword = (String) request.getSession().getAttribute("password");
if (filePassword != null) {
FileEntity fileEntity = fileService.getFile(id);
// Check if file password is correct
if (fileEntity.passwordHash != null && !fileService.checkPassword(fileEntity.uuid, filePassword)) {
model.addAttribute("uuid", fileEntity.uuid);
return "file-password";
}
// Redirect to file page
fileService.updateKeepIndefinitely(id, keepIndefinitely);
return "redirect:/file/" + fileEntity.uuid;
}
return "redirect:/admin/password";
}

View File

@@ -277,6 +277,10 @@ public class FileService {
return;
}
if (!keepIndefinitely) {
extendFile(id);
}
FileEntity fileEntity = referenceById.get();
fileEntity.keepIndefinitely = keepIndefinitely;
logger.info("File keepIndefinitely updated: {}", fileEntity);

View File

@@ -79,11 +79,22 @@
Files are kept only for <span th:text="${maxFileLifeTime}">30</span> days after this date.
</small>
<div class="d-flex justify-content-between align-items-center pt-3">
<h5 class="card-title">
Keep
Indefinitely:</h5>
<p class="card-text"
th:text="${file.keepIndefinitely} ? 'Yes' : 'No'"></p>
<h5 class="card-title">Keep Indefinitely:</h5>
<form class="d-inline" method="post" th:action="@{/file/keep-indefinitely/{id}(id=${file.id})}">
<input th:name="${_csrf.parameterName}" th:value="${_csrf.token}" type="hidden">
<input name="keepIndefinitely" type="hidden" value="false">
<div class="form-check form-switch">
<input
class="form-check-input"
id="keepIndefinitely"
name="keepIndefinitely"
onchange="updateCheckboxState(event, this)"
th:checked="${file.keepIndefinitely}"
th:disabled="${file.passwordHash == null}"
type="checkbox"
value="true"/>
</div>
</form>
</div>
<div class="d-flex justify-content-between align-items-center">
@@ -172,6 +183,19 @@
</div>
</div>
</div>
<script>
function updateCheckboxState(event, checkbox) {
event.preventDefault();
const hiddenField = checkbox.form.querySelector('input[name="keepIndefinitely"][type="hidden"]');
if (hiddenField) {
hiddenField.value = checkbox.checked;
}
console.log('Submitting form...');
checkbox.form.submit();
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="/js/fileView.js"></script>
</body>