Respond 404 for non-exist theme resources instead of 500 (#6340)

#### What type of PR is this?

/kind bug
/area core
/milestone 2.18.x

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

This PR checks readable of theme resources while getting resources to prevent Halo from throwing FileNotFoundException.

#### Which issue(s) this PR fixes:

Fixes #6338 

#### Special notes for your reviewer:

1. Try to request <https://www.halo.run/themes/fake-theme/assets/favicons/favicon-32x32.png>
2. See the result

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

```release-note
修复访问不存在的主题资源时出现服务器异常的问题
```
pull/6363/head
John Niang 2024-07-22 12:35:32 +08:00 committed by GitHub
parent 2a807b748b
commit 1992916ab6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View File

@ -80,6 +80,9 @@ public class ThemeWebFluxConfigurer implements WebFluxConfigurer {
var assetsPath = themeRoot.resolve(themeName + "/templates/assets/" + resourcePaths);
FileUtils.checkDirectoryTraversal(themeRoot, assetsPath);
var location = new FileSystemResource(assetsPath);
if (!location.isReadable()) {
return Mono.empty();
}
return Mono.just(location);
}

View File

@ -144,4 +144,14 @@ class WebFluxConfigTest {
}
}
@Nested
class StaticResourcesTest {
@Test
void shouldRespond404WhenThemeResourceNotFound() {
webClient.get().uri("/themes/fake-theme/assets/favicon.ico")
.exchange()
.expectStatus().isNotFound();
}
}
}