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 {
String fileName = System.currentTimeMillis() + "";
news.setImageFileName(fileName);
if (!news.getImageFile().isEmpty()) {
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)) {
newsRepository.save(news);
} else {
create(news);
}
FileUtil.saveFile(FileSystemStorageService.UPLOAD_DIR, fileName, news.getImageFile());
}
public News getById(@NotNull Integer id) {

View File

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