Complete admin theme api.

pull/146/head
ruibaby 2019-04-04 14:06:30 +08:00
parent 94eacf1da6
commit 82af70abcc
3 changed files with 61 additions and 18 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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
*