diff --git a/src/main/java/run/halo/app/model/params/AttachmentParam.java b/src/main/java/run/halo/app/model/params/AttachmentParam.java index 1760d5691..0d13a4ea4 100644 --- a/src/main/java/run/halo/app/model/params/AttachmentParam.java +++ b/src/main/java/run/halo/app/model/params/AttachmentParam.java @@ -5,6 +5,7 @@ import run.halo.app.model.dto.base.InputConverter; import run.halo.app.model.entity.Attachment; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; /** * Attachment params. @@ -16,5 +17,7 @@ import javax.validation.constraints.NotBlank; public class AttachmentParam implements InputConverter { @NotBlank(message = "Attachment name must not be blank") + @Size(max = 255, message = "Length of attachment name must not be more than {max}") private String name; + } diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index fbb4a8b48..7cb3c365f 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -195,6 +195,14 @@ public interface ThemeService { @NonNull String getActivatedThemeId(); + /** + * Gets activated theme property. + * + * @return activated theme property + */ + @NonNull + ThemeProperty getActivatedTheme(); + /** * Actives a theme. * 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 6ff12b0a8..4a4a7f882 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -79,6 +79,11 @@ public class ThemeServiceImpl implements ThemeService { */ private String activatedThemeId; + /** + * Activated theme property. + */ + private ThemeProperty activatedTheme; + public ThemeServiceImpl(HaloProperties haloProperties, OptionService optionService, StringCacheStore cacheStore, @@ -275,15 +280,20 @@ public class ThemeServiceImpl implements ThemeService { @Override public String render(String pageName) { - return String.format(RENDER_TEMPLATE, getActivatedThemeId(), pageName); + // Get activated theme + ThemeProperty activatedTheme = getActivatedTheme(); + // Get theme folder name + String themeFolderName = Paths.get(activatedTheme.getThemePath()).getFileName().toString(); + // Build render url + return String.format(RENDER_TEMPLATE, themeFolderName, pageName); } @Override public String getActivatedThemeId() { - if (StringUtils.isBlank(activatedThemeId)) { + if (activatedThemeId == null) { synchronized (this) { - if (StringUtils.isBlank(activatedThemeId)) { - return optionService.getByProperty(PrimaryProperties.THEME).orElse(DEFAULT_THEME_ID); + if (activatedThemeId == null) { + activatedThemeId = optionService.getByProperty(PrimaryProperties.THEME).orElse(DEFAULT_THEME_ID); } } } @@ -291,6 +301,20 @@ public class ThemeServiceImpl implements ThemeService { return activatedThemeId; } + @Override + public ThemeProperty getActivatedTheme() { + if (activatedTheme == null) { + synchronized (this) { + if (activatedTheme == null) { + // Get theme property + activatedTheme = getThemeOfNonNullBy(getActivatedThemeId()); + } + } + } + + return activatedTheme; + } + @Override public ThemeProperty activeTheme(String themeId) { // Check existence of the theme @@ -299,9 +323,8 @@ public class ThemeServiceImpl implements ThemeService { // Save the theme to database optionService.saveProperty(PrimaryProperties.THEME, themeId); - - // Set the activated theme id - setActivatedThemeId(themeId); + // Set activated theme + setActivatedTheme(themeProperty); // Clear the cache clearThemeCache(); @@ -404,8 +427,6 @@ public class ThemeServiceImpl implements ThemeService { cloneFromGit(uri, themeTmpPath); } else { downloadZipAndUnzip(uri, themeTmpPath); -// } else { -// throw new UnsupportedMediaTypeException("Unsupported download type: " + uri); } return add(themeTmpPath); @@ -486,12 +507,12 @@ public class ThemeServiceImpl implements ThemeService { } /** - * Set activated theme id. + * Sets activated theme. * - * @param themeId theme id + * @param activatedTheme activated theme */ - private void setActivatedThemeId(@Nullable String themeId) { - this.activatedThemeId = themeId; + private void setActivatedTheme(@Nullable ThemeProperty activatedTheme) { + this.activatedTheme = activatedTheme; } /**