diff --git a/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java b/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java index 4586ca3f2..d45dc9e9b 100644 --- a/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java +++ b/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Component; import run.halo.app.event.options.OptionUpdatedEvent; import run.halo.app.event.theme.ThemeActivatedEvent; import run.halo.app.event.user.UserUpdatedEvent; +import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeSettingService; @@ -92,8 +93,9 @@ public class FreemarkerConfigAwareListener { } private void loadThemeConfig() throws TemplateModelException { - configuration.setSharedVariable("theme", themeService.getActivatedTheme()); - configuration.setSharedVariable("static", themeService.getActivatedTheme().getFolderName()); + ThemeProperty activatedTheme = themeService.getActivatedTheme(); + configuration.setSharedVariable("theme", activatedTheme); + configuration.setSharedVariable("static", activatedTheme.getFolderName()); configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); log.debug("Loaded theme and settings"); } diff --git a/src/main/java/run/halo/app/listener/StartedListener.java b/src/main/java/run/halo/app/listener/StartedListener.java index 07d7ce71e..c97bfe4ad 100644 --- a/src/main/java/run/halo/app/listener/StartedListener.java +++ b/src/main/java/run/halo/app/listener/StartedListener.java @@ -1,5 +1,6 @@ package run.halo.app.listener; +import com.sun.nio.zipfs.JarFileSystemProvider; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationStartedEvent; @@ -81,6 +82,8 @@ public class StartedListener implements ApplicationListener extends CrudSer List convertToVo(@Nullable List comments, @Nullable Comparator comparator); /** - * Target must exist. + * Target validation. * * @param targetId target id must not be null (post id, sheet id or journal id) */ - void targetMustExist(@NonNull Integer targetId); + void validateTarget(@NonNull Integer targetId); /** * Lists a page of top comment. diff --git a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java index bbcac107b..479d542c6 100644 --- a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java @@ -223,7 +223,7 @@ public abstract class BaseCommentServiceImpl extend // Check post id if (!ServiceUtils.isEmptyId(comment.getPostId())) { - targetMustExist(comment.getPostId()); + validateTarget(comment.getPostId()); } // Check parent id diff --git a/src/main/java/run/halo/app/service/impl/JournalCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/JournalCommentServiceImpl.java index 5493b7968..7c54da67f 100644 --- a/src/main/java/run/halo/app/service/impl/JournalCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/JournalCommentServiceImpl.java @@ -47,9 +47,9 @@ public class JournalCommentServiceImpl extends BaseCommentServiceImpl } @Override - public void targetMustExist(Integer postId) { - if (!postRepository.existsById(postId)) { - throw new NotFoundException("The post with id " + postId + " was not found"); + public void validateTarget(Integer postId) { + Post post = postRepository.findById(postId) + .orElseThrow(() -> new NotFoundException("该文章不存在或已删除").setErrorData(postId)); + + if (post.getDisallowComment()) { + throw new BadRequestException("该文章已经被禁止评论").setErrorData(postId); } } } 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 e59b6d9fb..17f4fdd50 100644 --- a/src/main/java/run/halo/app/service/impl/SheetCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/SheetCommentServiceImpl.java @@ -6,6 +6,7 @@ import org.springframework.data.domain.PageImpl; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import run.halo.app.exception.BadRequestException; import run.halo.app.exception.NotFoundException; import run.halo.app.model.dto.post.BasePostMinimalDTO; import run.halo.app.model.entity.Sheet; @@ -48,9 +49,12 @@ public class SheetCommentServiceImpl extends BaseCommentServiceImpl new NotFoundException("该页面不存在或已删除").setErrorData(sheetId)); + + if (sheet.getDisallowComment()) { + throw new BadRequestException("该页面已被禁止评论").setErrorData(sheetId); } } diff --git a/src/test/java/run/halo/app/utils/PathTest.java b/src/test/java/run/halo/app/utils/PathTest.java new file mode 100644 index 000000000..6e74c630a --- /dev/null +++ b/src/test/java/run/halo/app/utils/PathTest.java @@ -0,0 +1,42 @@ +package run.halo.app.utils; + +import com.sun.nio.zipfs.JarFileSystemProvider; +import org.junit.Test; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.*; +import java.util.Collections; + +/** + * Path test. + * + * @author johnniang + * @date 19-5-20 + */ +public class PathTest { + + @Test(expected = FileSystemNotFoundException.class) + public void getPathOfJarFileFailure() throws URISyntaxException { + String file = "jar:file:/path/to/jar/xxx.jar!/BOOT-INF/classes!/templates/themes"; + URI uri = new URI(file); + Path path = Paths.get(uri); + + System.out.println("Path: " + path.toString()); + } + +// @Test +// public void getPathOfJarFileSuccessfully() throws URISyntaxException, IOException { +// String file = "jar:file:/path/to/jar/xxx.jar!/BOOT-INF/classes!/templates/themes"; +// URI uri = new URI(file); +// FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap()); +// Path path = fileSystem.getPath("/BOOT-INF/classes/templates/themes"); +// +// System.out.println("Path: " + path.toString()); +// +// Files.walk(path, 1).forEach(p -> { +// System.out.println(p.toString()); +// }); +// } +}