added a delete button for private, password-protected files

This commit is contained in:
Rostislav Raykov
2024-10-17 20:30:57 +03:00
parent 9d9a3269d7
commit b161a05393
8 changed files with 38 additions and 48 deletions

View File

@@ -88,4 +88,13 @@ public class FileViewController {
populateModelAttributes(fileEntity, model, request);
return "fileView";
}
@PostMapping("/delete/{id}")
public String deleteFile(@PathVariable Long id) {
if (fileService.deleteFile(id)) {
return "redirect:/file/list";
} else {
return "redirect:/file/" + id;
}
}
}

View File

@@ -170,7 +170,7 @@ public class FileService {
fileRepository.save(fileEntity);
}
public boolean deleteFile(String uuid) {
public boolean deleteFileFromFileSystem(String uuid) {
Path path = Path.of(fileSavePath, uuid);
try {
Files.delete(path);
@@ -181,6 +181,17 @@ public class FileService {
return true;
}
public boolean deleteFile(Long id) {
Optional<FileEntity> referenceById = fileRepository.findById(id);
if (referenceById.isEmpty()) {
return false;
}
FileEntity fileEntity = referenceById.get();
fileRepository.delete(fileEntity);
return deleteFileFromFileSystem(fileEntity.uuid);
}
public boolean checkPassword(String uuid, String password) {
Optional<FileEntity> referenceByUUID = fileRepository.findByUUID(uuid);
if (referenceByUUID.isEmpty()) {

View File

@@ -31,7 +31,7 @@ public class ScheduleService {
List<FileEntity> filesForDeletion = fileRepository.getFilesForDeletion(thresholdDate);
for (FileEntity file : filesForDeletion) {
logger.info("Deleting file: {}", file);
boolean deleted = fileService.deleteFile(file.uuid);
boolean deleted = fileService.deleteFileFromFileSystem(file.uuid);
if (deleted) {
fileRepository.delete(file);
} else {

View File

@@ -3,13 +3,8 @@
<head>
<meta charset="UTF-8">
<title>Enter Password</title>
<!-- Viewport Meta Tag -->
<meta content="width=device-width, initial-scale=1" name="viewport">
<!-- Bootstrap CSS -->
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
rel="stylesheet"
>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Navbar -->
@@ -43,7 +38,6 @@
<h1 class="text-center mb-4">Enter Password</h1>
<div class="row justify-content-center">
<div class="col-12 col-md-6 col-lg-4">
<!-- Form -->
<form class="card p-4" id="passwordForm" method="post" th:action="@{/file/password}">
<input th:name="${_csrf.parameterName}" th:value="${_csrf.token}" type="hidden"/>
<input name="uuid" th:value="${uuid}" type="hidden"/>

View File

@@ -3,13 +3,8 @@
<head>
<meta charset="UTF-8">
<title>File View</title>
<!-- Viewport Meta Tag -->
<meta content="width=device-width, initial-scale=1" name="viewport">
<!-- Bootstrap 5 CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Navbar -->
@@ -45,37 +40,30 @@
<h1 class="text-center mb-4">File View</h1>
<div class="row justify-content-center">
<div class="col-12 col-md-8 col-lg-6">
<!-- Card -->
<div class="card">
<div class="card-body">
<!-- File Name -->
<h5 class="card-title text-center" th:text="${file.name}">File Name</h5>
<!-- File Description if there is a description -->
<div th:if="${!#strings.isEmpty(file.description)}">
<p class="card-text text-center mb-3" th:text="${file.description}"></p>
</div>
<!-- Uploaded/Renewed At -->
<div class="d-flex justify-content-between align-items-center border-top pt-3">
<h5 class="card-title mb-0">Uploaded/Renewed At:</h5>
<p class="card-text mb-0" th:text="${file.uploadDate}"></p>
</div>
<small class="text-muted">Files are kept only for 30 days after this date.</small>
<!-- Keep Indefinitely -->
<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>
</div>
<!-- File Size -->
<div class="d-flex justify-content-between align-items-center">
<h5 class="card-title">File Size:</h5>
<p class="card-text" th:text="${fileSize}"></p>
</div>
<!-- Link -->
<h5 class="card-title border-top pt-3">Link</h5>
<div class="input-group mb-3">
<input
@@ -94,7 +82,6 @@
</button>
</div>
<!-- Buttons Row -->
<div class="d-flex justify-content-between mt-3 border-top pt-3">
<a
class="btn btn-success"
@@ -102,6 +89,15 @@
>
Download
</a>
<form method="post" th:action="@{/file/delete/{id}(id=${file.id})}"
th:if="${file.passwordHash != null}">
<input
th:name="${_csrf.parameterName}"
th:value="${_csrf.token}"
type="hidden"
/>
<button class="btn btn-danger" type="submit">Delete File</button>
</form>
<form method="post" th:action="@{/file/extend/{id}(id=${file.id})}"
th:if="${file.keepIndefinitely == false}">
<input
@@ -114,7 +110,6 @@
</div>
</div>
</div>
<!-- End of Card -->
</div>
</div>
</div>

View File

@@ -3,13 +3,8 @@
<head>
<meta charset="UTF-8">
<title>All Files</title>
<!-- Viewport Meta Tag -->
<meta content="width=device-width, initial-scale=1" name="viewport">
<!-- Bootstrap 5 CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Navbar -->
@@ -43,9 +38,7 @@
<!-- Main Content -->
<div class="container mt-5">
<h1 class="text-center mb-4">All Files</h1>
<!-- Cards Row -->
<div class="row">
<!-- Card Column -->
<div class="col-12 col-sm-6 col-md-4 col-lg-3 mb-4" th:each="file : ${files}">
<div class="card h-100">
<div class="card-body">
@@ -63,8 +56,8 @@
<a class="btn btn-primary w-100" th:href="@{/file/{UUID}(UUID=${file.uuid})}">Go to File Page</a>
</div>
</div>
</div> <!-- End of Card Column -->
</div> <!-- End of Cards Row -->
</div>
</div>
</div>
</body>
</html>

View File

@@ -3,13 +3,8 @@
<head>
<meta charset="UTF-8">
<title>Password Required</title>
<!-- Viewport Meta Tag -->
<meta content="width=device-width, initial-scale=1" name="viewport">
<!-- Bootstrap CSS -->
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
rel="stylesheet"
>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">

View File

@@ -3,13 +3,8 @@
<head>
<meta charset="UTF-8">
<title>Upload File</title>
<!-- Viewport Meta Tag -->
<meta content="width=device-width, initial-scale=1" name="viewport">
<!-- Bootstrap CSS -->
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
rel="stylesheet"
>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Navbar -->
@@ -47,7 +42,6 @@
</p>
<div class="row justify-content-center">
<div class="col-12 col-md-8 col-lg-6">
<!-- Form -->
<form
class="card p-4"
enctype="multipart/form-data"
@@ -99,7 +93,6 @@
</form>
</div>
</div>
<!-- Upload Indicator -->
<div
class="mt-3 text-info text-center"
id="uploadIndicator"