fix image upload

This commit is contained in:
Anton Romanov 2022-03-18 12:14:55 +04:00
parent 0f500266bc
commit beb5fd2e15
3 changed files with 7 additions and 3 deletions

Binary file not shown.

View File

@ -39,15 +39,18 @@ public class NewsService {
public void save(News news) throws IOException { public void save(News news) throws IOException {
String fileName = System.currentTimeMillis() + ""; String fileName = System.currentTimeMillis() + "";
if (!news.getImageFile().isEmpty()) {
news.setImageFileName(fileName); news.setImageFileName(fileName);
FileUtil.saveFile(FileSystemStorageService.UPLOAD_DIR, fileName, news.getImageFile());
} else {
news.setImageFileName(news.getImageFileName().isEmpty() ? "logo.png" : news.getImageFileName());
}
if (news.getId() != null && (news.getId() != 0)) { if (news.getId() != null && (news.getId() != 0)) {
newsRepository.save(news); newsRepository.save(news);
} else { } else {
create(news); create(news);
} }
FileUtil.saveFile(FileSystemStorageService.UPLOAD_DIR, fileName, news.getImageFile());
} }
public News getById(@NotNull Integer id) { public News getById(@NotNull Integer id) {

View File

@ -14,6 +14,7 @@
<input type="hidden" th:field="*{id}"> <input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{date}"> <input type="hidden" th:field="*{date}">
<input type="hidden" th:field="*{version}"> <input type="hidden" th:field="*{version}">
<input type="hidden" th:field="*{imageFileName}">
<div class="form-group"> <div class="form-group">
<label for="title">Заголовок</label> <label for="title">Заголовок</label>
<input type="text" class="form-control" id="title" th:field="*{title}" placeholder="Заголовок новости"> <input type="text" class="form-control" id="title" th:field="*{title}" placeholder="Заголовок новости">