Add support back for using relative permalink (#7475)

#### What type of PR is this?

/kind bug
/area core
/milestone 2.21.x

#### What this PR does / why we need it:

This PR fixes the problem of not working for relative permalink caused by <https://github.com/halo-dev/halo/pull/7459>.

#### Special notes for your reviewer:

1. Try to start Halo instance with `halo.use-absolute-permalink=false` and check the permalinks of posts and attachments.
1. Try to start Halo instance with `halo.use-absolute-permalink=true` and check the permalinks of posts and attachments.

#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/7469/head
John Niang 2025-05-26 18:12:18 +08:00 committed by GitHub
parent a0dc9590c2
commit d706b5eb6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions

View File

@ -63,13 +63,12 @@ class SystemConfigFirstExternalUrlSupplier implements ExternalUrlSupplier {
@Override @Override
public URI get() { public URI get() {
try { try {
if (externalUrl != null) {
return externalUrl.toURI();
}
if (!haloProperties.isUseAbsolutePermalink()) { if (!haloProperties.isUseAbsolutePermalink()) {
return URI.create(getBasePath()); return URI.create(getBasePath());
} }
if (externalUrl != null) {
return externalUrl.toURI();
}
return haloProperties.getExternalUrl().toURI(); return haloProperties.getExternalUrl().toURI();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw Exceptions.propagate(e); throw Exceptions.propagate(e);

View File

@ -55,7 +55,7 @@ class SystemConfigFirstExternalUrlSupplierTest {
} }
@Test @Test
void getURIWhenBasePathSetAndNotUsingAbsolutePermalink() throws MalformedURLException { void getURIWhenBasePathSetAndNotUsingAbsolutePermalink() {
when(webFluxProperties.getBasePath()).thenReturn("/blog"); when(webFluxProperties.getBasePath()).thenReturn("/blog");
when(haloProperties.isUseAbsolutePermalink()).thenReturn(false); when(haloProperties.isUseAbsolutePermalink()).thenReturn(false);
@ -137,10 +137,11 @@ class SystemConfigFirstExternalUrlSupplierTest {
class SystemConfigSupplier { class SystemConfigSupplier {
@Test @Test
void shouldGetUrlCorrectly() throws Exception { void shouldGetUrlWhenUseAbsolutePermalink() throws Exception {
var basic = new SystemSetting.Basic(); var basic = new SystemSetting.Basic();
basic.setExternalUrl("https://www.halo.run"); basic.setExternalUrl("https://www.halo.run");
when(systemConfigFetcher.getBasic()).thenReturn(Mono.just(basic)); when(systemConfigFetcher.getBasic()).thenReturn(Mono.just(basic));
when(haloProperties.isUseAbsolutePermalink()).thenReturn(true);
externalUrl.onExtensionInitialized(null); externalUrl.onExtensionInitialized(null);
assertEquals(URI.create("https://www.halo.run").toURL(), externalUrl.getRaw()); assertEquals(URI.create("https://www.halo.run").toURL(), externalUrl.getRaw());
assertEquals(URI.create("https://www.halo.run"), externalUrl.get()); assertEquals(URI.create("https://www.halo.run"), externalUrl.get());
@ -150,6 +151,22 @@ class SystemConfigFirstExternalUrlSupplierTest {
externalUrl.getURL(mockRequest)); externalUrl.getURL(mockRequest));
} }
@Test
void shouldGetUrlWhenNotUsingAbsolutePermalink() throws MalformedURLException {
var basic = new SystemSetting.Basic();
basic.setExternalUrl("https://www.halo.run");
when(systemConfigFetcher.getBasic()).thenReturn(Mono.just(basic));
when(haloProperties.isUseAbsolutePermalink()).thenReturn(false);
when(webFluxProperties.getBasePath()).thenReturn("/fake");
externalUrl.onExtensionInitialized(null);
assertEquals(URI.create("https://www.halo.run").toURL(), externalUrl.getRaw());
assertEquals(URI.create("/fake"), externalUrl.get());
var mockRequest = mock(HttpRequest.class);
assertEquals(URI.create("https://www.halo.run").toURL(),
externalUrl.getURL(mockRequest));
}
} }
} }