From 82af70abccdde163419c4dd8c5e5f29576c10be5 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Thu, 4 Apr 2019 14:06:30 +0800 Subject: [PATCH] Complete admin theme api. --- .../run/halo/app/service/ThemeService.java | 17 ++++++++- .../app/service/impl/ThemeServiceImpl.java | 35 ++++++++++++++++--- .../controller/admin/api/ThemeController.java | 27 +++++++------- 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index ccb62901d..4cb95b388 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -3,7 +3,6 @@ package run.halo.app.service; import run.halo.app.model.support.Theme; import run.halo.app.model.support.ThemeFile; import run.halo.app.model.support.ThemeProperties; -import run.halo.app.model.support.ThemeFile; import java.io.File; import java.util.List; @@ -75,4 +74,20 @@ public interface ThemeService { * @return ThemeProperties */ ThemeProperties getProperties(File path); + + /** + * Get template content by template absolute path. + * + * @param absolutePath absolute path + * @return template content + */ + String getTemplateContent(String absolutePath); + + /** + * Save template content by template absolute path. + * + * @param absolutePath absolute path + * @param content new content + */ + void saveTemplateContent(String absolutePath, String content); } 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 3ce88491c..f19b9fbca 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,11 @@ package run.halo.app.service.impl; +import cn.hutool.core.io.file.FileReader; +import cn.hutool.core.io.file.FileWriter; +import cn.hutool.core.text.StrBuilder; +import cn.hutool.core.util.StrUtil; +import cn.hutool.setting.dialect.Props; +import org.springframework.stereotype.Service; import run.halo.app.config.properties.HaloProperties; import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.Theme; @@ -7,11 +13,6 @@ import run.halo.app.model.support.ThemeFile; import run.halo.app.model.support.ThemeProperties; import run.halo.app.service.ThemeService; import run.halo.app.utils.FilenameUtils; -import cn.hutool.core.text.StrBuilder; -import cn.hutool.core.util.StrUtil; -import cn.hutool.setting.dialect.Props; -import org.springframework.stereotype.Service; -import run.halo.app.model.support.ThemeFile; import java.io.File; import java.util.ArrayList; @@ -203,4 +204,28 @@ public class ThemeServiceImpl implements ThemeService { } return properties; } + + /** + * Get template content by template absolute path. + * + * @param absolutePath absolute path + * @return template content + */ + @Override + public String getTemplateContent(String absolutePath) { + final FileReader fileReader = new FileReader(absolutePath); + return fileReader.readString(); + } + + /** + * Save template content by template absolute path. + * + * @param absolutePath absolute path + * @param content new content + */ + @Override + public void saveTemplateContent(String absolutePath, String content) { + final FileWriter fileWriter = new FileWriter(absolutePath); + fileWriter.write(content); + } } 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 e8490bd1b..cd25df980 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 @@ -1,5 +1,9 @@ package run.halo.app.web.controller.admin.api; +import freemarker.template.Configuration; +import freemarker.template.TemplateModelException; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.*; import run.halo.app.model.enums.OptionSource; import run.halo.app.model.properties.PrimaryProperties; import run.halo.app.model.properties.PropertyEnum; @@ -8,18 +12,6 @@ import run.halo.app.model.support.Theme; import run.halo.app.model.support.ThemeFile; import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; -import freemarker.template.Configuration; -import freemarker.template.TemplateModelException; -import io.swagger.annotations.ApiOperation; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import run.halo.app.model.enums.OptionSource; -import run.halo.app.model.properties.PrimaryProperties; -import run.halo.app.model.support.Theme; -import run.halo.app.model.support.ThemeFile; -import run.halo.app.service.ThemeService; import java.util.HashMap; import java.util.List; @@ -70,6 +62,17 @@ public class ThemeController { return themeService.listThemeFolderBy(HaloConst.ACTIVATED_THEME_NAME); } + @GetMapping("files/content") + public String getContentBy(@RequestParam(name = "path") String path) { + return themeService.getTemplateContent(path); + } + + @PutMapping("files/content") + public void updateContentBy(@RequestParam(name = "path") String path, + @RequestParam(name = "content") String content) { + themeService.saveTemplateContent(path, content); + } + /** * Active theme *