-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: cannot be accessed for a long time during startup
- Loading branch information
Showing
82 changed files
with
721 additions
and
3,917 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 20 additions & 49 deletions
69
src/main/java/run/halo/app/content/permalinks/TagPermalinkPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,47 @@ | ||
package run.halo.app.content.permalinks; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import org.springframework.context.ApplicationContext; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriUtils; | ||
import run.halo.app.core.extension.content.Constant; | ||
import run.halo.app.core.extension.content.Tag; | ||
import run.halo.app.extension.GroupVersionKind; | ||
import run.halo.app.extension.ExtensionUtil; | ||
import run.halo.app.infra.ExternalUrlSupplier; | ||
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher; | ||
import run.halo.app.infra.SystemSetting; | ||
import run.halo.app.infra.utils.PathUtils; | ||
import run.halo.app.theme.DefaultTemplateEnum; | ||
import run.halo.app.theme.router.PermalinkIndexAddCommand; | ||
import run.halo.app.theme.router.PermalinkIndexDeleteCommand; | ||
import run.halo.app.theme.router.PermalinkIndexUpdateCommand; | ||
import run.halo.app.theme.router.PermalinkPatternProvider; | ||
import run.halo.app.theme.router.PermalinkWatch; | ||
|
||
/** | ||
* @author guqing | ||
* @since 2.0.0 | ||
*/ | ||
@Component | ||
public class TagPermalinkPolicy implements PermalinkPolicy<Tag>, PermalinkWatch<Tag> { | ||
private final GroupVersionKind gvk = GroupVersionKind.fromExtension(Tag.class); | ||
private final PermalinkPatternProvider permalinkPatternProvider; | ||
private final ApplicationContext applicationContext; | ||
|
||
@RequiredArgsConstructor | ||
public class TagPermalinkPolicy implements PermalinkPolicy<Tag> { | ||
public static final String DEFAULT_PERMALINK_PREFIX = | ||
SystemSetting.ThemeRouteRules.empty().getTags(); | ||
private final ExternalUrlSupplier externalUrlSupplier; | ||
|
||
public TagPermalinkPolicy(PermalinkPatternProvider permalinkPatternProvider, | ||
ApplicationContext applicationContext, ExternalUrlSupplier externalUrlSupplier) { | ||
this.permalinkPatternProvider = permalinkPatternProvider; | ||
this.applicationContext = applicationContext; | ||
this.externalUrlSupplier = externalUrlSupplier; | ||
} | ||
private final SystemConfigurableEnvironmentFetcher environmentFetcher; | ||
|
||
@Override | ||
public String permalink(Tag tag) { | ||
Map<String, String> annotations = ExtensionUtil.nullSafeAnnotations(tag); | ||
String permalinkPrefix = | ||
annotations.getOrDefault(Constant.PERMALINK_PATTERN_ANNO, DEFAULT_PERMALINK_PREFIX); | ||
|
||
String slug = UriUtils.encode(tag.getSpec().getSlug(), StandardCharsets.UTF_8); | ||
String path = PathUtils.combinePath(pattern(), slug); | ||
String path = PathUtils.combinePath(permalinkPrefix, slug); | ||
return externalUrlSupplier.get() | ||
.resolve(path) | ||
.normalize().toString(); | ||
} | ||
|
||
@Override | ||
public String templateName() { | ||
return DefaultTemplateEnum.TAG.getValue(); | ||
} | ||
|
||
@Override | ||
public String pattern() { | ||
return permalinkPatternProvider.getPattern(DefaultTemplateEnum.TAG); | ||
} | ||
|
||
@Override | ||
public void onPermalinkAdd(Tag tag) { | ||
applicationContext.publishEvent(new PermalinkIndexAddCommand(this, getLocator(tag), | ||
tag.getStatusOrDefault().getPermalink())); | ||
} | ||
|
||
@Override | ||
public void onPermalinkUpdate(Tag tag) { | ||
applicationContext.publishEvent(new PermalinkIndexUpdateCommand(this, getLocator(tag), | ||
tag.getStatusOrDefault().getPermalink())); | ||
} | ||
|
||
@Override | ||
public void onPermalinkDelete(Tag tag) { | ||
applicationContext.publishEvent(new PermalinkIndexDeleteCommand(this, getLocator(tag))); | ||
} | ||
|
||
private ExtensionLocator getLocator(Tag tag) { | ||
return new ExtensionLocator(gvk, tag.getMetadata().getName(), tag.getSpec().getSlug()); | ||
return environmentFetcher.fetchRouteRules() | ||
.map(SystemSetting.ThemeRouteRules::getTags) | ||
.blockOptional() | ||
.orElse(DEFAULT_PERMALINK_PREFIX); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.