|
@@ -1,22 +1,20 @@
|
|
|
package org.example.sweater.controller;
|
|
|
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
import org.example.sweater.domain.Message;
|
|
|
import org.example.sweater.domain.User;
|
|
|
import org.example.sweater.repos.MessagesRepository;
|
|
|
import org.example.sweater.service.FileService;
|
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
-import org.springframework.security.web.csrf.CsrfToken;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.ui.Model;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.ModelAttribute;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
@Controller
|
|
|
public class MainController {
|
|
@@ -28,12 +26,6 @@ public class MainController {
|
|
|
private final MessagesRepository messagesRepository;
|
|
|
private final FileService fileService;
|
|
|
|
|
|
- @ModelAttribute
|
|
|
- public void addCsrfToken(Model model, HttpServletRequest request) {
|
|
|
- CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
|
|
|
- model.addAttribute("_csrf", token);
|
|
|
- }
|
|
|
-
|
|
|
@GetMapping("/")
|
|
|
public String mainPage() {
|
|
|
return "main";
|
|
@@ -74,7 +66,7 @@ public class MainController {
|
|
|
) throws IOException {
|
|
|
Message createdMessage = new Message(text, tag, user);
|
|
|
|
|
|
- if (file != null) {
|
|
|
+ if (file != null && !Objects.requireNonNull(file.getOriginalFilename()).isEmpty()) {
|
|
|
String savedFileName = fileService.saveFile(file);
|
|
|
|
|
|
createdMessage.setFilename(savedFileName);
|