diff --git a/src/main/java/cc/ryanc/halo/model/dto/ListPage.java b/src/main/java/cc/ryanc/halo/model/dto/ListPage.java index 9f89bb1b0..236819255 100644 --- a/src/main/java/cc/ryanc/halo/model/dto/ListPage.java +++ b/src/main/java/cc/ryanc/halo/model/dto/ListPage.java @@ -64,10 +64,10 @@ public class ListPage { this.nowPage = nowPage; this.totalCount = data.size(); this.totalPage = (totalCount + pageSize - 1) / pageSize; - this.prePage = nowPage-1>1? nowPage-1:1; - this.nextPage = nowPage>=totalPage? totalPage: nowPage + 1; - this.hasPrevious = nowPage!=prePage; - this.hasNext = nowPage!=nextPage; + this.prePage = nowPage - 1 > 1 ? nowPage - 1 : 1; + this.nextPage = nowPage >= totalPage ? totalPage : nowPage + 1; + this.hasPrevious = nowPage != prePage; + this.hasNext = nowPage != nextPage; } /** @@ -80,7 +80,7 @@ public class ListPage { if (fromIndex >= data.size()) { return Collections.emptyList(); } - if(fromIndex<0){ + if (fromIndex < 0) { return Collections.emptyList(); } int toIndex = nowPage * pageSize; diff --git a/src/main/java/cc/ryanc/halo/model/dto/Theme.java b/src/main/java/cc/ryanc/halo/model/dto/Theme.java index da90dcf33..4cc99cfbe 100644 --- a/src/main/java/cc/ryanc/halo/model/dto/Theme.java +++ b/src/main/java/cc/ryanc/halo/model/dto/Theme.java @@ -26,4 +26,9 @@ public class Theme implements Serializable { * 是否支持设置 */ private boolean hasOptions; + + /** + * 是否支持更新 + */ + private boolean hasUpdate; } diff --git a/src/main/java/cc/ryanc/halo/utils/HaloUtils.java b/src/main/java/cc/ryanc/halo/utils/HaloUtils.java index f7379d427..694af4256 100755 --- a/src/main/java/cc/ryanc/halo/utils/HaloUtils.java +++ b/src/main/java/cc/ryanc/halo/utils/HaloUtils.java @@ -170,6 +170,12 @@ public class HaloUtils { } else { theme.setHasOptions(false); } + File gitPath = new File(themesPath.getAbsolutePath(), file.getName() + "/.git"); + if (gitPath.exists()) { + theme.setHasUpdate(true); + } else { + theme.setHasUpdate(false); + } themes.add(theme); } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java index 3651c862d..95c8f1461 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java @@ -14,6 +14,7 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.file.FileReader; import cn.hutool.core.io.file.FileWriter; +import cn.hutool.core.util.RuntimeUtil; import cn.hutool.core.util.ZipUtil; import cn.hutool.extra.servlet.ServletUtil; import lombok.extern.slf4j.Slf4j; @@ -29,6 +30,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.websocket.server.PathParam; import java.io.File; +import java.io.FileNotFoundException; import java.util.List; /** @@ -50,6 +52,8 @@ public class ThemeController extends BaseController { @Autowired private LogsService logsService; + private static final String NOT_FOUND_GIT = "-bash: git: command not found"; + /** * 渲染主题设置页面 * @@ -149,6 +153,71 @@ public class ThemeController extends BaseController { return "redirect:/admin/themes"; } + /** + * 安装主题弹出层 + * + * @return 目录路径admin/widget/_theme-install + */ + @GetMapping(value = "/install") + public String install() { + return "admin/widget/_theme-install"; + } + + /** + * 在线拉取主题 + * + * @param remoteAddr 远程地址 + * @param themeName 主题名称 + * @return JsonResult + */ + @PostMapping(value = "/clone") + @ResponseBody + public JsonResult cloneFromRemote(@RequestParam(value = "remoteAddr") String remoteAddr, + @RequestParam(value = "themeName") String themeName) { + if (StringUtils.isBlank(remoteAddr) || StringUtils.isBlank(themeName)) { + return new JsonResult(0, "请输入完整信息!"); + } + try { + File basePath = new File(ResourceUtils.getURL("classpath:").getPath()); + File themePath = new File(basePath.getAbsolutePath(), "templates/themes"); + String cmdResult = RuntimeUtil.execForStr("git clone " + remoteAddr + " " + themePath.getAbsolutePath() + "/" + themeName); + if (NOT_FOUND_GIT.equals(cmdResult)) { + return new JsonResult(0, "没有安装Git!"); + } + HaloConst.THEMES.clear(); + HaloConst.THEMES = HaloUtils.getThemes(); + } catch (FileNotFoundException e) { + log.error("克隆主题失败:{}", e.getMessage()); + return new JsonResult(0, "克隆主题失败:" + e.getMessage()); + } + return new JsonResult(1, "安装成功!"); + } + + /** + * 更新主题 + * + * @param themeName 主题名 + * @return JsonResult + */ + @GetMapping(value = "/pull") + @ResponseBody + public JsonResult pullFromRemote(@RequestParam(value = "themeName") String themeName) { + try { + File basePath = new File(ResourceUtils.getURL("classpath:").getPath()); + File themePath = new File(basePath.getAbsolutePath(), "templates/themes"); + String cmdResult = RuntimeUtil.execForStr("cd " + themePath.getAbsolutePath() + "/" + themeName + " && git pull"); + if (NOT_FOUND_GIT.equals(cmdResult)) { + return new JsonResult(0, "没有安装Git!"); + } + HaloConst.THEMES.clear(); + HaloConst.THEMES = HaloUtils.getThemes(); + } catch (Exception e) { + log.error("更新主题失败:{}", e.getMessage()); + return new JsonResult(0, "更新主题失败:" + e.getMessage()); + } + return new JsonResult(1, "更新成功!"); + } + /** * 跳转到主题设置 * diff --git a/src/main/resources/templates/admin/admin_theme.ftl b/src/main/resources/templates/admin/admin_theme.ftl index 283eb7669..9d4f7850a 100755 --- a/src/main/resources/templates/admin/admin_theme.ftl +++ b/src/main/resources/templates/admin/admin_theme.ftl @@ -60,8 +60,8 @@

主题管理

- - 上传 + + 安装主题
-
-
-
-
- -
-
-
-
<#if themes?? && (themes?size>0)> <#list themes as theme> @@ -89,9 +80,12 @@
+ + + + + + + +