Skip to content

Commit

Permalink
[release-2.16] Fix the problem that theme assets could not be found a…
Browse files Browse the repository at this point in the history
…fter the first initialization (#6050)

This is an automated cherry-pick of #6049

/assign JohnNiang

```release-note
修复首次初始化后无法正常访问主题资源的问题
```
  • Loading branch information
halo-dev-bot authored Jun 7, 2024
1 parent 0bd8611 commit 7b2c8e4
Showing 1 changed file with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package run.halo.app.theme.config;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.stereotype.Component;
Expand All @@ -16,11 +14,11 @@
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.resource.AbstractResourceResolver;
import org.springframework.web.reactive.resource.EncodedResourceResolver;
import org.springframework.web.reactive.resource.PathResourceResolver;
import org.springframework.web.reactive.resource.ResourceResolverChain;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import run.halo.app.infra.ThemeRootGetter;
import run.halo.app.infra.utils.FileUtils;

@Component
public class ThemeWebFluxConfigurer implements WebFluxConfigurer {
Expand All @@ -47,8 +45,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
.setUseLastModified(useLastModified)
.resourceChain(true)
.addResolver(new EncodedResourceResolver())
.addResolver(new ThemePathResourceResolver(themeRootGetter.get()))
.addResolver(new PathResourceResolver());
.addResolver(new ThemePathResourceResolver(themeRootGetter.get()));
}

/**
Expand All @@ -59,14 +56,10 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
*/
private static class ThemePathResourceResolver extends AbstractResourceResolver {

private final Resource themeRootResource;
private final Path themeRoot;

private ThemePathResourceResolver(Path themeRoot) {
try {
this.themeRootResource = new FileUrlResource(themeRoot.toUri().toURL());
} catch (MalformedURLException e) {
throw new RuntimeException("Failed to resolve " + themeRoot + " to URL.", e);
}
this.themeRoot = themeRoot;
}

@Override
Expand All @@ -84,19 +77,17 @@ protected Mono<Resource> resolveResourceInternal(ServerWebExchange exchange,
return Mono.empty();
}

try {
var location = themeRootResource.createRelative(themeName + "/templates/assets/");
return chain.resolveResource(exchange, resourcePaths, List.of(location));
} catch (IOException e) {
return Mono.empty();
}
var assetsPath = themeRoot.resolve(themeName + "/templates/assets/" + resourcePaths);
FileUtils.checkDirectoryTraversal(themeRoot, assetsPath);
var location = new FileSystemResource(assetsPath);
return Mono.just(location);
}

@Override
protected Mono<String> resolveUrlPathInternal(String resourceUrlPath,
List<? extends Resource> locations,
ResourceResolverChain chain) {
return chain.resolveUrlPath(resourceUrlPath, locations);
List<? extends Resource> locations, ResourceResolverChain chain) {
throw new UnsupportedOperationException();
}

}
}

0 comments on commit 7b2c8e4

Please sign in to comment.