From b161a05393fe575a286fe22350e3b2be1e29e2d9 Mon Sep 17 00:00:00 2001 From: Rostislav Raykov Date: Thu, 17 Oct 2024 20:30:57 +0300 Subject: [PATCH] added a delete button for private, password-protected files --- .../controller/FileViewController.java | 9 +++++++ .../quickdrop/service/FileService.java | 13 +++++++++- .../quickdrop/service/ScheduleService.java | 2 +- .../resources/templates/filePassword.html | 8 +----- src/main/resources/templates/fileView.html | 25 ++++++++----------- src/main/resources/templates/listFiles.html | 13 +++------- src/main/resources/templates/password.html | 7 +----- src/main/resources/templates/upload.html | 9 +------ 8 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/main/java/org/rostislav/quickdrop/controller/FileViewController.java b/src/main/java/org/rostislav/quickdrop/controller/FileViewController.java index 8d5f65d..09f37cd 100644 --- a/src/main/java/org/rostislav/quickdrop/controller/FileViewController.java +++ b/src/main/java/org/rostislav/quickdrop/controller/FileViewController.java @@ -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; + } + } } diff --git a/src/main/java/org/rostislav/quickdrop/service/FileService.java b/src/main/java/org/rostislav/quickdrop/service/FileService.java index 2ea3eeb..317ec1c 100644 --- a/src/main/java/org/rostislav/quickdrop/service/FileService.java +++ b/src/main/java/org/rostislav/quickdrop/service/FileService.java @@ -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 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 referenceByUUID = fileRepository.findByUUID(uuid); if (referenceByUUID.isEmpty()) { diff --git a/src/main/java/org/rostislav/quickdrop/service/ScheduleService.java b/src/main/java/org/rostislav/quickdrop/service/ScheduleService.java index 7053812..94c9352 100644 --- a/src/main/java/org/rostislav/quickdrop/service/ScheduleService.java +++ b/src/main/java/org/rostislav/quickdrop/service/ScheduleService.java @@ -31,7 +31,7 @@ public class ScheduleService { List 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 { diff --git a/src/main/resources/templates/filePassword.html b/src/main/resources/templates/filePassword.html index b19b7c3..ec9aa12 100644 --- a/src/main/resources/templates/filePassword.html +++ b/src/main/resources/templates/filePassword.html @@ -3,13 +3,8 @@ Enter Password - - - + @@ -43,7 +38,6 @@

Enter Password

-
diff --git a/src/main/resources/templates/fileView.html b/src/main/resources/templates/fileView.html index fd7eda7..487c367 100644 --- a/src/main/resources/templates/fileView.html +++ b/src/main/resources/templates/fileView.html @@ -3,13 +3,8 @@ File View - - - + @@ -45,37 +40,30 @@

File View

-
-
File Name
-

-
Uploaded/Renewed At:

Files are kept only for 30 days after this date. -
Keep Indefinitely:

-
File Size:

-
Link
-
Download + + + +
-
diff --git a/src/main/resources/templates/listFiles.html b/src/main/resources/templates/listFiles.html index 8e72068..22dd2ac 100644 --- a/src/main/resources/templates/listFiles.html +++ b/src/main/resources/templates/listFiles.html @@ -3,13 +3,8 @@ All Files - - - + @@ -43,9 +38,7 @@

All Files

-
-
@@ -63,8 +56,8 @@ Go to File Page
-
-
+
+
diff --git a/src/main/resources/templates/password.html b/src/main/resources/templates/password.html index add9e56..e8bc667 100644 --- a/src/main/resources/templates/password.html +++ b/src/main/resources/templates/password.html @@ -3,13 +3,8 @@ Password Required - - - +
diff --git a/src/main/resources/templates/upload.html b/src/main/resources/templates/upload.html index 73acb39..fb9016e 100644 --- a/src/main/resources/templates/upload.html +++ b/src/main/resources/templates/upload.html @@ -3,13 +3,8 @@ Upload File - - - + @@ -47,7 +42,6 @@

-
-