diff --git a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java index f9b59fd0c..af06a9b1a 100644 --- a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java +++ b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java @@ -84,7 +84,6 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { String workDir = FILE_PROTOCOL + ensureSuffix(haloProperties.getWorkDir(), FILE_SEPARATOR); - String backupDir = FILE_PROTOCOL + ensureSuffix(haloProperties.getBackupDir(), FILE_SEPARATOR); registry.addResourceHandler("/**") .addResourceLocations(workDir + "templates/themes/") .addResourceLocations(workDir + "templates/admin/") @@ -109,6 +108,7 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { } } + @Override public void addFormatters(FormatterRegistry registry) { registry.addConverterFactory(new StringToEnumConverterFactory()); 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 7a035074c..0758e3364 100644 --- a/src/main/java/run/halo/app/controller/content/ContentContentController.java +++ b/src/main/java/run/halo/app/controller/content/ContentContentController.java @@ -68,4 +68,35 @@ public class ContentContentController { throw new NotFoundException("Not Found"); } } + + @GetMapping("{year:^[^A-Za-z]*$}/{month:^[^A-Za-z]*$}/{url}") + public String content(@PathVariable("year") Integer year, + @PathVariable("month") Integer month, + @PathVariable("url") String url, + @RequestParam(value = "token", required = false) String token, + Model model) { + PostPermalinkType postPermalinkType = optionService.getPostPermalinkType(); + if (postPermalinkType.equals(PostPermalinkType.DATE)) { + Post post = postService.getBy(year, month, url); + return postModel.content(post, token, model); + } else { + throw new NotFoundException("Not Found"); + } + } + + @GetMapping("{year:^[^A-Za-z]*$}/{month:^[^A-Za-z]*$}/{day:^[^A-Za-z]*$}/{url}") + public String content(@PathVariable("year") Integer year, + @PathVariable("month") Integer month, + @PathVariable("day") Integer day, + @PathVariable("url") String url, + @RequestParam(value = "token", required = false) String token, + Model model) { + PostPermalinkType postPermalinkType = optionService.getPostPermalinkType(); + if (postPermalinkType.equals(PostPermalinkType.DAY)) { + Post post = postService.getBy(year, month, day, url); + return postModel.content(post, token, model); + } else { + throw new NotFoundException("Not Found"); + } + } } diff --git a/src/main/java/run/halo/app/repository/PostRepository.java b/src/main/java/run/halo/app/repository/PostRepository.java index 76628a72b..1c522f6c0 100644 --- a/src/main/java/run/halo/app/repository/PostRepository.java +++ b/src/main/java/run/halo/app/repository/PostRepository.java @@ -69,7 +69,7 @@ public interface PostRepository extends BasePostRepository, JpaSpecificati * @param url post url * @return a optional of post */ - @Query("select post from Post post where DateUtil.year(post.createTime) = :year and DateUtil.month(post.createTime) = :month and DateUtil.day(post.createTime) = :day and post.url = :url") + @Query("select post from Post post where DateUtil.year(post.createTime) = :year and DateUtil.month(post.createTime) = :month and DateUtil.dayOfMonth(post.createTime) = :day and post.url = :url") Optional findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("day") Integer day, @Param("url") String url); /** @@ -82,6 +82,6 @@ public interface PostRepository extends BasePostRepository, JpaSpecificati * @param status post status * @return a optional of post */ - @Query("select post from Post post where DateUtil.year(post.createTime) = :year and DateUtil.month(post.createTime) = :month and DateUtil.day(post.createTime) = :day and post.url = :url and post.status = :status") + @Query("select post from Post post where DateUtil.year(post.createTime) = :year and DateUtil.month(post.createTime) = :month and DateUtil.dayOfMonth(post.createTime) = :day and post.url = :url and post.status = :status") Optional findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("day") Integer day, @Param("url") String url, @Param("status") PostStatus status); } 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 e9a74efa9..3483f9c9a 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -536,16 +536,32 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe // Set comment count postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L)); - StringBuilder fullPath = new StringBuilder(blogUrl); + StringBuilder fullPath = new StringBuilder(blogUrl) + .append("/"); if (permalinkType.equals(PostPermalinkType.DEFAULT)) { - fullPath.append("/") - .append(archivesPrefix) + fullPath.append(archivesPrefix) .append("/") .append(postListVO.getUrl()) .append(pathSuffix); } else if (permalinkType.equals(PostPermalinkType.ID)) { - fullPath.append("/?p=") + fullPath.append("?p=") .append(postListVO.getId()); + } else if (permalinkType.equals(PostPermalinkType.DATE)) { + fullPath.append(DateUtil.year(postListVO.getCreateTime())) + .append("/") + .append(DateUtil.month(postListVO.getCreateTime()) + 1) + .append("/") + .append(postListVO.getUrl()) + .append(pathSuffix); + } else if (permalinkType.equals(PostPermalinkType.DAY)) { + fullPath.append(DateUtil.year(postListVO.getCreateTime())) + .append("/") + .append(DateUtil.month(postListVO.getCreateTime()) + 1) + .append("/") + .append(DateUtil.dayOfMonth(postListVO.getCreateTime())) + .append("/") + .append(postListVO.getUrl()) + .append(pathSuffix); } postListVO.setFullPath(fullPath.toString());