use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.
the class ThemeRepositoryImplTest method getActivatedThemeBySingleThread.
@Test
void getActivatedThemeBySingleThread() {
ThemeProperty expectedTheme = new ThemeProperty();
expectedTheme.setId(HaloConst.DEFAULT_THEME_ID);
expectedTheme.setActivated(true);
given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty());
doReturn(Optional.of(expectedTheme)).when(themeRepository).fetchThemePropertyByThemeId(HaloConst.DEFAULT_THEME_ID);
ThemeProperty resultTheme = themeRepository.getActivatedThemeProperty();
assertEquals(expectedTheme, resultTheme);
verify(optionRepository, times(1)).findByKey(any());
verify(themeRepository, times(1)).fetchThemePropertyByThemeId(any());
}
use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.
the class ThemeRepositoryImplTest method getActivatedThemeByMultiThread.
@Test
void getActivatedThemeByMultiThread() throws InterruptedException {
ThemeProperty expectedTheme = new ThemeProperty();
expectedTheme.setId(HaloConst.DEFAULT_THEME_ID);
expectedTheme.setActivated(true);
given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty());
doReturn(Optional.of(expectedTheme)).when(themeRepository).fetchThemePropertyByThemeId(HaloConst.DEFAULT_THEME_ID);
ExecutorService executorService = Executors.newFixedThreadPool(10);
// define tasks
List<Callable<ThemeProperty>> tasks = IntStream.range(0, 10).mapToObj(i -> (Callable<ThemeProperty>) () -> themeRepository.getActivatedThemeProperty()).collect(Collectors.toList());
// invoke and get results
executorService.invokeAll(tasks).forEach(future -> {
try {
assertEquals(expectedTheme, future.get(100, TimeUnit.MILLISECONDS));
} catch (Exception e) {
throw new RuntimeException("Failed to get task result!", e);
}
});
verify(optionRepository, times(1)).findByKey(any());
verify(themeRepository, times(1)).fetchThemePropertyByThemeId(any());
}
use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.
the class ThemeServiceImpl method fetchConfig.
@Override
@NonNull
public List<Group> fetchConfig(@NonNull String themeId) {
Assert.hasText(themeId, "Theme id must not be blank");
// Get theme property
ThemeProperty themeProperty = getThemeOfNonNullBy(themeId);
if (!themeProperty.isHasOptions()) {
// If this theme dose not has an option, then return empty list
return Collections.emptyList();
}
try {
for (String optionsName : SETTINGS_NAMES) {
// Resolve the options path
Path optionsPath = Paths.get(themeProperty.getThemePath(), optionsName);
log.debug("Finding options in: [{}]", optionsPath.toString());
// Check existence
if (!Files.exists(optionsPath)) {
continue;
}
// Read the yaml file
String optionContent = Files.readString(optionsPath);
// Resolve it
return themeConfigResolver.resolve(optionContent);
}
return Collections.emptyList();
} catch (IOException e) {
throw new ServiceException("读取主题配置文件失败", e);
}
}
use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.
the class ThemeServiceImpl method checkDirectory.
/**
* Check if directory is valid or not.
*
* @param themeId themeId must not be blank
* @param absoluteName throws when the given absolute directory name is invalid
*/
private void checkDirectory(@NonNull String themeId, @NonNull String absoluteName) {
ThemeProperty themeProperty = getThemeOfNonNullBy(themeId);
FileUtils.checkDirectoryTraversal(themeProperty.getThemePath(), absoluteName);
}
use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.
the class ThemeServiceImpl method deleteTheme.
@Transactional
@Override
public void deleteTheme(@NonNull String themeId, @NonNull Boolean deleteSettings) {
// Get the theme property
ThemeProperty themeProperty = getThemeOfNonNullBy(themeId);
if (themeId.equals(getActivatedThemeId())) {
// Prevent to delete the activated theme
throw new BadRequestException("无法删除正在使用的主题!").setErrorData(themeId);
}
try {
// Delete the folder
FileUtils.deleteFolder(Paths.get(themeProperty.getThemePath()));
if (deleteSettings) {
// Delete theme settings
themeSettingRepository.deleteByThemeId(themeId);
}
// Delete theme cache
eventPublisher.publishEvent(new ThemeUpdatedEvent(this));
} catch (Exception e) {
throw new ServiceException("主题删除失败", e).setErrorData(themeId);
}
}
Aggregations