Implement file name extraction from URL and enhance logging in tutorial operations
This commit is contained in:
@@ -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";
|
||||
}
|
||||
}*/
|
||||
@@ -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<Tutorial> tutorialData = tutorialRepository.findById(id);
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
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<HttpStatus> 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);
|
||||
}
|
||||
}
|
||||
|
||||
+14
-8
@@ -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<ResponseMessage> handleMaxSizeException(MaxUploadSizeExceededException exc) {
|
||||
// return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage("File too large!"));
|
||||
// }
|
||||
//}
|
||||
/*
|
||||
@ControllerAdvice
|
||||
public class FileUploadExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
private static final Logger _logger = LoggerFactory.getLogger(AuthController.class);
|
||||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
public ResponseEntity<ResponseMessage> handleMaxSizeException(MaxUploadSizeExceededException exc) {
|
||||
_logger.error("File upload size exceeded: {}", exc.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage("File too large!"));
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -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<ResponseMessage> handleMaxSizeException(MaxUploadSizeExceededException exc) {
|
||||
// return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage("File too large!"));
|
||||
// }
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
+19
-10
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user