wiki增加历史版本功能,原创使用git方式管理wiki历史版本和查看,修改评论展示区域,修改布局
This commit is contained in:
@@ -14,7 +14,7 @@ import com.zyplayer.doc.data.repository.support.consts.UserMsgType;
|
||||
import com.zyplayer.doc.data.service.manage.*;
|
||||
import com.zyplayer.doc.wiki.controller.vo.UserPageAuthVo;
|
||||
import com.zyplayer.doc.wiki.framework.consts.WikiAuthType;
|
||||
import com.zyplayer.doc.wiki.service.WikiPageAuthService;
|
||||
import com.zyplayer.doc.wiki.service.common.WikiPageAuthService;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -68,15 +68,16 @@ public class WikiPageCommentController {
|
||||
wrapper.eq("del_flag", 0);
|
||||
wrapper.eq("page_id", pageComment.getPageId());
|
||||
wrapper.eq(pageComment.getParentId() != null, "parent_id", pageComment.getParentId());
|
||||
List<WikiPageComment> authList = wikiPageCommentService.list(wrapper);
|
||||
Map<Long, List<WikiPageComment>> listMap = authList.stream().filter(val -> val.getParentId() != null)
|
||||
.collect(Collectors.groupingBy(WikiPageComment::getParentId));
|
||||
List<WikiPageCommentVo> commentList = authList.stream().filter(val -> val.getParentId() == null)
|
||||
.map(val -> mapper.map(val, WikiPageCommentVo.class)).collect(Collectors.toList());
|
||||
for (WikiPageCommentVo commentVo : commentList) {
|
||||
commentVo.setCommentList(listMap.get(commentVo.getId()));
|
||||
}
|
||||
return DocResponseJson.ok(commentList);
|
||||
List<WikiPageComment> pageCommentList = wikiPageCommentService.list(wrapper);
|
||||
// 取消二级评论,全展示在一级
|
||||
// Map<Long, List<WikiPageComment>> listMap = pageCommentList.stream().filter(val -> val.getParentId() != null)
|
||||
// .collect(Collectors.groupingBy(WikiPageComment::getParentId));
|
||||
// List<WikiPageCommentVo> commentList = pageCommentList.stream().filter(val -> val.getParentId() == null)
|
||||
// .map(val -> mapper.map(val, WikiPageCommentVo.class)).collect(Collectors.toList());
|
||||
// for (WikiPageCommentVo commentVo : commentList) {
|
||||
// commentVo.setCommentList(listMap.get(commentVo.getId()));
|
||||
// }
|
||||
return DocResponseJson.ok(pageCommentList);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
|
||||
@@ -25,7 +25,8 @@ import com.zyplayer.doc.data.utils.CacheUtil;
|
||||
import com.zyplayer.doc.wiki.controller.vo.WikiPageContentVo;
|
||||
import com.zyplayer.doc.wiki.controller.vo.WikiPageVo;
|
||||
import com.zyplayer.doc.wiki.framework.consts.SpaceType;
|
||||
import com.zyplayer.doc.wiki.service.WikiPageAuthService;
|
||||
import com.zyplayer.doc.wiki.service.common.WikiPageAuthService;
|
||||
import com.zyplayer.doc.wiki.service.git.GitService;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dozer.Mapper;
|
||||
@@ -73,6 +74,8 @@ public class WikiPageController {
|
||||
@Resource
|
||||
UserMessageService userMessageService;
|
||||
@Resource
|
||||
GitService gitService;
|
||||
@Resource
|
||||
Mapper mapper;
|
||||
@Autowired(required = false)
|
||||
EsWikiPageService esWikiPageService;
|
||||
@@ -278,6 +281,8 @@ public class WikiPageController {
|
||||
} else {
|
||||
logger.warn("未开启elasticsearch服务,建议开启");
|
||||
}
|
||||
// 提交历史版本记录
|
||||
gitService.commitAndAddHistory(wikiPage.getId(), content);
|
||||
return DocResponseJson.ok(wikiPage);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.zyplayer.doc.wiki.controller;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.zyplayer.doc.core.annotation.AuthMan;
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
@@ -18,7 +17,7 @@ import com.zyplayer.doc.data.service.manage.UserMessageService;
|
||||
import com.zyplayer.doc.data.service.manage.WikiPageFileService;
|
||||
import com.zyplayer.doc.data.service.manage.WikiPageService;
|
||||
import com.zyplayer.doc.data.service.manage.WikiSpaceService;
|
||||
import com.zyplayer.doc.wiki.service.WikiPageAuthService;
|
||||
import com.zyplayer.doc.wiki.service.common.WikiPageAuthService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.zyplayer.doc.wiki.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.zyplayer.doc.core.annotation.AuthMan;
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
import com.zyplayer.doc.data.config.security.DocUserDetails;
|
||||
import com.zyplayer.doc.data.config.security.DocUserUtil;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.*;
|
||||
import com.zyplayer.doc.data.repository.support.consts.UserMsgSysType;
|
||||
import com.zyplayer.doc.data.repository.support.consts.UserMsgType;
|
||||
import com.zyplayer.doc.data.service.manage.*;
|
||||
import com.zyplayer.doc.wiki.framework.consts.SpaceType;
|
||||
import com.zyplayer.doc.wiki.service.git.GitService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文档控制器
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月17日
|
||||
*/
|
||||
@AuthMan
|
||||
@RestController
|
||||
@RequestMapping("/zyplayer-doc-wiki/page/history")
|
||||
public class WikiPageHistoryController {
|
||||
private static Logger logger = LoggerFactory.getLogger(WikiPageHistoryController.class);
|
||||
|
||||
@Resource
|
||||
WikiPageHistoryService wikiPageHistoryService;
|
||||
@Resource
|
||||
WikiSpaceService wikiSpaceService;
|
||||
@Resource
|
||||
WikiPageService wikiPageService;
|
||||
@Resource
|
||||
GitService gitService;
|
||||
|
||||
@PostMapping("/list")
|
||||
public ResponseJson<List<WikiPageHistory>> list(Long pageId, Integer pageNum) {
|
||||
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
|
||||
WikiPage wikiPageSel = wikiPageService.getById(pageId);
|
||||
WikiSpace wikiSpaceSel = wikiSpaceService.getById(wikiPageSel.getSpaceId());
|
||||
// 私人空间
|
||||
if (SpaceType.isOthersPrivate(wikiSpaceSel.getType(), currentUser.getUserId(), wikiSpaceSel.getCreateUserId())) {
|
||||
return DocResponseJson.warn("您没有权限查看该空间的文章详情!");
|
||||
}
|
||||
UpdateWrapper<WikiPageHistory> wrapper = new UpdateWrapper<>();
|
||||
wrapper.eq("page_id", pageId);
|
||||
wrapper.eq("del_flag", 0);
|
||||
IPage<WikiPageHistory> page = new Page<>(pageNum, 20, false);
|
||||
wikiPageHistoryService.page(page, wrapper);
|
||||
return DocResponseJson.ok(page);
|
||||
}
|
||||
|
||||
@PostMapping("/detail")
|
||||
public ResponseJson<Object> detail(Long id) {
|
||||
WikiPageHistory wikiPageHistory = wikiPageHistoryService.getById(id);
|
||||
if (wikiPageHistory == null) {
|
||||
return DocResponseJson.warn("未找到相关记录");
|
||||
}
|
||||
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
|
||||
WikiPage wikiPageSel = wikiPageService.getById(wikiPageHistory.getPageId());
|
||||
WikiSpace wikiSpaceSel = wikiSpaceService.getById(wikiPageSel.getSpaceId());
|
||||
// 私人空间
|
||||
if (SpaceType.isOthersPrivate(wikiSpaceSel.getType(), currentUser.getUserId(), wikiSpaceSel.getCreateUserId())) {
|
||||
return DocResponseJson.warn("您没有权限查看该空间的文章详情!");
|
||||
}
|
||||
String pageHistory = gitService.getPageHistory(wikiPageHistory.getGitCommitId(), wikiPageHistory.getPageId());
|
||||
return DocResponseJson.ok(pageHistory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.zyplayer.doc.wiki.service;
|
||||
package com.zyplayer.doc.wiki.service.common;
|
||||
|
||||
import com.zyplayer.doc.data.config.security.DocUserUtil;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.WikiSpace;
|
||||
@@ -0,0 +1,133 @@
|
||||
package com.zyplayer.doc.wiki.service.git;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.zyplayer.doc.data.config.security.DocUserDetails;
|
||||
import com.zyplayer.doc.data.config.security.DocUserUtil;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.WikiPageHistory;
|
||||
import com.zyplayer.doc.data.service.manage.WikiPageHistoryService;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.Status;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.ObjectLoader;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* git管理相关服务
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2020年9月5日
|
||||
*/
|
||||
@Service
|
||||
public class GitService {
|
||||
private static Logger logger = LoggerFactory.getLogger(GitService.class);
|
||||
|
||||
@Value("${zyplayer.doc.wiki.git-file-path:''}")
|
||||
private String gitFilePath;
|
||||
|
||||
@Resource
|
||||
WikiPageHistoryService wikiPageHistoryService;
|
||||
|
||||
/**
|
||||
* 提交和写历史记录
|
||||
*/
|
||||
public void commitAndAddHistory(Long pageId, String content) {
|
||||
String commitId = this.writeAndCommit(pageId, content);
|
||||
// 保存历史记录
|
||||
if (commitId != null) {
|
||||
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
|
||||
WikiPageHistory entity = new WikiPageHistory();
|
||||
entity.setPageId(pageId);
|
||||
entity.setCreateTime(new Date());
|
||||
entity.setDelFlag(0);
|
||||
entity.setGitCommitId(commitId);
|
||||
entity.setCreateUserId(currentUser.getUserId());
|
||||
entity.setCreateUserName(currentUser.getUsername());
|
||||
wikiPageHistoryService.save(entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 把内容写入文件并提交git
|
||||
*/
|
||||
private synchronized String writeAndCommit(Long pageId, String content) {
|
||||
try {
|
||||
// 初始化git仓库
|
||||
File dirFile = new File(this.getGitPath());
|
||||
Git git = Git.init().setGitDir(dirFile).setDirectory(dirFile.getParentFile()).call();
|
||||
// 修改文件内容
|
||||
FileUtil.writeString(content, this.getGitPagePath(pageId), "utf-8");
|
||||
// git提交
|
||||
if (git.status().call().isClean()) {
|
||||
logger.info("no changed");
|
||||
return null;
|
||||
}
|
||||
git.add().addFilepattern(".").call();
|
||||
Status status = git.status().call();
|
||||
Set<String> missing = status.getMissing();
|
||||
if (CollectionUtils.isNotEmpty(missing)) {
|
||||
for (String f : missing) {
|
||||
logger.info("missing file: {}", f);
|
||||
git.rm().addFilepattern(f).call();
|
||||
}
|
||||
}
|
||||
Set<String> removed = status.getRemoved();
|
||||
if (CollectionUtils.isNotEmpty(removed)) {
|
||||
for (String f : removed) {
|
||||
logger.info("remove file: {}", f);
|
||||
git.rm().addFilepattern(f).call();
|
||||
}
|
||||
}
|
||||
RevCommit commit = git.commit().setMessage("commit").call();
|
||||
logger.info("commit id: {}", commit.getName());
|
||||
return commit.getName();
|
||||
} catch (Exception e) {
|
||||
logger.error("git仓库提交失败", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getPageHistory(String objId, Long pageId) {
|
||||
try {
|
||||
Git git = Git.open(new File(this.getGitPath()));
|
||||
Repository repository = git.getRepository();
|
||||
RevCommit revCommit = new RevWalk(repository).parseCommit(ObjectId.fromString(objId));
|
||||
TreeWalk treeWalk = TreeWalk.forPath(repository, this.getGitPageFile(pageId), revCommit.getTree());
|
||||
treeWalk.setRecursive(false);
|
||||
ObjectId blobId = treeWalk.getObjectId(0);
|
||||
ObjectLoader loader = repository.open(blobId);
|
||||
return new String(loader.getBytes());
|
||||
} catch (Exception e) {
|
||||
logger.error("获取git文件内容失败", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String getGitPath() {
|
||||
return gitFilePath + "/.git";
|
||||
}
|
||||
|
||||
|
||||
private String getGitPageFile(Long pageId) {
|
||||
return pageId + ".txt";
|
||||
}
|
||||
|
||||
private String getGitPagePath(Long pageId) {
|
||||
return gitFilePath + "/" + this.getGitPageFile(pageId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
//package com.zyplayer.doc.wiki.service.git;
|
||||
//
|
||||
//import org.apache.commons.collections.CollectionUtils;
|
||||
//import org.eclipse.jgit.api.Git;
|
||||
//import org.eclipse.jgit.api.Status;
|
||||
//import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
//import org.eclipse.jgit.lib.ObjectId;
|
||||
//import org.eclipse.jgit.lib.ObjectLoader;
|
||||
//import org.eclipse.jgit.lib.Repository;
|
||||
//import org.eclipse.jgit.revwalk.RevCommit;
|
||||
//import org.eclipse.jgit.revwalk.RevWalk;
|
||||
//import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//
|
||||
//import java.io.File;
|
||||
//import java.util.Set;
|
||||
//
|
||||
//public class GitTest {
|
||||
// private static Logger logger = LoggerFactory.getLogger(GitTest.class);
|
||||
//
|
||||
// private static final String LOCAL_PATH = "d:/zyplayerDoc/wikiGitFiles/.git";
|
||||
//
|
||||
// /**
|
||||
// * 提交
|
||||
// */
|
||||
// private static String commitFiles() {
|
||||
// try {
|
||||
// Git git = Git.open(new File(LOCAL_PATH));
|
||||
// if (git.status().call().isClean()) {
|
||||
// logger.info("no changed");
|
||||
// return null;
|
||||
// }
|
||||
// git.add().addFilepattern(".").call();
|
||||
// Status status = git.status().call();
|
||||
// Set<String> missing = status.getMissing();
|
||||
// if (CollectionUtils.isNotEmpty(missing)) {
|
||||
// for (String f : missing) {
|
||||
// logger.info("missing file: {}", f);
|
||||
// git.rm().addFilepattern(f).call();
|
||||
// }
|
||||
// }
|
||||
// Set<String> removed = status.getRemoved();
|
||||
// if (CollectionUtils.isNotEmpty(removed)) {
|
||||
// for (String f : removed) {
|
||||
// logger.info("remove file: {}", f);
|
||||
// git.rm().addFilepattern(f).call();
|
||||
// }
|
||||
// }
|
||||
// RevCommit commit = git.commit().setMessage("commit").call();
|
||||
// logger.info("commit id: {}", commit.getName());
|
||||
// return commit.getName();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// private static void init() throws GitAPIException {
|
||||
// File dirFile = new File(LOCAL_PATH);
|
||||
// Git.init().setGitDir(dirFile).setDirectory(dirFile.getParentFile()).call();
|
||||
// }
|
||||
//
|
||||
// private static void getFile(String objId, String entryPath) throws Exception {
|
||||
// Git git = Git.open(new File(LOCAL_PATH));
|
||||
// Repository repository = git.getRepository();
|
||||
// RevCommit revCommit = new RevWalk(repository).parseCommit(ObjectId.fromString(objId));
|
||||
// TreeWalk treeWalk = TreeWalk.forPath(repository, entryPath, revCommit.getTree());
|
||||
// treeWalk.setRecursive(false);
|
||||
// ObjectId blobId = treeWalk.getObjectId(0);
|
||||
// ObjectLoader loader = repository.open(blobId);
|
||||
// String fileContent = new String(loader.getBytes());
|
||||
// logger.info("fileContent: {}", fileContent);
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) throws Exception {
|
||||
//// init();
|
||||
//// commitFiles();
|
||||
// getFile("b47305c88251b7473928640b1f5346b40bb7387e", "14.txt");
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user