Add file upload functionality with dialog components and update image handling
This commit is contained in:
@@ -5,15 +5,9 @@ import com.jambotronGroup.jambotron.fileUpload.FilesStorageService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.Marker;
|
||||
import org.slf4j.event.Level;
|
||||
import org.slf4j.helpers.BasicMarker;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@SpringBootApplication
|
||||
public class JambotronApplication implements CommandLineRunner {
|
||||
@@ -35,4 +29,5 @@ public class JambotronApplication implements CommandLineRunner {
|
||||
// storageService.deleteAll();
|
||||
storageService.init();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.jambotronGroup.jambotron.configuretions;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
/*
|
||||
@Configuration
|
||||
|
||||
@EnableWebMvc
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
@Value("${spring.resources.static-locations}")
|
||||
String resourceLocations;
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
//registry.addResourceHandler("/public/**").addResourceLocations(resourceLocations);
|
||||
registry.addResourceHandler("/**").addResourceLocations("classpath:/resources/");
|
||||
|
||||
//registry.
|
||||
registry.addResourceHandler("/media/**").addResourceLocations("resources/main/public/media/");
|
||||
|
||||
}
|
||||
}*/
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.jambotronGroup.jambotron.configuretions;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
/*
|
||||
@ControllerAdvice
|
||||
public class NotFoundHandler {
|
||||
@Value("${spa.default-file}")
|
||||
String defaultFile;
|
||||
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
public ResponseEntity<String> renderDefaultPage() {
|
||||
try {
|
||||
File indexFile = ResourceUtils.getFile(defaultFile);
|
||||
FileInputStream inputStream = new FileInputStream(indexFile);
|
||||
String body = StreamUtils.copyToString(inputStream, Charset.defaultCharset());
|
||||
return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(body);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There was an error completing the action.");
|
||||
}
|
||||
}
|
||||
}*/
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.jambotronGroup.jambotron.controllers;
|
||||
|
||||
import com.jambotronGroup.jambotron.fileUpload.FilesStorageService;
|
||||
import com.jambotronGroup.jambotron.fileUpload.ResponseImageUploadResult;
|
||||
import com.jambotronGroup.jambotron.fileUpload.ResponseMessage;
|
||||
import com.jambotronGroup.jambotron.model.FileInfo;
|
||||
import com.jambotronGroup.jambotron.model.User;
|
||||
@@ -30,17 +31,32 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
//@CrossOrigin("http://localhost:8081")
|
||||
public class FilesController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FilesController.class);
|
||||
|
||||
|
||||
@Autowired
|
||||
AuthenticationFacade authenticationFacade;
|
||||
@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) {
|
||||
|
||||
@@ -52,7 +68,6 @@ public class FilesController {
|
||||
|
||||
String message = "";
|
||||
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
try {
|
||||
@@ -92,14 +107,21 @@ public class FilesController {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
|
||||
}
|
||||
|
||||
@PostMapping("/api/file/upload")
|
||||
@PostMapping("/api/user/upload")
|
||||
public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
String message = "";
|
||||
try {
|
||||
storageService.save(file);
|
||||
User user = authenticationFacade.getUser();
|
||||
|
||||
String localFullFileName = storageService.save(user.getId().toString(), file);
|
||||
|
||||
FileInfo fileInfo = new FileInfo(
|
||||
file.getOriginalFilename(),
|
||||
FilesController.getUserImageUrl(localFullFileName));
|
||||
|
||||
|
||||
message = "Uploaded the file successfully: " + file.getOriginalFilename();
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseImageUploadResult(message,fileInfo));
|
||||
} catch (Exception e) {
|
||||
message = "Could not upload the file: " + file.getOriginalFilename() + ". Error: " + e.getMessage();
|
||||
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
|
||||
@@ -113,8 +135,8 @@ public class FilesController {
|
||||
|
||||
List<FileInfo> fileInfos = storageService.loadUserImages(user.getId().toString()).map(path -> {
|
||||
String filename = path.getFileName().toString();
|
||||
String url = MvcUriComponentsBuilder
|
||||
.fromMethodName(FilesController.class, "getUserImage", path.getFileName().toString()).build().toString();
|
||||
|
||||
String url = FilesController.getUserImageUrl(filename);
|
||||
|
||||
return new FileInfo(filename, url);
|
||||
}).collect(Collectors.toList());
|
||||
@@ -127,9 +149,7 @@ public class FilesController {
|
||||
public ResponseEntity<List<FileInfo>> getListFiles() {
|
||||
List<FileInfo> fileInfos = storageService.loadAll().map(path -> {
|
||||
String filename = path.getFileName().toString();
|
||||
String url = MvcUriComponentsBuilder
|
||||
.fromMethodName(FilesController.class, "getFile", path.getFileName().toString()).build().toString();
|
||||
|
||||
String url = FilesController.getPublicImageUrl(filename);
|
||||
return new FileInfo(filename, url);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.jambotronGroup.jambotron.exceptionHandlers;
|
||||
|
||||
import com.jambotronGroup.jambotron.fileUpload.ResponseMessage;
|
||||
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 FileUploadExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
//
|
||||
// @ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
// public ResponseEntity<ResponseMessage> handleMaxSizeException(MaxUploadSizeExceededException exc) {
|
||||
// return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage("File too large!"));
|
||||
// }
|
||||
//}
|
||||
@@ -10,7 +10,7 @@ import java.util.stream.Stream;
|
||||
public interface FilesStorageService {
|
||||
public void init();
|
||||
|
||||
public void save(String userID,MultipartFile file);
|
||||
public String save(String userID,MultipartFile file);
|
||||
|
||||
public void save(MultipartFile file);
|
||||
|
||||
|
||||
@@ -56,14 +56,25 @@ public class FilesStorageServiceImpl implements FilesStorageService {
|
||||
return this.rootPublic.resolve(newFilename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a file to a user's directory.
|
||||
* uploads/user-images/{userID}/{filename}
|
||||
*
|
||||
* @param userID The ID of the user.
|
||||
* @param file The file to save.
|
||||
*/
|
||||
@Override
|
||||
public void save(String userID,MultipartFile file) {
|
||||
public String save(String userID,MultipartFile file) {
|
||||
Path targetPath = null;
|
||||
try {
|
||||
Path path = this.root.resolve(userID);
|
||||
path.toFile().mkdirs(); // Ensure user directory exists
|
||||
|
||||
Files.copy(file.getInputStream(), path.resolve(file.getOriginalFilename()),
|
||||
targetPath = path.resolve(file.getOriginalFilename());
|
||||
|
||||
Files.copy(file.getInputStream(), targetPath,
|
||||
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
} catch (Exception e) {
|
||||
if (e instanceof FileAlreadyExistsException) {
|
||||
throw new RuntimeException("A file of that name already exists.");
|
||||
@@ -71,6 +82,8 @@ public class FilesStorageServiceImpl implements FilesStorageService {
|
||||
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
|
||||
return targetPath.getFileName().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.jambotronGroup.jambotron.fileUpload;
|
||||
|
||||
import com.jambotronGroup.jambotron.model.FileInfo;
|
||||
|
||||
public class ResponseImageUploadResult extends ResponseMessage {
|
||||
private FileInfo fileInfo;
|
||||
|
||||
|
||||
public ResponseImageUploadResult(String message, FileInfo fileInfo) {
|
||||
super(message);
|
||||
this.fileInfo = fileInfo;
|
||||
}
|
||||
|
||||
public FileInfo getFileInfo() {
|
||||
return this.fileInfo;
|
||||
}
|
||||
|
||||
public void setFileInfo(FileInfo fileInfo) {
|
||||
this.fileInfo = fileInfo;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package com.jambotronGroup.jambotron.fileUpload;
|
||||
public class ResponseMessage {
|
||||
private String message;
|
||||
|
||||
|
||||
|
||||
public ResponseMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@@ -104,6 +104,9 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
|
||||
|
||||
.requestMatchers("/tutorials-images/**").permitAll()
|
||||
|
||||
.requestMatchers("/api/file/files").permitAll()
|
||||
.requestMatchers("/api/file/upload").permitAll()
|
||||
|
||||
.requestMatchers("/files/**").permitAll()
|
||||
|
||||
.requestMatchers("/api/auth/**").permitAll()
|
||||
|
||||
Reference in New Issue
Block a user