diff --git a/src/main/java/run/halo/app/controller/admin/api/PostController.java b/src/main/java/run/halo/app/controller/admin/api/PostController.java index 0413f8464..7115c479d 100644 --- a/src/main/java/run/halo/app/controller/admin/api/PostController.java +++ b/src/main/java/run/halo/app/controller/admin/api/PostController.java @@ -150,7 +150,7 @@ public class PostController { cacheStore.putAny("preview-post-token-" + postId, token, 10, TimeUnit.MINUTES); // build preview post url - String url = String.format("%s/preview/post/%s?token=%s", optionService.getBlogBaseUrl(), post.getUrl(), token); + String url = String.format("%s/archives/%s?preview=true&token=%s", optionService.getBlogBaseUrl(), post.getUrl(), token); // redirect to preview url response.sendRedirect(url); diff --git a/src/main/java/run/halo/app/controller/admin/api/SheetController.java b/src/main/java/run/halo/app/controller/admin/api/SheetController.java index 5438a7802..13e0e5782 100644 --- a/src/main/java/run/halo/app/controller/admin/api/SheetController.java +++ b/src/main/java/run/halo/app/controller/admin/api/SheetController.java @@ -123,7 +123,7 @@ public class SheetController { cacheStore.putAny("preview-sheet-token-" + sheetId, token, 10, TimeUnit.MINUTES); // build preview sheet url - String url = String.format("%s/preview/s/%s?token=%s", optionService.getBlogBaseUrl(), sheet.getUrl(), token); + String url = String.format("%s/s/%s?preview=true&token=%s", optionService.getBlogBaseUrl(), sheet.getUrl(), token); // redirect to preview url response.sendRedirect(url); diff --git a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java index 95c19469a..2824ca41f 100644 --- a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java +++ b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java @@ -13,15 +13,18 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import run.halo.app.cache.StringCacheStore; +import run.halo.app.exception.ForbiddenException; import run.halo.app.model.entity.Category; import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Tag; import run.halo.app.model.enums.PostStatus; -import run.halo.app.model.vo.BaseCommentVO; import run.halo.app.model.vo.PostListVO; import run.halo.app.service.*; +import run.halo.app.utils.MarkdownUtils; import java.util.List; +import java.util.concurrent.TimeUnit; import static org.springframework.data.domain.Sort.Direction.DESC; @@ -38,8 +41,6 @@ public class ContentArchiveController { private final PostService postService; - private final PostCommentService postCommentService; - private final ThemeService themeService; private final PostCategoryService postCategoryService; @@ -48,18 +49,20 @@ public class ContentArchiveController { private final OptionService optionService; + private final StringCacheStore cacheStore; + public ContentArchiveController(PostService postService, - PostCommentService postCommentService, ThemeService themeService, PostCategoryService postCategoryService, PostTagService postTagService, - OptionService optionService) { + OptionService optionService, + StringCacheStore cacheStore) { this.postService = postService; - this.postCommentService = postCommentService; this.themeService = themeService; this.postCategoryService = postCategoryService; this.postTagService = postTagService; this.optionService = optionService; + this.cacheStore = cacheStore; } /** @@ -99,34 +102,46 @@ public class ContentArchiveController { /** * Render post page. * - * @param url post slug url. - * @param cp comment page number - * @param model model + * @param url post slug url. + * @param preview preview + * @param token preview token + * @param model model * @return template path: themes/{theme}/post.ftl */ @GetMapping("{url}") public String post(@PathVariable("url") String url, - @RequestParam(value = "cp", defaultValue = "1") Integer cp, - @SortDefault(sort = "createTime", direction = DESC) Sort sort, + @RequestParam(value = "preview", required = false, defaultValue = "false") boolean preview, + @RequestParam(value = "token", required = false) String token, Model model) { - Post post = postService.getBy(PostStatus.PUBLISHED, url); + Post post = postService.getBy(preview ? PostStatus.DRAFT : PostStatus.PUBLISHED, url); + + if (preview) { + // render markdown to html when preview post + post.setFormatContent(MarkdownUtils.renderHtml(post.getOriginalContent())); + + // verify token + String cachedToken = cacheStore.getAny("preview-post-token-" + post.getId(), String.class).orElseThrow(() -> new ForbiddenException("该文章的预览链接不存在或已过期")); + + if (!cachedToken.equals(token)) { + throw new ForbiddenException("该文章的预览链接不存在或已过期"); + } + } postService.getNextPost(post.getCreateTime()).ifPresent(nextPost -> model.addAttribute("nextPost", nextPost)); postService.getPrePost(post.getCreateTime()).ifPresent(prePost -> model.addAttribute("prePost", prePost)); - List categories = postCategoryService.listCategoriesBy(post.getId()); List tags = postTagService.listTagsBy(post.getId()); - Page comments = postCommentService.pageVosBy(post.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort)); - final int[] pageRainbow = PageUtil.rainbow(cp, comments.getTotalPages(), 3); - model.addAttribute("is_post", true); model.addAttribute("post", post); model.addAttribute("categories", categories); model.addAttribute("tags", tags); - model.addAttribute("comments", comments); - model.addAttribute("pageRainbow", pageRainbow); + + if (preview) { + // refresh timeUnit + cacheStore.putAny("preview-post-token-" + post.getId(), token, 10, TimeUnit.MINUTES); + } return themeService.render("post"); } diff --git a/src/main/java/run/halo/app/controller/content/ContentPreviewController.java b/src/main/java/run/halo/app/controller/content/ContentPreviewController.java deleted file mode 100644 index 7edeba0c8..000000000 --- a/src/main/java/run/halo/app/controller/content/ContentPreviewController.java +++ /dev/null @@ -1,116 +0,0 @@ -package run.halo.app.controller.content; - -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import run.halo.app.cache.StringCacheStore; -import run.halo.app.exception.ForbiddenException; -import run.halo.app.model.entity.Category; -import run.halo.app.model.entity.Post; -import run.halo.app.model.entity.Sheet; -import run.halo.app.model.entity.Tag; -import run.halo.app.model.enums.PostStatus; -import run.halo.app.model.support.HaloConst; -import run.halo.app.service.*; -import run.halo.app.utils.MarkdownUtils; - -import java.util.List; -import java.util.concurrent.TimeUnit; - -/** - * Post and sheet preview controller. - * - * @author ryanwang - * @date : 2019-09-05 - */ -@Slf4j -@Controller -@RequestMapping(value = "/preview") -public class ContentPreviewController { - - private final PostService postService; - - private final ThemeService themeService; - - private final PostCategoryService postCategoryService; - - private final PostTagService postTagService; - - private final SheetService sheetService; - - private final StringCacheStore cacheStore; - - public ContentPreviewController(PostService postService, - ThemeService themeService, - PostCategoryService postCategoryService, - PostTagService postTagService, - SheetService sheetService, - StringCacheStore cacheStore) { - this.postService = postService; - this.themeService = themeService; - this.postCategoryService = postCategoryService; - this.postTagService = postTagService; - this.sheetService = sheetService; - this.cacheStore = cacheStore; - } - - @GetMapping(value = "post/{url}") - public String post(@PathVariable("url") String url, - @RequestParam(value = "token") String token, - Model model) { - Post post = postService.getBy(PostStatus.DRAFT, url); - - post.setFormatContent(MarkdownUtils.renderHtml(post.getOriginalContent())); - - // verify token - String cachedToken = cacheStore.getAny("preview-post-token-" + post.getId(), String.class).orElseThrow(() -> new ForbiddenException("该文章的预览链接不存在或已过期")); - - if (!cachedToken.equals(token)) { - throw new ForbiddenException("该文章的预览链接不存在或已过期"); - } - - List categories = postCategoryService.listCategoriesBy(post.getId()); - List tags = postTagService.listTagsBy(post.getId()); - - model.addAttribute("is_post", true); - model.addAttribute("post", post); - model.addAttribute("categories", categories); - model.addAttribute("tags", tags); - - // refresh timeUnit - cacheStore.putAny("preview-post-token-" + post.getId(), token, 10, TimeUnit.MINUTES); - - return themeService.render("post"); - } - - @GetMapping(value = "s/{url}") - public String sheet(@PathVariable("url") String url, - @RequestParam(value = "token") String token, - Model model) { - - Sheet sheet = sheetService.getBy(PostStatus.DRAFT, url); - - sheet.setFormatContent(MarkdownUtils.renderHtml(sheet.getOriginalContent())); - - // verify token - String cachedToken = cacheStore.getAny("preview-sheet-token-" + sheet.getId(), String.class).orElseThrow(() -> new ForbiddenException("该页面的预览链接不存在或已过期")); - - if (!cachedToken.equals(token)) { - throw new ForbiddenException("该页面的预览链接不存在或已过期"); - } - - // sheet and post all can use - model.addAttribute("sheet", sheetService.convertToDetail(sheet)); - model.addAttribute("post", sheetService.convertToDetail(sheet)); - model.addAttribute("is_sheet", true); - - if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { - return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate()); - } - return themeService.render("sheet"); - } -} diff --git a/src/main/java/run/halo/app/controller/content/ContentSheetController.java b/src/main/java/run/halo/app/controller/content/ContentSheetController.java index 2b1ae4c61..707ffeee0 100644 --- a/src/main/java/run/halo/app/controller/content/ContentSheetController.java +++ b/src/main/java/run/halo/app/controller/content/ContentSheetController.java @@ -4,11 +4,17 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import run.halo.app.cache.StringCacheStore; +import run.halo.app.exception.ForbiddenException; import run.halo.app.model.entity.Sheet; import run.halo.app.model.enums.PostStatus; import run.halo.app.model.support.HaloConst; import run.halo.app.service.SheetService; import run.halo.app.service.ThemeService; +import run.halo.app.utils.MarkdownUtils; + +import java.util.concurrent.TimeUnit; /** * Content sheet controller. @@ -24,10 +30,14 @@ public class ContentSheetController { private final ThemeService themeService; + private final StringCacheStore cacheStore; + public ContentSheetController(SheetService sheetService, - ThemeService themeService) { + ThemeService themeService, + StringCacheStore cacheStore) { this.sheetService = sheetService; this.themeService = themeService; + this.cacheStore = cacheStore; } /** @@ -53,20 +63,41 @@ public class ContentSheetController { /** * Render custom sheet * - * @param url sheet url - * @param model model + * @param url sheet url + * @param preview preview + * @param token token + * @param model model * @return template path: themes/{theme}/sheet.ftl */ @GetMapping(value = "/s/{url}") public String sheet(@PathVariable(value = "url") String url, + @RequestParam(value = "preview", required = false, defaultValue = "false") boolean preview, + @RequestParam(value = "token", required = false) String token, Model model) { - Sheet sheet = sheetService.getBy(PostStatus.PUBLISHED, url); + Sheet sheet = sheetService.getBy(preview ? PostStatus.DRAFT : PostStatus.PUBLISHED, url); + + if (preview) { + // render markdown to html when preview post + sheet.setFormatContent(MarkdownUtils.renderHtml(sheet.getOriginalContent())); + + // verify token + String cachedToken = cacheStore.getAny("preview-sheet-token-" + sheet.getId(), String.class).orElseThrow(() -> new ForbiddenException("该页面的预览链接不存在或已过期")); + + if (!cachedToken.equals(token)) { + throw new ForbiddenException("该页面的预览链接不存在或已过期"); + } + } // sheet and post all can use model.addAttribute("sheet", sheetService.convertToDetail(sheet)); model.addAttribute("post", sheetService.convertToDetail(sheet)); model.addAttribute("is_sheet", true); + if (preview) { + // refresh timeUnit + cacheStore.putAny("preview-sheet-token-" + sheet.getId(), token, 10, TimeUnit.MINUTES); + } + if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate()); }