Skip to content

Commit

Permalink
Merge branch 'main' into refactor/3353
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnNiang authored Feb 24, 2023
2 parents bcc77da + f1ec6ce commit 9489066
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
import run.halo.app.core.extension.Plugin;
import run.halo.app.core.extension.ReverseProxy;
import run.halo.app.core.extension.Setting;
Expand Down Expand Up @@ -108,27 +110,36 @@ boolean readinessDetection(String name) {
}
createInitialReverseProxyIfNotPresent(plugin);

Plugin.PluginStatus status = plugin.statusNonNull();

// filled logo path
String logo = plugin.getSpec().getLogo();
if (PathUtils.isAbsoluteUri(logo)) {
status.setLogo(logo);
} else {
String assetsPrefix =
PluginConst.assertsRoutePrefix(plugin.getMetadata().getName());
status.setLogo(PathUtils.combinePath(assetsPrefix, logo));
}
generateAccessibleLogoUrl(plugin);

// update phase
PluginWrapper pluginWrapper = getPluginWrapper(name);
Plugin.PluginStatus status = plugin.statusNonNull();
status.setPhase(pluginWrapper.getPluginState());
updateStatus(plugin.getMetadata().getName(), status);
return false;
})
.orElse(false);
}

void generateAccessibleLogoUrl(Plugin plugin) {
String logo = plugin.getSpec().getLogo();
if (StringUtils.isBlank(logo)) {
return;
}
Plugin.PluginStatus status = plugin.statusNonNull();
if (PathUtils.isAbsoluteUri(logo)) {
status.setLogo(logo);
} else {
String assetsPrefix =
PluginConst.assertsRoutePrefix(plugin.getMetadata().getName());
String versionedLogo =
applyVersioningToStaticResource(logo, plugin.getSpec().getVersion());
status.setLogo(PathUtils.combinePath(assetsPrefix, versionedLogo));
}
}

Optional<Setting> lookupPluginSetting(String name, String settingName) {
Assert.notNull(name, "Plugin name must not be null");
Assert.notNull(settingName, "Setting name must not be null");
Expand Down Expand Up @@ -374,12 +385,15 @@ void doStart(String name) {

plugin.statusNonNull().setLastStartTime(Instant.now());

final String pluginVersion = plugin.getSpec().getVersion();
String jsBundlePath =
BundleResourceUtils.getJsBundlePath(haloPluginManager, name);
jsBundlePath = applyVersioningToStaticResource(jsBundlePath, pluginVersion);
status.setEntry(jsBundlePath);

String cssBundlePath =
BundleResourceUtils.getCssBundlePath(haloPluginManager, name);
cssBundlePath = applyVersioningToStaticResource(cssBundlePath, pluginVersion);
status.setStylesheet(cssBundlePath);

status.setPhase(currentState);
Expand All @@ -398,6 +412,15 @@ void doStart(String name) {
});
}

private String applyVersioningToStaticResource(@Nullable String path, String pluginVersion) {
if (StringUtils.isNotBlank(path)) {
return UriComponentsBuilder.fromUriString(path)
.queryParam("version", pluginVersion)
.build().toString();
}
return path;
}

PluginStartingError getStaringErrorInfo(String name) {
PluginStartingError startingError =
haloPluginManager.getPluginStartingError(name);
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/extensions/user.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ apiVersion: v1alpha1
kind: User
metadata:
name: anonymousUser
finalizers:
- system-protection
spec:
displayName: Anonymous User
email: anonymous@example.com
Expand All @@ -12,6 +14,8 @@ apiVersion: v1alpha1
kind: User
metadata:
name: ghost
finalizers:
- system-protection
spec:
displayName: 已删除用户
email: ghost@example.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.json.JSONException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -287,6 +288,78 @@ void createInitialReverseProxyWhenExist() {
verify(extensionClient, times(1)).update(any());
}

@Nested
class PluginLogoTest {

@Test
void absoluteUri() {
Plugin plugin = new Plugin();
plugin.setSpec(new Plugin.PluginSpec());
plugin.getSpec().setLogo("https://example.com/logo.png");
plugin.getSpec().setVersion("1.0.0");
pluginReconciler.generateAccessibleLogoUrl(plugin);
assertThat(plugin.statusNonNull().getLogo())
.isEqualTo("https://example.com/logo.png");
}

@Test
void absoluteUriWithQueryParam() {
Plugin plugin = new Plugin();
plugin.setSpec(new Plugin.PluginSpec());
plugin.getSpec().setLogo("https://example.com/logo.png?hello=world");
plugin.getSpec().setVersion("1.0.0");
pluginReconciler.generateAccessibleLogoUrl(plugin);
assertThat(plugin.statusNonNull().getLogo())
.isEqualTo("https://example.com/logo.png?hello=world");
}

@Test
void logoIsNull() {
Plugin plugin = new Plugin();
plugin.setSpec(new Plugin.PluginSpec());
plugin.getSpec().setLogo(null);
plugin.getSpec().setVersion("1.0.0");
pluginReconciler.generateAccessibleLogoUrl(plugin);
assertThat(plugin.statusNonNull().getLogo()).isNull();
}

@Test
void logoIsEmpty() {
Plugin plugin = new Plugin();
plugin.setSpec(new Plugin.PluginSpec());
plugin.getSpec().setLogo("");
plugin.getSpec().setVersion("1.0.0");
pluginReconciler.generateAccessibleLogoUrl(plugin);
assertThat(plugin.statusNonNull().getLogo()).isNull();
}

@Test
void relativePath() {
Plugin plugin = new Plugin();
plugin.setSpec(new Plugin.PluginSpec());
plugin.setMetadata(new Metadata());
plugin.getMetadata().setName("fake-plugin");
plugin.getSpec().setLogo("/static/logo.jpg");
plugin.getSpec().setVersion("1.0.0");
pluginReconciler.generateAccessibleLogoUrl(plugin);
assertThat(plugin.statusNonNull().getLogo())
.isEqualTo("/plugins/fake-plugin/assets/static/logo.jpg?version=1.0.0");
}

@Test
void dataBlob() {
Plugin plugin = new Plugin();
plugin.setSpec(new Plugin.PluginSpec());
plugin.setMetadata(new Metadata());
plugin.getMetadata().setName("fake-plugin");
plugin.getSpec().setLogo("data:image/gif;base64,R0lGODfake");
plugin.getSpec().setVersion("2.0.0");
pluginReconciler.generateAccessibleLogoUrl(plugin);
assertThat(plugin.statusNonNull().getLogo())
.isEqualTo("data:image/gif;base64,R0lGODfake");
}
}

private ArgumentCaptor<Plugin> doReconcileNeedRequeue() {
ArgumentCaptor<Plugin> pluginCaptor = ArgumentCaptor.forClass(Plugin.class);
doNothing().when(extensionClient).update(pluginCaptor.capture());
Expand Down

0 comments on commit 9489066

Please sign in to comment.