personalweb

archived
git clone git://git.wimdupont.com/personalweb.git
Log | Files | Refs | LICENSE

commit 8680baa7fbd65793bf15ec7e8aa4a50a972041be
parent 0a9024ef808890067bf6a98cc9141cacc5e908ff
Author: WimDupont <WimDupont@users.noreply.gitlab.com>
Date:   Fri, 20 Aug 2021 20:18:57 +0200

cleanup

Diffstat:
Msrc/main/java/com/wimdupont/personalweb/controller/BlogController.java | 31++++++++++++++++++++++++-------
Dsrc/main/java/com/wimdupont/personalweb/model/dao/rss/Channel.java | 30------------------------------
Dsrc/main/java/com/wimdupont/personalweb/model/dao/rss/Item.java | 31-------------------------------
Msrc/main/java/com/wimdupont/personalweb/service/BlogArticleGenerator.java | 15+++++++++------
Msrc/main/java/com/wimdupont/personalweb/service/RssFeedGenerator.java | 34++++++++++++++++++----------------
5 files changed, 51 insertions(+), 90 deletions(-)

diff --git a/src/main/java/com/wimdupont/personalweb/controller/BlogController.java b/src/main/java/com/wimdupont/personalweb/controller/BlogController.java @@ -1,6 +1,9 @@ package com.wimdupont.personalweb.controller; +import com.wimdupont.personalweb.service.BlogArticleGenerator; import com.wimdupont.personalweb.util.Constants; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -16,28 +19,42 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; +@Slf4j @Controller @RequestMapping("/blog") +@RequiredArgsConstructor public class BlogController { + private final BlogArticleGenerator blogArticleGenerator; + @SuppressWarnings("unused") @GetMapping - public String getBlog(Model model) { + public String getBlog(Model model) throws IOException { + blogArticleGenerator.generate(); List<String> articles = new ArrayList<>(); File[] files = new File(String.format("%s/html", Constants.ARTICLES_DIRECTORY)).listFiles(); - Arrays.sort(files, Comparator.comparingLong(File::lastModified)); - for (File htmlArticle : files) { - articles.add(htmlArticle.getName().replaceFirst(".html", "")); + if (files != null) { + Arrays.sort(files, Comparator.comparingLong(File::lastModified)); + for (File htmlArticle : files) { + articles.add(htmlArticle.getName().replaceFirst(".html", "")); + } + model.addAttribute("articles", articles); } - model.addAttribute("articles", articles); return "blog"; } @SuppressWarnings("unused") @GetMapping(value = "/article/{name}") - public String getBlogArticle(@PathVariable(value = "name") String name, Model model) throws IOException { - model.addAttribute("article", Files.readString(Paths.get(String.format("%s/html/%s%s", Constants.ARTICLES_DIRECTORY, name, ".html")))); + public String getBlogArticle(@PathVariable(value = "name") String name, Model model) { + String article; + try { + article = Files.readString(Paths.get(String.format("%s/html/%s%s", Constants.ARTICLES_DIRECTORY, name, ".html"))); + } catch (IOException e) { + article = String.format("Article with name \"%s\" not found.", name); + log.warn(e.getMessage(), e); + } + model.addAttribute("article", article); model.addAttribute("title", name); return "article"; } diff --git a/src/main/java/com/wimdupont/personalweb/model/dao/rss/Channel.java b/src/main/java/com/wimdupont/personalweb/model/dao/rss/Channel.java @@ -1,30 +0,0 @@ -//package com.sxcy.sxcyweb.model.dao.rss; -// -//import lombok.AllArgsConstructor; -//import lombok.Builder; -//import lombok.Data; -//import lombok.NoArgsConstructor; -//import org.hibernate.annotations.GenericGenerator; -// -//import javax.persistence.Entity; -//import javax.persistence.GeneratedValue; -//import javax.persistence.Id; -// -//@Entity -//@Builder(toBuilder = true) -//@Data -//@NoArgsConstructor -//@AllArgsConstructor -////TODO -//public class Channel { -// -// @Id -// @GeneratedValue(generator = "uuid") -// @GenericGenerator(name="uuid", strategy = "uuid") -// private String guid; -// -// private String title; -// private String link; -// private String description; -// private String author; -//} diff --git a/src/main/java/com/wimdupont/personalweb/model/dao/rss/Item.java b/src/main/java/com/wimdupont/personalweb/model/dao/rss/Item.java @@ -1,31 +0,0 @@ -//package com.sxcy.sxcyweb.model.dao.rss; -// -//import lombok.AllArgsConstructor; -//import lombok.Builder; -//import lombok.Data; -//import lombok.NoArgsConstructor; -//import org.hibernate.annotations.GenericGenerator; -// -//import javax.persistence.Entity; -//import javax.persistence.GeneratedValue; -//import javax.persistence.Id; -//import java.time.LocalDateTime; -// -//@Entity -//@Builder(toBuilder = true) -//@Data -//@NoArgsConstructor -//@AllArgsConstructor -////TODO -//public class Item { -// -// @Id -// @GeneratedValue(generator = "uuid") -// @GenericGenerator(name="uuid", strategy = "uuid") -// private String guid; -// -// private String title; -// private String link; -// private LocalDateTime pubDate; -// private String description; -//} diff --git a/src/main/java/com/wimdupont/personalweb/service/BlogArticleGenerator.java b/src/main/java/com/wimdupont/personalweb/service/BlogArticleGenerator.java @@ -21,12 +21,15 @@ public class BlogArticleGenerator { @Scheduled(cron = "0 0 0 * * *") public void generate() throws IOException { - for (File article : new File(Constants.ARTICLES_DIRECTORY).listFiles()) { - if (article.isFile() && article.getName().endsWith(".adoc") && !new File(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replace(".adoc",".html"))).exists()) { - String htmlArticle = adocConverter.convert(article); - new File(String.format("%s/html/", Constants.ARTICLES_DIRECTORY)).mkdir(); - new File(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replaceFirst(".adoc", ".html"))).createNewFile(); - Files.write(Paths.get(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replaceFirst(".adoc", ".html"))), htmlArticle.getBytes(StandardCharsets.UTF_8)); + File[] files = new File(Constants.ARTICLES_DIRECTORY).listFiles(); + if (files != null) { + for (File article : files) { + if (article.isFile() && article.getName().endsWith(".adoc") && !new File(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replace(".adoc", ".html"))).exists()) { + String htmlArticle = adocConverter.convert(article); + new File(String.format("%s/html/", Constants.ARTICLES_DIRECTORY)).mkdir(); + new File(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replaceFirst(".adoc", ".html"))).createNewFile(); + Files.write(Paths.get(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replaceFirst(".adoc", ".html"))), htmlArticle.getBytes(StandardCharsets.UTF_8)); + } } } } diff --git a/src/main/java/com/wimdupont/personalweb/service/RssFeedGenerator.java b/src/main/java/com/wimdupont/personalweb/service/RssFeedGenerator.java @@ -43,22 +43,24 @@ public class RssFeedGenerator { List<Item> items = new ArrayList<>(); File[] files = new File(String.format("%s/html", Constants.ARTICLES_DIRECTORY)).listFiles(); - Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed()); - for (File article : files) { - String articleString = null; - try { - articleString = Files.readString(Paths.get(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName()))); - } catch (IOException e) { - e.printStackTrace(); - } + if (files != null) { + Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed()); + for (File article : files) { + String articleString = null; + try { + articleString = Files.readString(Paths.get(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName()))); + } catch (IOException e) { + e.printStackTrace(); + } - String title = article.getName().replace(".html", ""); - Item item = genericItem(new Date(article.lastModified()), title); - item.setTitle(title); - Content content = new Content(); - content.setValue(articleString); - item.setContent(content); - items.add(item); + String title = article.getName().replace(".html", ""); + Item item = genericItem(new Date(article.lastModified()), title); + item.setTitle(title); + Content content = new Content(); + content.setValue(articleString); + item.setContent(content); + items.add(item); + } } return items; } @@ -71,7 +73,7 @@ public class RssFeedGenerator { item.setTitle(title); item.setUri(url); Guid guid = new Guid(); - guid.setValue(title + "-" +postDate.getTime()); + guid.setValue(title + "-" + postDate.getTime()); item.setGuid(guid); item.setPubDate(postDate); return item;