From 879531d1a8535b0b48f8a4e08a5afde2021bbf7a Mon Sep 17 00:00:00 2001 From: liosha84 <138026690+liosha84@users.noreply.github.com> Date: Wed, 6 Aug 2025 23:16:53 +0300 Subject: [PATCH] Implement file name extraction from URL and enhance logging in tutorial operations --- .../controllers/RedirectController.java | 12 -------- .../controllers/TutorialController.java | 20 +++++++++---- .../FileUploadExceptionHandler.java | 22 +++++++++----- .../fileUpload/FileUploadExceptionAdvice.java | 19 ------------ .../fileUpload/FilesStorageService.java | 2 ++ .../fileUpload/FilesStorageServiceImpl.java | 29 ++++++++++++------- 6 files changed, 49 insertions(+), 55 deletions(-) delete mode 100644 src/main/java/com/jambotronGroup/jambotron/controllers/RedirectController.java delete mode 100644 src/main/java/com/jambotronGroup/jambotron/fileUpload/FileUploadExceptionAdvice.java diff --git a/src/main/java/com/jambotronGroup/jambotron/controllers/RedirectController.java b/src/main/java/com/jambotronGroup/jambotron/controllers/RedirectController.java deleted file mode 100644 index db09ee0..0000000 --- a/src/main/java/com/jambotronGroup/jambotron/controllers/RedirectController.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.jambotronGroup.jambotron.controllers; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; - -/*@Controller -public class RedirectController { - @RequestMapping(value = "/{path:[^\\.]*}") - public String redirect() { - return "forward:/index.html"; - } -}*/ diff --git a/src/main/java/com/jambotronGroup/jambotron/controllers/TutorialController.java b/src/main/java/com/jambotronGroup/jambotron/controllers/TutorialController.java index dd1c6d8..4c6d1db 100644 --- a/src/main/java/com/jambotronGroup/jambotron/controllers/TutorialController.java +++ b/src/main/java/com/jambotronGroup/jambotron/controllers/TutorialController.java @@ -10,6 +10,8 @@ import com.jambotronGroup.jambotron.repository.UserRepository; import com.jambotronGroup.jambotron.security.AuthenticationFacade; import com.jambotronGroup.jambotron.security.services.UserDetailsImpl; import com.jambotronGroup.jambotron.utils.FilesRoutingHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -30,6 +32,8 @@ import java.util.*; @RequestMapping("/api") public class TutorialController { + private static final Logger _logger = LoggerFactory.getLogger(AuthController.class); + @Autowired AuthenticationFacade authenticationFacade; @Autowired @@ -112,7 +116,7 @@ public class TutorialController { User user = authenticationFacade.getUser(); - String newFilename = FilesStorageServiceImpl.getFileNameFromUrl(tutorial.getTitleimage()); + String newFilename = filesStorageService.getFileNameFromUrl(tutorial.getTitleimage()); Path path= filesStorageService.moveFile( user.getId().toString(), tutorial.getTitleimage(), @@ -121,7 +125,6 @@ public class TutorialController { String url = FilesRoutingHelper.getPublicImageUrl(path.getFileName().toString()); - try { Tutorial newTutorial =new Tutorial( tutorial.getTitle(), @@ -137,8 +140,10 @@ public class TutorialController { Tutorial _tutorial = tutorialRepository .save(newTutorial); + _logger.info("Tutorial created: " + _tutorial.getId() + " by user: " + user.getId()); return new ResponseEntity<>(_tutorial, HttpStatus.CREATED); } catch (Exception e) { + _logger.error("Error creating tutorial: ", e.getMessage()); return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -164,8 +169,6 @@ public class TutorialController { User user = authenticationFacade.getUser(); - - Optional tutorialData = tutorialRepository.findById(id); Map map = new LinkedHashMap(); if (tutorialData.isPresent()) { @@ -177,8 +180,8 @@ public class TutorialController { try { - String imageFileName = FilesStorageServiceImpl.getFileNameFromUrl(tutorial.getTitleimage()); - String servImageFileName = FilesStorageServiceImpl.getFileNameFromUrl(servTutorial.getTitleimage()); + String imageFileName = filesStorageService.getFileNameFromUrl(tutorial.getTitleimage()); + String servImageFileName = filesStorageService.getFileNameFromUrl(servTutorial.getTitleimage()); if(!servImageFileName.equals(imageFileName)){ filesStorageService.deletePublicFile(servImageFileName); @@ -197,10 +200,13 @@ public class TutorialController { map.put("status", 0); map.put("message", e.getMessage()); + _logger.error("Error updating tutorial: ", e.getMessage()); return new ResponseEntity<>(map,HttpStatus.INTERNAL_SERVER_ERROR); } + _logger.info("Tutorial updated: " + servTutorial.getId() + " by user: " + user.getId()); return new ResponseEntity<>(servTutorial, HttpStatus.OK); } else { + _logger.error("Tutorial with id: " + id + " not found for user: " + user.getId()); return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } @@ -209,8 +215,10 @@ public class TutorialController { public ResponseEntity deleteTutorial(@PathVariable("id") long id) { try { tutorialRepository.deleteById(id); + _logger.info("Tutorial deleted: " + id + " by user: " + authenticationFacade.getUserDetails().getId()); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (Exception e) { + _logger.error("Error deleting tutorial: ", e.getMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/src/main/java/com/jambotronGroup/jambotron/exceptionHandlers/FileUploadExceptionHandler.java b/src/main/java/com/jambotronGroup/jambotron/exceptionHandlers/FileUploadExceptionHandler.java index 9e69a0c..03dbd15 100644 --- a/src/main/java/com/jambotronGroup/jambotron/exceptionHandlers/FileUploadExceptionHandler.java +++ b/src/main/java/com/jambotronGroup/jambotron/exceptionHandlers/FileUploadExceptionHandler.java @@ -1,6 +1,9 @@ package com.jambotronGroup.jambotron.exceptionHandlers; +import com.jambotronGroup.jambotron.controllers.AuthController; import com.jambotronGroup.jambotron.fileUpload.ResponseMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -8,11 +11,14 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; -//@ControllerAdvice -//public class FileUploadExceptionHandler extends ResponseEntityExceptionHandler { -// -// @ExceptionHandler(MaxUploadSizeExceededException.class) -// public ResponseEntity handleMaxSizeException(MaxUploadSizeExceededException exc) { -// return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage("File too large!")); -// } -//} \ No newline at end of file +/* +@ControllerAdvice +public class FileUploadExceptionHandler extends ResponseEntityExceptionHandler { + + private static final Logger _logger = LoggerFactory.getLogger(AuthController.class); + @ExceptionHandler(MaxUploadSizeExceededException.class) + public ResponseEntity handleMaxSizeException(MaxUploadSizeExceededException exc) { + _logger.error("File upload size exceeded: {}", exc.getMessage()); + return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage("File too large!")); + } +}*/ diff --git a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FileUploadExceptionAdvice.java b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FileUploadExceptionAdvice.java deleted file mode 100644 index 080beb2..0000000 --- a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FileUploadExceptionAdvice.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.jambotronGroup.jambotron.fileUpload; - - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.multipart.MaxUploadSizeExceededException; -import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; - -@ControllerAdvice -public class FileUploadExceptionAdvice extends ResponseEntityExceptionHandler { - - -// @ExceptionHandler(MaxUploadSizeExceededException.class) -// public ResponseEntity handleMaxSizeException(MaxUploadSizeExceededException exc) { -// return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage("File too large!")); -// } -} \ No newline at end of file diff --git a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageService.java b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageService.java index b56dd4e..33b7354 100644 --- a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageService.java +++ b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageService.java @@ -10,6 +10,8 @@ import java.util.stream.Stream; public interface FilesStorageService { public void init(); + public String getFileNameFromUrl(String urlString); + public String save(String userID,MultipartFile file); public void save(MultipartFile file); diff --git a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageServiceImpl.java b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageServiceImpl.java index b572440..900c2b9 100644 --- a/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageServiceImpl.java +++ b/src/main/java/com/jambotronGroup/jambotron/fileUpload/FilesStorageServiceImpl.java @@ -1,6 +1,9 @@ package com.jambotronGroup.jambotron.fileUpload; +import com.jambotronGroup.jambotron.controllers.AuthController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.stereotype.Service; @@ -19,9 +22,8 @@ import java.util.stream.Stream; @Service public class FilesStorageServiceImpl implements FilesStorageService { -// private final Path root = Paths.get("uploads/user-images/"); -// -// private final Path rootPublic = Paths.get("uploads/public-images/"); + private static final Logger _logger = LoggerFactory.getLogger(AuthController.class); + // Update paths to use the Docker volume private final Path root = Paths.get("/jambotron_data/uploads/user-images/"); @@ -38,10 +40,18 @@ public class FilesStorageServiceImpl implements FilesStorageService { } } - public static String getFileNameFromUrl(String urlString) throws Exception { - URL url = new URL(urlString); // Create a URL object - String path = url.getPath(); // Get the path from the URL - return path.substring(path.lastIndexOf('/') + 1); // Extract the file name + @Override + public String getFileNameFromUrl(String urlString){ + try { + + URL url = new URL(urlString); // Create a URL object + String path = url.getPath(); // Get the path from the URL + return path.substring(path.lastIndexOf('/') + 1); // Extract the file name + + }catch (MalformedURLException e) { + _logger.error("Invalid URL: " + urlString, e); + throw new RuntimeException("Invalid URL: " + urlString); + } } /** @@ -52,12 +62,11 @@ public class FilesStorageServiceImpl implements FilesStorageService { * @param url The URL of the file to move. * @param newFilename The new name for the file in the public directory. * @return The path to the moved file in the public directory. - * @throws Exception If the file cannot be moved. */ @Override - public Path moveFile(String userID, String url, String newFilename) throws Exception { + public Path moveFile(String userID, String url, String newFilename) { - String filename = FilesStorageServiceImpl.getFileNameFromUrl(url); + String filename = this.getFileNameFromUrl(url); try { Path sourcePath = this.root.resolve(userID).resolve(filename);