diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index 4cb95b388..c399b794f 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -90,4 +90,11 @@ public interface ThemeService { * @param content new content */ void saveTemplateContent(String absolutePath, String content); + + /** + * Delete a theme by key. + * + * @param key theme key + */ + void deleteTheme(String key); } diff --git a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java index aa114c6c0..b4e2a0510 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -1,5 +1,6 @@ package run.halo.app.service.impl; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.file.FileReader; import cn.hutool.core.io.file.FileWriter; import cn.hutool.core.text.StrBuilder; @@ -239,4 +240,16 @@ public class ThemeServiceImpl implements ThemeService { final FileWriter fileWriter = new FileWriter(absolutePath); fileWriter.write(content); } + + /** + * Delete a theme by key. + * + * @param key theme key + */ + @Override + public void deleteTheme(String key) { + File file = new File(this.getThemeBasePath(),key); + FileUtil.del(file); + HaloConst.THEMES = this.getThemes(); + } } diff --git a/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java b/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java index a0a2db873..c34bcf4e6 100644 --- a/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java +++ b/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java @@ -74,12 +74,12 @@ public class ThemeController { } @GetMapping("files/custom") - public List customTemplate(){ + public List customTemplate() { return themeService.getCustomTpl(HaloConst.ACTIVATED_THEME_NAME); } /** - * Active theme + * Active theme. * * @param theme theme name * @throws TemplateModelException TemplateModelException @@ -95,4 +95,15 @@ public class ThemeController { configuration.setSharedVariable("themeName", theme); configuration.setSharedVariable("options", optionService.listOptions()); } + + /** + * Deletes a theme. + * + * @param key theme key + */ + @DeleteMapping("{key}") + @ApiOperation("Deletes a theme") + public void deleteBy(@PathVariable("key") String key) { + themeService.deleteTheme(key); + } }