Refactor theme service

pull/146/head
johnniang 2019-04-30 15:58:39 +08:00
parent e4e36cbfe5
commit 6fcf1a6952
3 changed files with 33 additions and 44 deletions

View File

@ -65,8 +65,8 @@ public class ThemeController {
} }
@GetMapping("files/custom") @GetMapping("files/custom")
public List<String> customTemplate() { public Set<String> customTemplate() {
return themeService.getCustomTpl(themeService.getActivatedThemeId()); return themeService.listCustomTemplates(themeService.getActivatedThemeId());
} }
@PostMapping("{themeId}/activation") @PostMapping("{themeId}/activation")

View File

@ -66,6 +66,8 @@ public interface ThemeService {
*/ */
String THEMES_CACHE_KEY = "themes"; String THEMES_CACHE_KEY = "themes";
String CUSTOM_SHEET_PREFIX = "sheet_";
/** /**
* Get theme property by theme id. * Get theme property by theme id.
* *
@ -109,12 +111,12 @@ public interface ThemeService {
List<ThemeFile> listThemeFolderBy(@NonNull String themeId); List<ThemeFile> listThemeFolderBy(@NonNull String themeId);
/** /**
* Gets custom template, such as page_xxx.ftl, and xxx will be template name * Lists a set of custom template, such as sheet_xxx.ftl, and xxx will be template name
* *
* @param themeId theme id * @param themeId theme id must not be blank
* @return List * @return a set of templates
*/ */
List<String> getCustomTpl(@NonNull String themeId); Set<String> listCustomTemplates(@NonNull String themeId);
/** /**
* Judging whether template exists under the specified theme * Judging whether template exists under the specified theme
@ -130,14 +132,7 @@ public interface ThemeService {
* @param themeId theme name * @param themeId theme name
* @return boolean * @return boolean
*/ */
boolean isThemeExist(@NonNull String themeId); boolean themeExists(@NonNull String themeId);
/**
* Gets theme base path.
*
* @return File
*/
File getThemeBasePath();
/** /**
* Gets theme base path. * Gets theme base path.

View File

@ -3,8 +3,6 @@ package run.halo.app.service.impl;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader; import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter; import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
@ -38,7 +36,6 @@ import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.HaloUtils; import run.halo.app.utils.HaloUtils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -60,14 +57,12 @@ public class ThemeServiceImpl implements ThemeService {
/** /**
* Theme work directory. * Theme work directory.
*/ */
private final Path workDir; private final Path themeWorkDir;
private final OptionService optionService; private final OptionService optionService;
private final StringCacheStore cacheStore; private final StringCacheStore cacheStore;
private final Configuration configuration;
private final ThemeConfigResolver themeConfigResolver; private final ThemeConfigResolver themeConfigResolver;
private final ThemePropertyResolver themePropertyResolver; private final ThemePropertyResolver themePropertyResolver;
@ -89,19 +84,17 @@ public class ThemeServiceImpl implements ThemeService {
public ThemeServiceImpl(HaloProperties haloProperties, public ThemeServiceImpl(HaloProperties haloProperties,
OptionService optionService, OptionService optionService,
StringCacheStore cacheStore, StringCacheStore cacheStore,
Configuration configuration,
ThemeConfigResolver themeConfigResolver, ThemeConfigResolver themeConfigResolver,
ThemePropertyResolver themePropertyResolver, ThemePropertyResolver themePropertyResolver,
RestTemplate restTemplate, RestTemplate restTemplate,
ApplicationEventPublisher eventPublisher) { ApplicationEventPublisher eventPublisher) {
this.optionService = optionService; this.optionService = optionService;
this.cacheStore = cacheStore; this.cacheStore = cacheStore;
this.configuration = configuration;
this.themeConfigResolver = themeConfigResolver; this.themeConfigResolver = themeConfigResolver;
this.themePropertyResolver = themePropertyResolver; this.themePropertyResolver = themePropertyResolver;
this.restTemplate = restTemplate; this.restTemplate = restTemplate;
workDir = Paths.get(haloProperties.getWorkDir(), THEME_FOLDER); themeWorkDir = Paths.get(haloProperties.getWorkDir(), THEME_FOLDER);
this.eventPublisher = eventPublisher; this.eventPublisher = eventPublisher;
} }
@ -114,8 +107,10 @@ public class ThemeServiceImpl implements ThemeService {
public Optional<ThemeProperty> getThemeBy(String themeId) { public Optional<ThemeProperty> getThemeBy(String themeId) {
Assert.hasText(themeId, "Theme id must not be blank"); Assert.hasText(themeId, "Theme id must not be blank");
// Get all themes
Set<ThemeProperty> themes = getThemes(); Set<ThemeProperty> themes = getThemes();
// filter and find first
return themes.stream().filter(themeProperty -> StringUtils.equals(themeProperty.getId(), themeId)).findFirst(); return themes.stream().filter(themeProperty -> StringUtils.equals(themeProperty.getId(), themeId)).findFirst();
} }
@ -165,19 +160,23 @@ public class ThemeServiceImpl implements ThemeService {
} }
@Override @Override
public List<String> getCustomTpl(String themeId) { public Set<String> listCustomTemplates(String themeId) {
List<String> templates = new ArrayList<>(); // Get the theme path
File themePath = new File(getThemeBasePath(), themeId); Path themePath = Paths.get(getThemeOfNonNullBy(themeId).getThemePath());
File[] themeFiles = themePath.listFiles();
if (null != themeFiles && themeFiles.length > 0) { try {
for (File file : themeFiles) { return Files.list(themePath)
String[] split = StrUtil.removeSuffix(file.getName(), HaloConst.SUFFIX_FTL).split("_"); .filter(path -> StringUtils.startsWithIgnoreCase(path.getFileName().toString(), CUSTOM_SHEET_PREFIX))
if (split.length == 2 && "sheet".equals(split[0])) { .map(path -> {
templates.add(StrUtil.removeSuffix(file.getName(), HaloConst.SUFFIX_FTL)); // Remove prefix
} String customTemplate = StringUtils.removeStartIgnoreCase(path.getFileName().toString(), CUSTOM_SHEET_PREFIX);
} // Remove suffix
return StringUtils.removeEndIgnoreCase(customTemplate, HaloConst.SUFFIX_FTL);
})
.collect(Collectors.toSet());
} catch (IOException e) {
throw new ServiceException("Failed to list files of path " + themePath.toString(), e);
} }
return templates;
} }
@Override @Override
@ -185,7 +184,7 @@ public class ThemeServiceImpl implements ThemeService {
Assert.hasText(template, "Template must not be blank"); Assert.hasText(template, "Template must not be blank");
// Resolve template path // Resolve template path
Path templatePath = getBasePath().resolve(getActivatedTheme().getFolderName()).resolve(template); Path templatePath = Paths.get(getActivatedTheme().getThemePath(), template);
// Check the directory // Check the directory
checkDirectory(templatePath.toString()); checkDirectory(templatePath.toString());
@ -195,18 +194,13 @@ public class ThemeServiceImpl implements ThemeService {
} }
@Override @Override
public boolean isThemeExist(String themeId) { public boolean themeExists(String themeId) {
return getThemeBy(themeId).isPresent(); return getThemeBy(themeId).isPresent();
} }
@Override
public File getThemeBasePath() {
return getBasePath().toFile();
}
@Override @Override
public Path getBasePath() { public Path getBasePath() {
return workDir; return themeWorkDir;
} }
@Override @Override
@ -400,7 +394,7 @@ public class ThemeServiceImpl implements ThemeService {
} }
// Copy the temporary path to current theme folder // Copy the temporary path to current theme folder
Path targetThemePath = workDir.resolve(tmpThemeProperty.getId()); Path targetThemePath = themeWorkDir.resolve(tmpThemeProperty.getId());
FileUtils.copyFolder(themeTmpPath, targetThemePath); FileUtils.copyFolder(themeTmpPath, targetThemePath);
// Get property again // Get property again