diff --git a/src/main/java/net/sytes/kashey/sweater/config/MvcConfig.java b/src/main/java/net/sytes/kashey/sweater/config/MvcConfig.java index cb1eb8f..50049af 100644 --- a/src/main/java/net/sytes/kashey/sweater/config/MvcConfig.java +++ b/src/main/java/net/sytes/kashey/sweater/config/MvcConfig.java @@ -1,13 +1,27 @@ package net.sytes.kashey.sweater.config; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MvcConfig implements WebMvcConfigurer { + + @Value("${upload.path}") + private String uploadPath; + public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/img/**") + .addResourceLocations("file://" + uploadPath + "/"); + registry.addResourceHandler("/static/**") + .addResourceLocations("classpath:/static/"); + } } \ No newline at end of file diff --git a/src/main/java/net/sytes/kashey/sweater/config/WebSecurityConfig.java b/src/main/java/net/sytes/kashey/sweater/config/WebSecurityConfig.java index d63cdc8..1ab2de3 100644 --- a/src/main/java/net/sytes/kashey/sweater/config/WebSecurityConfig.java +++ b/src/main/java/net/sytes/kashey/sweater/config/WebSecurityConfig.java @@ -17,7 +17,7 @@ public class WebSecurityConfig { public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests((requests) -> requests - .requestMatchers("/", "/registration").permitAll() + .requestMatchers("/", "/registration","/static/**").permitAll() .anyRequest().authenticated() ) .formLogin((form) -> form diff --git a/src/main/java/net/sytes/kashey/sweater/controller/MainController.java b/src/main/java/net/sytes/kashey/sweater/controller/MainController.java index b2d3745..7f43371 100644 --- a/src/main/java/net/sytes/kashey/sweater/controller/MainController.java +++ b/src/main/java/net/sytes/kashey/sweater/controller/MainController.java @@ -1,27 +1,32 @@ package net.sytes.kashey.sweater.controller; import net.sytes.kashey.sweater.domain.Message; -import net.sytes.kashey.sweater.domain.User; import net.sytes.kashey.sweater.repository.MessageRepository; -import net.sytes.kashey.sweater.repository.UserRepository; import net.sytes.kashey.sweater.security.CustomUserDetails; +import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; +import java.util.Objects; +import java.util.UUID; @Controller public class MainController { private final MessageRepository messageRepository; - private final UserRepository userRepository; - public MainController(MessageRepository messageRepository, UserRepository userRepository) { + @Value("${upload.path}") + private String uploadPath; + + public MainController(MessageRepository messageRepository) { this.messageRepository = messageRepository; - this.userRepository = userRepository; } @GetMapping("/") @@ -42,18 +47,28 @@ public class MainController { } @PostMapping("/main") - public String addMessage(@AuthenticationPrincipal CustomUserDetails details, + public String addMessage(@AuthenticationPrincipal CustomUserDetails customUserDetails, @RequestParam String text, @RequestParam String tag, - Model model) { + @RequestParam("file") MultipartFile file, + Model model) throws IOException { - User author = userRepository.findByUsername(details.getUsername()) - .orElseThrow(() -> new UsernameNotFoundException("User not found: " + details.getUsername())); + Message newMessage = new Message(text, tag, customUserDetails.getUser()); - messageRepository.save(new Message(text, tag, author)); + if (file != null && !Objects.requireNonNull(file.getOriginalFilename()).isEmpty()) { + File uploadDir = new File(uploadPath); // Директория на сервере, куда помещаем пришедший файл + if (!uploadDir.exists()) { + uploadDir.mkdirs(); + } + String uuidFile = UUID.randomUUID().toString(); + String resultFilename = uuidFile + "." + file.getOriginalFilename(); // случайный уид + имя файла с клиента + file.transferTo(new File(uploadPath + "/" + resultFilename)); // помещаем переименованный файл с клиента в директорию сервера + newMessage.setFilename(resultFilename); + } + + messageRepository.save(newMessage); model.addAttribute("messages", messageRepository.findAll()); model.addAttribute("tag", ""); return "main"; } - } \ No newline at end of file diff --git a/src/main/java/net/sytes/kashey/sweater/domain/Message.java b/src/main/java/net/sytes/kashey/sweater/domain/Message.java index b7c2fcf..2bff318 100644 --- a/src/main/java/net/sytes/kashey/sweater/domain/Message.java +++ b/src/main/java/net/sytes/kashey/sweater/domain/Message.java @@ -10,6 +10,8 @@ public class Message { private String text; private String tag; + private String filename; + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "user_id") private User author; @@ -58,4 +60,12 @@ public class Message { public String getAuthorName() { return author == null ? "<none>" : author.getUsername(); } + + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } } diff --git a/src/main/java/net/sytes/kashey/sweater/security/CustomUserDetails.java b/src/main/java/net/sytes/kashey/sweater/security/CustomUserDetails.java index 52c59a5..4dcacca 100644 --- a/src/main/java/net/sytes/kashey/sweater/security/CustomUserDetails.java +++ b/src/main/java/net/sytes/kashey/sweater/security/CustomUserDetails.java @@ -52,4 +52,8 @@ public class CustomUserDetails implements UserDetails { public boolean isEnabled() { return user.isEnabled(); } + + public User getUser() { + return user; + } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 46cda95..0ad3c5c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,3 +5,5 @@ spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.freemarker.expose-request-attributes=true spring.freemarker.suffix=.ftl +upload.path=/home/kashey/soft/sweater/uploads + diff --git a/src/main/resources/static/style.css b/src/main/resources/static/style.css new file mode 100644 index 0000000..10fdddc --- /dev/null +++ b/src/main/resources/static/style.css @@ -0,0 +1,3 @@ +body { + color: #191970; +} \ No newline at end of file diff --git a/src/main/resources/templates/main.ftl b/src/main/resources/templates/main.ftl index 42b9786..1f1aa1c 100644 --- a/src/main/resources/templates/main.ftl +++ b/src/main/resources/templates/main.ftl @@ -7,9 +7,10 @@