diff --git a/src/main/java/run/halo/app/controller/content/ContentContentController.java b/src/main/java/run/halo/app/controller/content/ContentContentController.java index eb633c4118..6e96e9e5ea 100644 --- a/src/main/java/run/halo/app/controller/content/ContentContentController.java +++ b/src/main/java/run/halo/app/controller/content/ContentContentController.java @@ -109,13 +109,17 @@ public String content(@PathVariable("prefix") String prefix, Model model) { if (optionService.getArchivesPrefix().equals(prefix)) { return postModel.archives(page, model); - } else if (optionService.getJournalsPrefix().equals(prefix)) { + } + + if (optionService.getJournalsPrefix().equals(prefix)) { return journalModel.list(page, model); - } else if (optionService.getPhotosPrefix().equals(prefix)) { + } + + if (optionService.getPhotosPrefix().equals(prefix)) { return photoModel.list(page, model); - } else { - throw new NotFoundException("Not Found"); } + + throw new NotFoundException("Not Found"); } @GetMapping("{prefix}/{slug}") @@ -124,23 +128,36 @@ public String content(@PathVariable("prefix") String prefix, @RequestParam(value = "token", required = false) String token, Model model) { PostPermalinkType postPermalinkType = optionService.getPostPermalinkType(); + if (optionService.getArchivesPrefix().equals(prefix)) { + if (postPermalinkType.equals(PostPermalinkType.DEFAULT)) { + Post post = postService.getBySlug(slug); + return postModel.content(post, token, model); + } + if (postPermalinkType.equals(PostPermalinkType.ID_SLUG) && StringUtils.isNumeric(slug)) { + Post post = postService.getById(Integer.parseInt(slug)); + return postModel.content(post, token, model); + } + } - if (postPermalinkType.equals(PostPermalinkType.DEFAULT) && optionService.getArchivesPrefix().equals(prefix)) { - Post post = postService.getBySlug(slug); - return postModel.content(post, token, model); - } else if (postPermalinkType.equals(PostPermalinkType.YEAR) && prefix.length() == 4 && StringUtils.isNumeric(prefix)) { - Post post = postService.getBy(Integer.parseInt(prefix), slug); - return postModel.content(post, token, model); - } else if (optionService.getSheetPrefix().equals(prefix)) { + if (optionService.getSheetPrefix().equals(prefix)) { Sheet sheet = sheetService.getBySlug(slug); return sheetModel.content(sheet, token, model); - } else if (optionService.getCategoriesPrefix().equals(prefix)) { + } + + if (optionService.getCategoriesPrefix().equals(prefix)) { return categoryModel.listPost(model, slug, 1); - } else if (optionService.getTagsPrefix().equals(prefix)) { + } + + if (optionService.getTagsPrefix().equals(prefix)) { return tagModel.listPost(model, slug, 1); - } else { - throw new NotFoundException("Not Found"); } + + if (postPermalinkType.equals(PostPermalinkType.YEAR) && prefix.length() == 4 && StringUtils.isNumeric(prefix)) { + Post post = postService.getBy(Integer.parseInt(prefix), slug); + return postModel.content(post, token, model); + } + + throw new NotFoundException("Not Found"); } @GetMapping("{prefix}/{slug}/page/{page:\\d+}") @@ -150,11 +167,13 @@ public String content(@PathVariable("prefix") String prefix, Model model) { if (optionService.getCategoriesPrefix().equals(prefix)) { return categoryModel.listPost(model, slug, page); - } else if (optionService.getTagsPrefix().equals(prefix)) { + } + + if (optionService.getTagsPrefix().equals(prefix)) { return tagModel.listPost(model, slug, page); - } else { - throw new NotFoundException("Not Found"); } + + throw new NotFoundException("Not Found"); } @GetMapping("{year:\\d+}/{month:\\d+}/{slug}") @@ -167,9 +186,9 @@ public String content(@PathVariable("year") Integer year, if (postPermalinkType.equals(PostPermalinkType.DATE)) { Post post = postService.getBy(year, month, slug); return postModel.content(post, token, model); - } else { - throw new NotFoundException("Not Found"); } + + throw new NotFoundException("Not Found"); } @GetMapping("{year:\\d+}/{month:\\d+}/{day:\\d+}/{slug}") @@ -183,9 +202,9 @@ public String content(@PathVariable("year") Integer year, if (postPermalinkType.equals(PostPermalinkType.DAY)) { Post post = postService.getBy(year, month, day, slug); return postModel.content(post, token, model); - } else { - throw new NotFoundException("Not Found"); } + + throw new NotFoundException("Not Found"); } @PostMapping(value = "archives/{slug:.*}/password") diff --git a/src/main/java/run/halo/app/model/enums/PostPermalinkType.java b/src/main/java/run/halo/app/model/enums/PostPermalinkType.java index fd2dacab6e..e1f331c30f 100644 --- a/src/main/java/run/halo/app/model/enums/PostPermalinkType.java +++ b/src/main/java/run/halo/app/model/enums/PostPermalinkType.java @@ -31,7 +31,12 @@ public enum PostPermalinkType implements ValueEnum { /** * /1970/${slug} */ - YEAR(4); + YEAR(4), + + /** + * archives/${id} + */ + ID_SLUG(5); private final Integer value; diff --git a/src/main/java/run/halo/app/service/impl/PostCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostCommentServiceImpl.java index 08d99352f7..b4539df2e4 100644 --- a/src/main/java/run/halo/app/service/impl/PostCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostCommentServiceImpl.java @@ -158,6 +158,11 @@ private BasePostMinimalDTO buildPostFullPath(BasePostMinimalDTO post) { .append(URL_SEPARATOR) .append(post.getSlug()) .append(pathSuffix); + } else if (permalinkType.equals(PostPermalinkType.ID_SLUG)) { + fullPath.append(archivesPrefix) + .append(URL_SEPARATOR) + .append(post.getId()) + .append(pathSuffix); } post.setFullPath(fullPath.toString()); diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index f2643ee2f1..68f9350f96 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -872,6 +872,11 @@ private String buildFullPath(Post post) { .append(URL_SEPARATOR) .append(post.getSlug()) .append(pathSuffix); + } else if (permalinkType.equals(PostPermalinkType.ID_SLUG)) { + fullPath.append(archivesPrefix) + .append(URL_SEPARATOR) + .append(post.getId()) + .append(pathSuffix); } return fullPath.toString(); }