Refactor image URL handling and enhance tutorial component layout
This commit is contained in:
@@ -8,6 +8,7 @@ import com.jambotronGroup.jambotron.model.User;
|
||||
import com.jambotronGroup.jambotron.payload.request.SaveZhipuAiImageRequest;
|
||||
import com.jambotronGroup.jambotron.security.AuthenticationFacade;
|
||||
import com.jambotronGroup.jambotron.system.SystemServiceImpl;
|
||||
import com.jambotronGroup.jambotron.utils.FilesRoutingHelper;
|
||||
import jakarta.validation.Valid;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -40,22 +41,6 @@ public class FilesController {
|
||||
@Autowired
|
||||
FilesStorageService storageService;
|
||||
|
||||
/**
|
||||
* Generates a URL for accessing a user's image.
|
||||
* format: /api/user/user-images/{filename:.+}
|
||||
*
|
||||
* @param filename The name of the file.
|
||||
* @return The URL to access the file.
|
||||
*/
|
||||
private static String getUserImageUrl(String filename) {
|
||||
return MvcUriComponentsBuilder
|
||||
.fromMethodName(FilesController.class, "getUserImage", filename).build().toString();
|
||||
}
|
||||
|
||||
private static String getPublicImageUrl(String filename) {
|
||||
return MvcUriComponentsBuilder
|
||||
.fromMethodName(FilesController.class, "getFile", filename).build().toString();
|
||||
}
|
||||
|
||||
@PostMapping("/api/user/saveZhipuAiImage")
|
||||
public ResponseEntity<ResponseMessage> saveZhipuAiImage(@Valid @RequestBody SaveZhipuAiImageRequest request) {
|
||||
@@ -117,8 +102,8 @@ public class FilesController {
|
||||
|
||||
FileInfo fileInfo = new FileInfo(
|
||||
file.getOriginalFilename(),
|
||||
FilesController.getUserImageUrl(localFullFileName));
|
||||
|
||||
FilesRoutingHelper.getUserImageUrl(localFullFileName));
|
||||
|
||||
|
||||
message = "Uploaded the file successfully: " + file.getOriginalFilename();
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseImageUploadResult(message,fileInfo));
|
||||
@@ -136,7 +121,7 @@ public class FilesController {
|
||||
List<FileInfo> fileInfos = storageService.loadUserImages(user.getId().toString()).map(path -> {
|
||||
String filename = path.getFileName().toString();
|
||||
|
||||
String url = FilesController.getUserImageUrl(filename);
|
||||
String url = FilesRoutingHelper.getUserImageUrl(filename);
|
||||
|
||||
return new FileInfo(filename, url);
|
||||
}).collect(Collectors.toList());
|
||||
@@ -149,7 +134,7 @@ public class FilesController {
|
||||
public ResponseEntity<List<FileInfo>> getListFiles() {
|
||||
List<FileInfo> fileInfos = storageService.loadAll().map(path -> {
|
||||
String filename = path.getFileName().toString();
|
||||
String url = FilesController.getPublicImageUrl(filename);
|
||||
String url = FilesRoutingHelper.getPublicImageUrl(filename);
|
||||
return new FileInfo(filename, url);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.jambotronGroup.jambotron.repository.TutorialRepository;
|
||||
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.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -118,8 +119,7 @@ public class TutorialController {
|
||||
String.format("Tutorial_%s",newFilename )
|
||||
);
|
||||
|
||||
String url = MvcUriComponentsBuilder
|
||||
.fromMethodName(FilesController.class, "getFile", path.getFileName().toString()).build().toString();
|
||||
String url = FilesRoutingHelper.getPublicImageUrl(path.getFileName().toString());
|
||||
|
||||
|
||||
try {
|
||||
@@ -160,7 +160,12 @@ public class TutorialController {
|
||||
}
|
||||
|
||||
@PutMapping("user/tutorial-update/{id}")
|
||||
public ResponseEntity<?> updateTutorial(@PathVariable("id") long id, @RequestBody Tutorial tutorial) {
|
||||
public ResponseEntity<?> updateTutorial(@PathVariable("id") long id, @RequestBody Tutorial tutorial){
|
||||
|
||||
User user = authenticationFacade.getUser();
|
||||
|
||||
|
||||
|
||||
Optional<Tutorial> tutorialData = tutorialRepository.findById(id);
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
if (tutorialData.isPresent()) {
|
||||
@@ -169,7 +174,22 @@ public class TutorialController {
|
||||
servTutorial.setDescription(tutorial.getDescription());
|
||||
servTutorial.setPublished(tutorial.isPublished());
|
||||
servTutorial.setTobepublished(tutorial.isTobepublished());
|
||||
|
||||
try {
|
||||
|
||||
String imageFileName = FilesStorageServiceImpl.getFileNameFromUrl(tutorial.getTitleimage());
|
||||
|
||||
String servImageFileName = FilesStorageServiceImpl.getFileNameFromUrl(servTutorial.getTitleimage());
|
||||
filesStorageService.deletePublicFile(servImageFileName);
|
||||
|
||||
Path path= filesStorageService.moveFile(
|
||||
user.getId().toString(),
|
||||
tutorial.getTitleimage(),
|
||||
String.format("Tutorial_%s",imageFileName )
|
||||
);
|
||||
|
||||
servTutorial.setTitleimage(FilesRoutingHelper.getPublicImageUrl(path.getFileName().toString()));
|
||||
|
||||
servTutorial = tutorialRepository.save(servTutorial);
|
||||
} catch (Exception e) {
|
||||
|
||||
|
||||
@@ -25,4 +25,6 @@ public interface FilesStorageService {
|
||||
public Stream<Path> loadUserImages(String userID);
|
||||
|
||||
public Path moveFile(String userID, String url, String newFilename) throws Exception;
|
||||
|
||||
public void deletePublicFile(String filename);
|
||||
}
|
||||
@@ -39,6 +39,16 @@ public class FilesStorageServiceImpl implements FilesStorageService {
|
||||
return path.substring(path.lastIndexOf('/') + 1); // Extract the file name
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves a file from a user's directory to the public directory with a new name.
|
||||
* StandardCopyOption.REPLACE_EXISTING
|
||||
*
|
||||
* @param userID The ID of the user owning the file.
|
||||
* @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 {
|
||||
|
||||
@@ -56,6 +66,16 @@ public class FilesStorageServiceImpl implements FilesStorageService {
|
||||
return this.rootPublic.resolve(newFilename);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deletePublicFile(String filename) {
|
||||
try {
|
||||
Path filePath = this.rootPublic.resolve(filename);
|
||||
Files.deleteIfExists(filePath);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Could not delete the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a file to a user's directory.
|
||||
* uploads/user-images/{userID}/{filename}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.jambotronGroup.jambotron.utils;
|
||||
|
||||
import com.jambotronGroup.jambotron.controllers.FilesController;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
|
||||
|
||||
@Component
|
||||
public class FilesRoutingHelper {
|
||||
/**
|
||||
* Generates a URL for accessing a user's image.
|
||||
* format: /api/user/user-images/{filename:.+}
|
||||
*
|
||||
* @param filename The name of the file.
|
||||
* @return The URL to access the file.
|
||||
*/
|
||||
public static String getUserImageUrl(String filename) {
|
||||
return MvcUriComponentsBuilder
|
||||
.fromMethodName(FilesController.class, "getUserImage", filename).build().toString();
|
||||
}
|
||||
|
||||
public static String getPublicImageUrl(String filename) {
|
||||
return MvcUriComponentsBuilder
|
||||
.fromMethodName(FilesController.class, "getFile", filename).build().toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user