Complete issue(#156)

pull/165/head
johnniang 2019-05-20 22:36:41 +08:00
parent c02bb1dc48
commit 30955e598f
8 changed files with 68 additions and 13 deletions

View File

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

View File

@ -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<ApplicationStartedEv
URI themeUri = ResourceUtils.getURL(themeClassPath).toURI();
log.debug("Theme uri: [{}]", themeUri);
Path source;
if (themeUri.getScheme().equalsIgnoreCase("jar")) {

View File

@ -186,11 +186,11 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
List<BaseCommentVO> convertToVo(@Nullable List<COMMENT> comments, @Nullable Comparator<BaseCommentVO> 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.

View File

@ -223,7 +223,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
// Check post id
if (!ServiceUtils.isEmptyId(comment.getPostId())) {
targetMustExist(comment.getPostId());
validateTarget(comment.getPostId());
}
// Check parent id

View File

@ -47,9 +47,9 @@ public class JournalCommentServiceImpl extends BaseCommentServiceImpl<JournalCom
}
@Override
public void targetMustExist(Integer journalId) {
public void validateTarget(Integer journalId) {
if (!journalRepository.existsById(journalId)) {
throw new NotFoundException("The journal with id " + journalId + " was not found");
throw new NotFoundException("该日志不存在或已删除").setErrorData(journalId);
}
}

View File

@ -8,6 +8,7 @@ import org.springframework.data.domain.Pageable;
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.Post;
@ -92,9 +93,12 @@ public class PostCommentServiceImpl extends BaseCommentServiceImpl<PostComment>
}
@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);
}
}
}

View File

@ -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<SheetComment
}
@Override
public void targetMustExist(Integer sheetId) {
if (sheetRepository.existsById(sheetId)) {
throw new NotFoundException("The sheet with id " + sheetId + " was not found");
public void validateTarget(Integer sheetId) {
Sheet sheet = sheetRepository.findById(sheetId)
.orElseThrow(() -> new NotFoundException("该页面不存在或已删除").setErrorData(sheetId));
if (sheet.getDisallowComment()) {
throw new BadRequestException("该页面已被禁止评论").setErrorData(sheetId);
}
}

View File

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