diff --git a/data/db.mv.db b/data/db.mv.db index c0b9794..49a6a54 100644 Binary files a/data/db.mv.db and b/data/db.mv.db differ diff --git a/src/main/java/ru/ulstu/controller/NewsController.java b/src/main/java/ru/ulstu/controller/NewsController.java index 34e8ba3..a2f2859 100644 --- a/src/main/java/ru/ulstu/controller/NewsController.java +++ b/src/main/java/ru/ulstu/controller/NewsController.java @@ -7,6 +7,7 @@ package ru.ulstu.controller; import org.springframework.data.domain.Page; +import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; @@ -18,9 +19,11 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.model.News; import ru.ulstu.model.OffsetablePageRequest; +import ru.ulstu.model.UserRoleConstants; import ru.ulstu.service.NewsService; import javax.validation.Valid; +import java.io.IOException; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -68,12 +71,13 @@ public class NewsController { } @PostMapping("saveNews") - public String saveNews(@Valid @ModelAttribute News news, BindingResult result) { + @Secured({UserRoleConstants.ADMIN}) + public String saveNews(@Valid @ModelAttribute News news, + BindingResult result) throws IOException { if (result.hasErrors()) { return "editNews"; } newsService.save(news); - return "redirect:/news/news"; } diff --git a/src/main/java/ru/ulstu/model/News.java b/src/main/java/ru/ulstu/model/News.java index f4dd2c8..f6ab364 100644 --- a/src/main/java/ru/ulstu/model/News.java +++ b/src/main/java/ru/ulstu/model/News.java @@ -1,9 +1,11 @@ package ru.ulstu.model; import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.web.multipart.MultipartFile; import javax.persistence.Entity; import javax.persistence.Lob; +import javax.persistence.Transient; import javax.validation.constraints.NotEmpty; import java.util.Date; @@ -21,6 +23,11 @@ public class News extends BaseEntity { @NotEmpty(message = "Текст новости не может быть пустым") private String text; + private String imageFileName; + + @Transient + private MultipartFile imageFile; + public News() { } @@ -54,6 +61,22 @@ public class News extends BaseEntity { return text; } + public String getImageFileName() { + return imageFileName; + } + + public void setImageFileName(String imageFileName) { + this.imageFileName = imageFileName; + } + + public MultipartFile getImageFile() { + return imageFile; + } + + public void setImageFile(MultipartFile imageFile) { + this.imageFile = imageFile; + } + public String getPreview() { return text != null && text.length() > MAX_NEWS_TEXT_PREVIEW_LENGTH ? text.substring(0, MAX_NEWS_TEXT_PREVIEW_LENGTH) + "..." diff --git a/src/main/java/ru/ulstu/service/FileUtil.java b/src/main/java/ru/ulstu/service/FileUtil.java new file mode 100644 index 0000000..af9fb4a --- /dev/null +++ b/src/main/java/ru/ulstu/service/FileUtil.java @@ -0,0 +1,29 @@ +package ru.ulstu.service; + +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +public class FileUtil { + + public static void saveFile(String uploadDir, String fileName, + MultipartFile multipartFile) throws IOException { + Path uploadPath = Paths.get(uploadDir); + + if (!Files.exists(uploadPath)) { + Files.createDirectories(uploadPath); + } + + try (InputStream inputStream = multipartFile.getInputStream()) { + Path filePath = uploadPath.resolve(fileName); + Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException ioe) { + throw new IOException("Could not save image file: " + fileName, ioe); + } + } +} diff --git a/src/main/java/ru/ulstu/service/NewsService.java b/src/main/java/ru/ulstu/service/NewsService.java index f49dbdd..feac00a 100644 --- a/src/main/java/ru/ulstu/service/NewsService.java +++ b/src/main/java/ru/ulstu/service/NewsService.java @@ -7,6 +7,7 @@ import ru.ulstu.model.News; import ru.ulstu.repository.NewsRepository; import javax.validation.constraints.NotNull; +import java.io.IOException; import java.util.Date; import java.util.List; @@ -27,12 +28,19 @@ public class NewsService { newsRepository.save(news); } - public void save(News news) { + public void save(News news) throws IOException { + String fileName = System.currentTimeMillis() + ""; + news.setImageFileName(fileName); + if (news.getId() != null && (news.getId() != 0)) { newsRepository.save(news); } else { create(news); } + + String uploadDir = "news-photos/"; + FileUtil.saveFile(uploadDir, fileName, news.getImageFile()); + } public News getById(@NotNull Integer id) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 14058f2..89e854d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,11 +3,12 @@ # You may use, distribute and modify this code, please write to: romanov73@gmail.com. # # - spring.main.banner-mode=off logging.level.tech.athene=DEBUG server.port=8080 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false +spring.servlet.multipart.max-file-size=100000000 +spring.servlet.multipart.max-request-size=100000000 # go to http://localhost:8080/h2-console spring.datasource.url=jdbc:h2:file:./data/db spring.datasource.driverClassName=org.h2.Driver diff --git a/src/main/resources/templates/editNews.html b/src/main/resources/templates/editNews.html index 71a8aad..d448036 100644 --- a/src/main/resources/templates/editNews.html +++ b/src/main/resources/templates/editNews.html @@ -10,7 +10,7 @@ layout:decorate="~{default}">
+ Ошибка
+