feat: support /year/month/day/url and /year/month/url mapping.

pull/489/head
ruibaby 2020-01-11 12:29:54 +08:00
parent 76dd2b2c72
commit 62153403df
4 changed files with 54 additions and 7 deletions

View File

@ -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());

View File

@ -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");
}
}
}

View File

@ -69,7 +69,7 @@ public interface PostRepository extends BasePostRepository<Post>, 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<Post> 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<Post>, 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<Post> findBy(@Param("year") Integer year, @Param("month") Integer month, @Param("day") Integer day, @Param("url") String url, @Param("status") PostStatus status);
}

View File

@ -536,16 +536,32 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> 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());