Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #1174 #1177

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/run/halo/app/model/enums/SheetPermalinkType.java
Original file line number Diff line number Diff line change
@@ -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<Integer> {

/**
* /{@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;
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/run/halo/app/service/OptionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -392,6 +393,13 @@ public interface OptionService extends CrudService<Option, Integer> {
*/
PostPermalinkType getPostPermalinkType();

/**
* Get sheet permalink type.
*
* @return SheetPermalinkType
*/
SheetPermalinkType getSheetPermalinkType();

/**
* Get sheet custom prefix.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,15 +96,23 @@ public List<SheetCommentWithSheetVO> convertToWithSheetVo(List<SheetComment> 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;
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/run/halo/app/service/impl/SheetServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down