wiki历史支持push到远程仓库,防止丢失
This commit is contained in:
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.zyplayer.doc.core.annotation.AuthMan;
|
||||
import com.zyplayer.doc.core.exception.ConfirmException;
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
import com.zyplayer.doc.data.config.security.DocUserDetails;
|
||||
@@ -281,8 +282,12 @@ public class WikiPageController {
|
||||
} else {
|
||||
logger.warn("未开启elasticsearch服务,建议开启");
|
||||
}
|
||||
// 提交历史版本记录
|
||||
gitService.commitAndAddHistory(wikiPage.getId(), content);
|
||||
try {
|
||||
// 提交历史版本记录
|
||||
gitService.commitAndAddHistory(wikiPage.getId(), content);
|
||||
} catch (ConfirmException e) {
|
||||
return DocResponseJson.warn(e.getMessage());
|
||||
}
|
||||
return DocResponseJson.ok(wikiPage);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,11 +10,13 @@ import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.Status;
|
||||
import org.eclipse.jgit.internal.storage.file.FileRepository;
|
||||
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.transport.UsernamePasswordCredentialsProvider;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -36,8 +38,16 @@ import java.util.Set;
|
||||
public class GitService {
|
||||
private static Logger logger = LoggerFactory.getLogger(GitService.class);
|
||||
|
||||
@Value("${zyplayer.doc.wiki.git-file-path:''}")
|
||||
@Value("${zyplayer.doc.wiki.git-file-path:}")
|
||||
private String gitFilePath;
|
||||
@Value("${zyplayer.doc.wiki.git-local-path:}")
|
||||
private String gitLocalPath;
|
||||
@Value("${zyplayer.doc.wiki.git-remote-url:}")
|
||||
private String gitRemoteUrl;
|
||||
@Value("${zyplayer.doc.wiki.git-remote-username:}")
|
||||
private String gitRemoteUsername;
|
||||
@Value("${zyplayer.doc.wiki.git-remote-password:}")
|
||||
private String gitRemotePassword;
|
||||
|
||||
@Resource
|
||||
WikiPageHistoryService wikiPageHistoryService;
|
||||
@@ -50,7 +60,7 @@ public class GitService {
|
||||
*/
|
||||
public void commitAndAddHistory(Long pageId, String content) {
|
||||
// 未配置git仓库地址,不存历史
|
||||
if (StringUtils.isBlank(gitFilePath)) {
|
||||
if (StringUtils.isBlank(this.getGitLocalPath())) {
|
||||
return;
|
||||
}
|
||||
String commitId = this.writeAndCommit(pageId, content);
|
||||
@@ -78,11 +88,25 @@ public class GitService {
|
||||
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();
|
||||
Git git;
|
||||
File dirFile = new File(this.getDotGitPath());
|
||||
if (!dirFile.exists()) {
|
||||
if (StringUtils.isNotBlank(gitRemoteUrl)) {
|
||||
// 远程仓库拉取
|
||||
git = Git.cloneRepository().setURI(gitRemoteUrl)
|
||||
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(gitRemoteUsername, gitRemotePassword))
|
||||
.setBranch("master").setGitDir(dirFile).setDirectory(dirFile.getParentFile()).call();
|
||||
} else {
|
||||
// 创建本地仓库
|
||||
git = Git.init().setGitDir(dirFile).setDirectory(dirFile.getParentFile()).call();
|
||||
}
|
||||
} else {
|
||||
// 打开本地仓库
|
||||
git = new Git(new FileRepository(this.getDotGitPath()));
|
||||
}
|
||||
// 修改文件内容
|
||||
FileUtil.writeString(content, this.getGitPagePath(pageId), "utf-8");
|
||||
// git提交
|
||||
// 没有改变的内容
|
||||
if (git.status().call().isClean()) {
|
||||
logger.info("no changed");
|
||||
return null;
|
||||
@@ -104,12 +128,16 @@ public class GitService {
|
||||
}
|
||||
}
|
||||
RevCommit commit = git.commit().setMessage("commit").call();
|
||||
// 存在远程地址则推送
|
||||
if (StringUtils.isNotBlank(gitRemoteUrl)) {
|
||||
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(gitRemoteUsername, gitRemotePassword)).call();
|
||||
}
|
||||
logger.info("commit id: {}", commit.getName());
|
||||
return commit.getName();
|
||||
} catch (Exception e) {
|
||||
logger.error("git仓库提交失败", e);
|
||||
throw new ConfirmException("创建历史记录失败:" + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,11 +148,11 @@ public class GitService {
|
||||
* @return 页面内容
|
||||
*/
|
||||
public String getPageHistory(String objId, Long pageId) {
|
||||
if (StringUtils.isBlank(gitFilePath)) {
|
||||
if (StringUtils.isBlank(this.getGitLocalPath())) {
|
||||
throw new ConfirmException("未配置WIKI历史版本的git目录");
|
||||
}
|
||||
try {
|
||||
Git git = Git.open(new File(this.getGitPath()));
|
||||
Git git = Git.open(new File(this.getDotGitPath()));
|
||||
Repository repository = git.getRepository();
|
||||
RevCommit revCommit = new RevWalk(repository).parseCommit(ObjectId.fromString(objId));
|
||||
TreeWalk treeWalk = TreeWalk.forPath(repository, this.getGitPageFile(pageId), revCommit.getTree());
|
||||
@@ -134,12 +162,16 @@ public class GitService {
|
||||
return new String(loader.getBytes());
|
||||
} catch (Exception e) {
|
||||
logger.error("获取git文件内容失败", e);
|
||||
return null;
|
||||
throw new ConfirmException("获取历史版本数据失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private String getGitPath() {
|
||||
return gitFilePath + "/.git";
|
||||
private String getGitLocalPath() {
|
||||
return StringUtils.defaultIfBlank(gitLocalPath, gitFilePath);
|
||||
}
|
||||
|
||||
private String getDotGitPath() {
|
||||
return this.getGitLocalPath() + "/.git";
|
||||
}
|
||||
|
||||
private String getGitPageFile(Long pageId) {
|
||||
@@ -147,6 +179,6 @@ public class GitService {
|
||||
}
|
||||
|
||||
private String getGitPagePath(Long pageId) {
|
||||
return gitFilePath + "/" + this.getGitPageFile(pageId);
|
||||
return this.getGitLocalPath() + "/" + this.getGitPageFile(pageId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
::-webkit-scrollbar{width:6px;height:9px;-webkit-appearance:none}::-webkit-scrollbar-thumb{background:#ddd;border-radius:10px}::-webkit-scrollbar-track-piece{background:#eee}body,html{margin:0;padding:0}#app,.el-container,.el-menu,.global-layout-vue,body,html{height:100%}.el-header{background-color:#1d4e89!important;color:#333;line-height:40px;text-align:right;height:40px!important}.icon-collapse{float:left;font-size:25px;color:#aaa;margin-top:8px;cursor:pointer}.icon-collapse:hover{color:#eee}.head-icon{margin-right:15px;font-size:16px;cursor:pointer;color:#fff}.header-user-message .page-info-box{text-align:right;margin-top:10px}#app[data-v-8f7ddbbc],body[data-v-8f7ddbbc],html[data-v-8f7ddbbc]{margin:0;padding:0;height:100%}pre[data-v-8f7ddbbc]{margin:0;white-space:pre-wrap;font-size:14px;font-family:auto}.el-menu[data-v-8f7ddbbc]{-webkit-box-sizing:border-box;box-sizing:border-box;border-right:0;margin-right:3px}.el-header[data-v-8f7ddbbc]{background-color:#409eff;color:#333;line-height:40px;text-align:right;height:40px!important}.doc-body-box[data-v-8f7ddbbc]{overflow-x:hidden;overflow-y:auto;width:100%;padding:10px;border-left:1px solid #f1f1f1;-webkit-box-sizing:border-box;box-sizing:border-box}.el-tree[data-v-8f7ddbbc]{margin-right:3px}.logo[data-v-8f7ddbbc]{border-bottom:1px solid #f1f1f1;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;padding:5px 10px;width:260px;height:40px;line-height:40px;font-size:25px;color:#666;text-align:center}.icon-collapse[data-v-8f7ddbbc]{float:left;font-size:25px;color:#aaa;cursor:pointer;position:fixed}.icon-collapse[data-v-8f7ddbbc]:hover{color:#ccc}.comment-box .head[data-v-8f7ddbbc]{float:left;background-color:#ccc;border-radius:50%;margin-right:10px;width:45px;height:45px;line-height:45px;text-align:center;color:#fff}.build-info[data-v-8f7ddbbc]{position:fixed;bottom:0;left:0;background:#fafafa;width:240px;text-align:center;padding:5px 0;color:#aaa;font-size:12px}.build-info span[data-v-8f7ddbbc]{color:#4183c4;cursor:pointer}
|
||||
::-webkit-scrollbar{width:6px;height:9px;-webkit-appearance:none}::-webkit-scrollbar-thumb{background:#ddd;border-radius:10px}::-webkit-scrollbar-track-piece{background:#eee}body,html{margin:0;padding:0}#app,.el-container,.el-menu,.global-layout-vue,body,html{height:100%}.el-header{background-color:#1d4e89!important;color:#333;line-height:40px;text-align:right;height:40px!important}.icon-collapse{float:left;font-size:25px;color:#aaa;margin-top:8px;cursor:pointer}.icon-collapse:hover{color:#eee}.head-icon{margin-right:15px;font-size:16px;cursor:pointer;color:#fff}.header-user-message .page-info-box{text-align:right;margin-top:10px}#app[data-v-3799a29f],body[data-v-3799a29f],html[data-v-3799a29f]{margin:0;padding:0;height:100%}pre[data-v-3799a29f]{margin:0;white-space:pre-wrap;font-size:14px;font-family:auto}.el-menu[data-v-3799a29f]{-webkit-box-sizing:border-box;box-sizing:border-box;border-right:0;margin-right:3px}.el-header[data-v-3799a29f]{background-color:#409eff;color:#333;line-height:40px;text-align:right;height:40px!important}.doc-body-box[data-v-3799a29f]{overflow-x:hidden;overflow-y:auto;width:100%;padding:10px;border-left:1px solid #f1f1f1;-webkit-box-sizing:border-box;box-sizing:border-box}.el-tree[data-v-3799a29f]{margin-right:3px}.logo[data-v-3799a29f]{border-bottom:1px solid #f1f1f1;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;padding:5px 10px;width:260px;height:40px;line-height:40px;font-size:25px;color:#666;text-align:center}.icon-collapse[data-v-3799a29f]{float:left;font-size:25px;color:#aaa;cursor:pointer;position:fixed}.icon-collapse[data-v-3799a29f]:hover{color:#ccc}.comment-box .head[data-v-3799a29f]{float:left;background-color:#ccc;border-radius:50%;margin-right:10px;width:45px;height:45px;line-height:45px;text-align:center;color:#fff}.build-info[data-v-3799a29f]{position:fixed;bottom:0;left:0;background:#fafafa;width:240px;text-align:center;padding:5px 0;color:#aaa;font-size:12px}.build-info span[data-v-3799a29f]{color:#4183c4;cursor:pointer}
|
||||
@@ -1 +1 @@
|
||||
.page-show-vue{height:100%}.page-show-vue .icon-collapse{float:left;font-size:25px;color:#aaa;margin-top:8px;cursor:pointer}.page-show-vue .icon-collapse:hover{color:#eee}.page-show-vue .wiki-title{font-size:20px;text-align:center;font-weight:700}.page-show-vue .create-user-time{margin-right:20px}.page-show-vue .wiki-author{font-size:14px;color:#888;padding:20px 0;height:40px;line-height:40px}.page-show-vue .wiki-content{font-size:14px}.page-show-vue .wiki-content.w-e-text{overflow-y:auto}.page-show-vue .wiki-content.w-e-text img{cursor:auto}.page-show-vue .wiki-content.w-e-text img:hover{-webkit-box-shadow:unset;box-shadow:unset}.page-show-vue .upload-page-file .el-upload-list{display:none}.page-show-vue .is-link{color:#1e88e5;cursor:pointer}.page-show-vue #newPageContentDiv .w-e-text-container{height:600px!important}.page-show-vue .head{float:left;background-color:#ccc;border-radius:50%;margin-right:10px;width:45px;height:45px;line-height:45px;text-align:center;color:#fff}.page-show-vue .el-tabs__header{margin:0}.page-show-vue .el-tabs__nav-wrap{padding:0 20px}.page-show-vue .close-action-tab{position:absolute;right:15px;top:12px;cursor:pointer;z-index:1}.page-show-vue .action-tab-box{height:calc(100vh - 120px);overflow:auto;padding:20px 10px}.page-show-vue .action-box-empty{text-align:center;padding-top:30px;color:#888;font-size:14px}.page-show-vue .history-item{height:55px;line-height:25px;cursor:pointer;vertical-align:middle}.page-show-vue .history-loading-status{margin-left:5px;color:#67c23a}.page-show-vue .el-timeline{-webkit-padding-start:0;padding-inline-start:0}.page-show-vue .comment-user-name{margin-bottom:10px}.page-show-vue .comment-content{padding:0;color:#666;margin:0;white-space:pre-wrap;word-wrap:break-word;line-height:20px}.page-show-vue .comment-input-box{position:absolute;bottom:0;width:100%;background:#fff;border-top:1px solid #f1f1f1}.page-show-vue .comment-input-box textarea{resize:none;width:100%;-webkit-box-sizing:border-box;box-sizing:border-box;border:0;outline:none!important;padding:10px}.page-show-vue .comment-card .comment-user-name .el-icon-delete{color:#888;font-size:13px;cursor:pointer;float:right;display:none}.page-show-vue .comment-card:hover .comment-user-name .el-icon-delete{display:inline-block}
|
||||
.page-show-vue{height:100%}.page-show-vue .icon-collapse{float:left;font-size:25px;color:#aaa;margin-top:8px;cursor:pointer}.page-show-vue .icon-collapse:hover{color:#eee}.page-show-vue .wiki-title{font-size:20px;text-align:center;font-weight:700}.page-show-vue .create-user-time{margin-right:20px}.page-show-vue .wiki-author{font-size:14px;color:#888;padding:20px 0;height:40px;line-height:40px}.page-show-vue .wiki-content{font-size:14px}.page-show-vue .wiki-content.w-e-text{overflow-y:auto}.page-show-vue .wiki-content.w-e-text img{cursor:auto}.page-show-vue .wiki-content.w-e-text img:hover{-webkit-box-shadow:unset;box-shadow:unset}.page-show-vue .upload-page-file .el-upload-list{display:none}.page-show-vue .is-link{color:#1e88e5;cursor:pointer}.page-show-vue #newPageContentDiv .w-e-text-container{height:600px!important}.page-show-vue .head{float:left;background-color:#ccc;border-radius:50%;margin-right:10px;width:45px;height:45px;line-height:45px;text-align:center;color:#fff}.page-show-vue .el-tabs__header{margin:0}.page-show-vue .el-tabs__nav-wrap{padding:0 20px}.page-show-vue .close-action-tab{position:absolute;right:15px;top:12px;cursor:pointer;z-index:1}.page-show-vue .action-tab-box{height:calc(100vh - 120px);overflow:auto;padding:20px 10px}.page-show-vue .action-box-empty{text-align:center;padding-top:30px;color:#888;font-size:14px}.page-show-vue .history-item{height:55px;line-height:25px;cursor:pointer;vertical-align:middle}.page-show-vue .history-loading-status{margin-left:5px;color:#67c23a}.page-show-vue .history-loading-status.el-icon-circle-close{color:#f56c6c}.page-show-vue .el-timeline{-webkit-padding-start:0;padding-inline-start:0}.page-show-vue .comment-user-name{margin-bottom:10px}.page-show-vue .comment-content{padding:0;color:#666;margin:0;white-space:pre-wrap;word-wrap:break-word;line-height:20px}.page-show-vue .comment-input-box{position:absolute;bottom:0;width:100%;background:#fff;border-top:1px solid #f1f1f1}.page-show-vue .comment-input-box textarea{resize:none;width:100%;-webkit-box-sizing:border-box;box-sizing:border-box;border:0;outline:none!important;padding:10px}.page-show-vue .comment-card .comment-user-name .el-icon-delete{color:#888;font-size:13px;cursor:pointer;float:right;display:none}.page-show-vue .comment-card:hover .comment-user-name .el-icon-delete{display:inline-block}
|
||||
@@ -1 +1 @@
|
||||
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=favicon-wiki.png><title>WIKI文档管理系统</title><link href=css/chunk-009efa60.c1509684.css rel=prefetch><link href=css/chunk-0741282a.1750663f.css rel=prefetch><link href=css/chunk-07e56bb7.1eef05a2.css rel=prefetch><link href=css/chunk-32cc5643.5a5b2ca1.css rel=prefetch><link href=css/chunk-34407190.57bbfb51.css rel=prefetch><link href=css/chunk-4b763d50.7e74e765.css rel=prefetch><link href=css/chunk-5544a2b8.17d2b8a1.css rel=prefetch><link href=css/chunk-578c28a7.83c6d32d.css rel=prefetch><link href=css/chunk-5dc97978.ec6236ec.css rel=prefetch><link href=js/chunk-009efa60.9b21a8c7.js rel=prefetch><link href=js/chunk-0741282a.d128d142.js rel=prefetch><link href=js/chunk-07e56bb7.74268f53.js rel=prefetch><link href=js/chunk-2d207ece.20edf665.js rel=prefetch><link href=js/chunk-2d20f55a.217546ad.js rel=prefetch><link href=js/chunk-32cc5643.fcf57a84.js rel=prefetch><link href=js/chunk-34407190.69ced152.js rel=prefetch><link href=js/chunk-4b763d50.80332c84.js rel=prefetch><link href=js/chunk-5544a2b8.a272f03c.js rel=prefetch><link href=js/chunk-578c28a7.54a578cc.js rel=prefetch><link href=js/chunk-5dc97978.1238356c.js rel=prefetch><link href=css/app.d1dc89b9.css rel=preload as=style><link href=css/chunk-vendors.43fc3011.css rel=preload as=style><link href=js/app.8a617a58.js rel=preload as=script><link href=js/chunk-vendors.4d2ae4cf.js rel=preload as=script><link href=css/chunk-vendors.43fc3011.css rel=stylesheet><link href=css/app.d1dc89b9.css rel=stylesheet></head><body><noscript><strong>We're sorry but zyplayer-wiki-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=js/chunk-vendors.4d2ae4cf.js></script><script src=js/app.8a617a58.js></script></body></html>
|
||||
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=favicon-wiki.png><title>WIKI文档管理系统</title><link href=css/chunk-009efa60.c1509684.css rel=prefetch><link href=css/chunk-0741282a.1750663f.css rel=prefetch><link href=css/chunk-07e56bb7.63072588.css rel=prefetch><link href=css/chunk-32cc5643.5a5b2ca1.css rel=prefetch><link href=css/chunk-34407190.57bbfb51.css rel=prefetch><link href=css/chunk-4b763d50.7e74e765.css rel=prefetch><link href=css/chunk-5544a2b8.b3d0f39b.css rel=prefetch><link href=css/chunk-578c28a7.83c6d32d.css rel=prefetch><link href=css/chunk-5dc97978.ec6236ec.css rel=prefetch><link href=js/chunk-009efa60.dbe73c24.js rel=prefetch><link href=js/chunk-0741282a.d128d142.js rel=prefetch><link href=js/chunk-07e56bb7.e67dab8e.js rel=prefetch><link href=js/chunk-2d207ece.20edf665.js rel=prefetch><link href=js/chunk-2d20f55a.217546ad.js rel=prefetch><link href=js/chunk-32cc5643.fcf57a84.js rel=prefetch><link href=js/chunk-34407190.0d583c2d.js rel=prefetch><link href=js/chunk-4b763d50.1adcb2f6.js rel=prefetch><link href=js/chunk-5544a2b8.6714a421.js rel=prefetch><link href=js/chunk-578c28a7.6a10864d.js rel=prefetch><link href=js/chunk-5dc97978.14484106.js rel=prefetch><link href=css/app.e1e856e7.css rel=preload as=style><link href=css/chunk-vendors.43fc3011.css rel=preload as=style><link href=js/app.9c54b8f0.js rel=preload as=script><link href=js/chunk-vendors.4d2ae4cf.js rel=preload as=script><link href=css/chunk-vendors.43fc3011.css rel=stylesheet><link href=css/app.e1e856e7.css rel=stylesheet></head><body><noscript><strong>We're sorry but zyplayer-wiki-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=js/chunk-vendors.4d2ae4cf.js></script><script src=js/app.9c54b8f0.js></script></body></html>
|
||||
File diff suppressed because one or more lines are too long
1
zyplayer-doc-wiki/src/main/resources/js/app.9c54b8f0.js
Normal file
1
zyplayer-doc-wiki/src/main/resources/js/app.9c54b8f0.js
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user