mirror of https://github.com/halo-dev/halo
feat: add error log when permalink exists (#2460)
#### What type of PR is this? /kind improvement /area core /milestone 2.0 #### What this PR does / why we need it: 注册 permalink 到 Permalinker 时校验 permalink 是否已经存在,如果存在则记录错误日志且不向 PermalinkIndexer 添加记录 TODO: - [ ] 后续增加事件功能后检查到 permalink 重复则发送记录事件的 event 来代替日志记录 #### Which issue(s) this PR fixes: A part of #2454 #### Special notes for your reviewer: /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note None ```pull/2474/head
parent
721cddff5d
commit
264a4330c3
|
@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
@ -17,6 +18,7 @@ import run.halo.app.extension.GroupVersionKind;
|
||||||
* @author guqing
|
* @author guqing
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class PermalinkIndexer {
|
public class PermalinkIndexer {
|
||||||
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
|
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
|
||||||
|
@ -173,8 +175,20 @@ public class PermalinkIndexer {
|
||||||
return permalinkLocatorLookup.size();
|
return permalinkLocatorLookup.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a record to the {@link PermalinkIndexer}.
|
||||||
|
* If permalink already exists, it will not be added to indexer
|
||||||
|
*
|
||||||
|
* @param addCommand a command to add a record to {@link PermalinkIndexer}
|
||||||
|
*/
|
||||||
@EventListener(PermalinkIndexAddCommand.class)
|
@EventListener(PermalinkIndexAddCommand.class)
|
||||||
public void onPermalinkAdd(PermalinkIndexAddCommand addCommand) {
|
public void onPermalinkAdd(PermalinkIndexAddCommand addCommand) {
|
||||||
|
if (checkPermalinkExists(addCommand.getLocator(), addCommand.getPermalink())) {
|
||||||
|
// TODO send an extension event to log this error
|
||||||
|
log.error("Permalink [{}] already exists, you can try to change the slug [{}].",
|
||||||
|
addCommand.getPermalink(), addCommand.getLocator());
|
||||||
|
return;
|
||||||
|
}
|
||||||
register(addCommand.getLocator(), addCommand.getPermalink());
|
register(addCommand.getLocator(), addCommand.getPermalink());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,9 +197,26 @@ public class PermalinkIndexer {
|
||||||
remove(deleteCommand.getLocator());
|
remove(deleteCommand.getLocator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a {@link PermalinkIndexer} record by {@link ExtensionLocator} and permalink.
|
||||||
|
* If permalink already exists, it will not be updated
|
||||||
|
*
|
||||||
|
* @param updateCommand a command to update an indexer record
|
||||||
|
*/
|
||||||
@EventListener(PermalinkIndexUpdateCommand.class)
|
@EventListener(PermalinkIndexUpdateCommand.class)
|
||||||
public void onPermalinkUpdate(PermalinkIndexUpdateCommand updateCommand) {
|
public void onPermalinkUpdate(PermalinkIndexUpdateCommand updateCommand) {
|
||||||
|
if (checkPermalinkExists(updateCommand.getLocator(), updateCommand.getPermalink())) {
|
||||||
|
// TODO send an extension event to log this error
|
||||||
|
log.error("Permalink [{}] already exists, you can try to change the slug [{}].",
|
||||||
|
updateCommand.getPermalink(), updateCommand.getLocator());
|
||||||
|
return;
|
||||||
|
}
|
||||||
remove(updateCommand.getLocator());
|
remove(updateCommand.getLocator());
|
||||||
register(updateCommand.getLocator(), updateCommand.getPermalink());
|
register(updateCommand.getLocator(), updateCommand.getPermalink());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean checkPermalinkExists(ExtensionLocator locator, String permalink) {
|
||||||
|
ExtensionLocator lookup = lookup(permalink);
|
||||||
|
return lookup != null && !lookup.equals(locator);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue