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 6e96e9e5ea..60e902d61e 100644 --- a/src/main/java/run/halo/app/controller/content/ContentContentController.java +++ b/src/main/java/run/halo/app/controller/content/ContentContentController.java @@ -15,6 +15,7 @@ import run.halo.app.model.entity.Sheet; import run.halo.app.model.enums.PostPermalinkType; import run.halo.app.model.enums.PostStatus; +import run.halo.app.model.enums.SheetPermalinkType; import run.halo.app.service.OptionService; import run.halo.app.service.PostService; import run.halo.app.service.SheetService; @@ -81,7 +82,12 @@ public ContentContentController(PostModel postModel, @GetMapping("{prefix}") public String content(@PathVariable("prefix") String prefix, + @RequestParam(value = "token", required = false) String token, Model model) { + if (optionService.getSheetPermalinkType().equals(SheetPermalinkType.ROOT)) { + Sheet sheet = sheetService.getBySlug(prefix); + return sheetModel.content(sheet, token, model); + } if (optionService.getArchivesPrefix().equals(prefix)) { return postModel.archives(1, model); } @@ -139,7 +145,7 @@ public String content(@PathVariable("prefix") String prefix, } } - if (optionService.getSheetPrefix().equals(prefix)) { + if (optionService.getSheetPermalinkType().equals(SheetPermalinkType.SECONDARY) && optionService.getSheetPrefix().equals(prefix)) { Sheet sheet = sheetService.getBySlug(slug); return sheetModel.content(sheet, token, model); } diff --git a/src/main/java/run/halo/app/model/enums/SheetPermalinkType.java b/src/main/java/run/halo/app/model/enums/SheetPermalinkType.java new file mode 100644 index 0000000000..17e3655eff --- /dev/null +++ b/src/main/java/run/halo/app/model/enums/SheetPermalinkType.java @@ -0,0 +1,31 @@ +package run.halo.app.model.enums; + +/** + * Sheet Permalink type enum. + * + * @author ryanwang + * @date 2020-12-01 + */ +public enum SheetPermalinkType implements ValueEnum { + + /** + * /{@link run.halo.app.model.properties.PermalinkProperties#SHEET_PREFIX}/${slug} + */ + SECONDARY(0), + + /** + * /${slug} + */ + ROOT(1); + + private final Integer value; + + SheetPermalinkType(Integer value) { + this.value = value; + } + + @Override + public Integer getValue() { + return value; + } +} diff --git a/src/main/java/run/halo/app/model/properties/PermalinkProperties.java b/src/main/java/run/halo/app/model/properties/PermalinkProperties.java index cd4ea482e1..8151280fb8 100644 --- a/src/main/java/run/halo/app/model/properties/PermalinkProperties.java +++ b/src/main/java/run/halo/app/model/properties/PermalinkProperties.java @@ -1,6 +1,7 @@ package run.halo.app.model.properties; import run.halo.app.model.enums.PostPermalinkType; +import run.halo.app.model.enums.SheetPermalinkType; /** * Permalink properties enum. @@ -15,6 +16,11 @@ public enum PermalinkProperties implements PropertyEnum { */ POST_PERMALINK_TYPE("post_permalink_type", PostPermalinkType.class, PostPermalinkType.DEFAULT.name()), + /** + * Sheet Permalink type. + */ + SHEET_PERMALINK_TYPE("sheet_permalink_type", SheetPermalinkType.class, SheetPermalinkType.SECONDARY.name()), + /** * Categories prefix * such as: /categories or /categories/${slug} diff --git a/src/main/java/run/halo/app/service/OptionService.java b/src/main/java/run/halo/app/service/OptionService.java index 8dd65f2c30..b4bfd86183 100755 --- a/src/main/java/run/halo/app/service/OptionService.java +++ b/src/main/java/run/halo/app/service/OptionService.java @@ -12,6 +12,7 @@ import run.halo.app.model.dto.OptionSimpleDTO; import run.halo.app.model.entity.Option; import run.halo.app.model.enums.PostPermalinkType; +import run.halo.app.model.enums.SheetPermalinkType; import run.halo.app.model.enums.ValueEnum; import run.halo.app.model.params.OptionParam; import run.halo.app.model.params.OptionQuery; @@ -392,6 +393,13 @@ public interface OptionService extends CrudService { */ PostPermalinkType getPostPermalinkType(); + /** + * Get sheet permalink type. + * + * @return SheetPermalinkType + */ + SheetPermalinkType getSheetPermalinkType(); + /** * Get sheet custom prefix. * diff --git a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java index d737573de6..a050a39241 100644 --- a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java @@ -23,6 +23,7 @@ import run.halo.app.model.dto.OptionSimpleDTO; import run.halo.app.model.entity.Option; import run.halo.app.model.enums.PostPermalinkType; +import run.halo.app.model.enums.SheetPermalinkType; import run.halo.app.model.enums.ValueEnum; import run.halo.app.model.params.OptionParam; import run.halo.app.model.params.OptionQuery; @@ -518,6 +519,11 @@ public PostPermalinkType getPostPermalinkType() { return getEnumByPropertyOrDefault(PermalinkProperties.POST_PERMALINK_TYPE, PostPermalinkType.class, PostPermalinkType.DEFAULT); } + @Override + public SheetPermalinkType getSheetPermalinkType() { + return getEnumByPropertyOrDefault(PermalinkProperties.SHEET_PERMALINK_TYPE, SheetPermalinkType.class, SheetPermalinkType.SECONDARY); + } + @Override public String getSheetPrefix() { return getByPropertyOrDefault(PermalinkProperties.SHEET_PREFIX, String.class, PermalinkProperties.SHEET_PREFIX.defaultValue()); diff --git a/src/main/java/run/halo/app/service/impl/SheetCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/SheetCommentServiceImpl.java index 89769ed871..89e38fe04f 100644 --- a/src/main/java/run/halo/app/service/impl/SheetCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/SheetCommentServiceImpl.java @@ -11,6 +11,7 @@ import run.halo.app.model.dto.post.BasePostMinimalDTO; import run.halo.app.model.entity.Sheet; import run.halo.app.model.entity.SheetComment; +import run.halo.app.model.enums.SheetPermalinkType; import run.halo.app.model.vo.SheetCommentWithSheetVO; import run.halo.app.repository.SheetCommentRepository; import run.halo.app.repository.SheetRepository; @@ -95,15 +96,23 @@ public List convertToWithSheetVo(List she private BasePostMinimalDTO buildSheetFullPath(BasePostMinimalDTO basePostMinimalDTO) { StringBuilder fullPath = new StringBuilder(); + SheetPermalinkType permalinkType = optionService.getSheetPermalinkType(); + if (optionService.isEnabledAbsolutePath()) { fullPath.append(optionService.getBlogBaseUrl()); } - fullPath.append(URL_SEPARATOR) - .append(optionService.getSheetPrefix()) - .append(URL_SEPARATOR) - .append(basePostMinimalDTO.getSlug()) - .append(optionService.getPathSuffix()); + if (permalinkType.equals(SheetPermalinkType.SECONDARY)) { + fullPath.append(URL_SEPARATOR) + .append(optionService.getSheetPrefix()) + .append(URL_SEPARATOR) + .append(basePostMinimalDTO.getSlug()) + .append(optionService.getPathSuffix()); + } else if (permalinkType.equals(SheetPermalinkType.ROOT)) { + fullPath.append(URL_SEPARATOR) + .append(basePostMinimalDTO.getSlug()) + .append(optionService.getPathSuffix()); + } basePostMinimalDTO.setFullPath(fullPath.toString()); return basePostMinimalDTO; diff --git a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java index 9653ef6492..6ec73d9108 100644 --- a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java @@ -20,6 +20,7 @@ import run.halo.app.model.entity.SheetMeta; import run.halo.app.model.enums.LogType; import run.halo.app.model.enums.PostStatus; +import run.halo.app.model.enums.SheetPermalinkType; import run.halo.app.model.vo.SheetDetailVO; import run.halo.app.model.vo.SheetListVO; import run.halo.app.repository.SheetRepository; @@ -346,15 +347,23 @@ protected void slugMustNotExist(Sheet sheet) { private String buildFullPath(Sheet sheet) { StringBuilder fullPath = new StringBuilder(); + SheetPermalinkType permalinkType = optionService.getSheetPermalinkType(); + if (optionService.isEnabledAbsolutePath()) { fullPath.append(optionService.getBlogBaseUrl()); } - fullPath.append(URL_SEPARATOR) - .append(optionService.getSheetPrefix()) - .append(URL_SEPARATOR) - .append(sheet.getSlug()) - .append(optionService.getPathSuffix()); + if (permalinkType.equals(SheetPermalinkType.SECONDARY)) { + fullPath.append(URL_SEPARATOR) + .append(optionService.getSheetPrefix()) + .append(URL_SEPARATOR) + .append(sheet.getSlug()) + .append(optionService.getPathSuffix()); + } else if (permalinkType.equals(SheetPermalinkType.ROOT)) { + fullPath.append(URL_SEPARATOR) + .append(sheet.getSlug()) + .append(optionService.getPathSuffix()); + } return fullPath.toString(); }