fix: default theme cannot be initialized in the jar distribution (#2991)

#### What type of PR is this?
/kind bug

#### What this PR does / why we need it:
To fix that default earth theme file not found in system initialization.

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

Fixes #2910 

#### Special notes for your reviewer:

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

```release-note
NONE
```
pull/3003/head^2
will 2022-12-19 23:00:16 +08:00 committed by GitHub
parent 27775c9ac9
commit dee496b349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 6 deletions

View File

@ -1,12 +1,10 @@
package run.halo.app.infra; package run.halo.app.infra;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import run.halo.app.core.extension.theme.ThemeService; import run.halo.app.core.extension.theme.ThemeService;
@ -47,15 +45,16 @@ public class DefaultThemeInitializer implements ApplicationListener<SchemeInitia
return; return;
} }
log.info("Initializing default theme from {}", location); log.info("Initializing default theme from {}", location);
var defaultThemeUri = ResourceUtils.getURL(location).toURI(); PathMatchingResourcePatternResolver resolver =
new PathMatchingResourcePatternResolver();
var latch = new CountDownLatch(1); var latch = new CountDownLatch(1);
themeService.install(Files.newInputStream(Path.of(defaultThemeUri))) themeService.install(ResourceUtils.getURL(location).openStream())
.doFinally(signalType -> latch.countDown()) .doFinally(signalType -> latch.countDown())
.subscribe(theme -> log.info("Initialized default theme: {}", .subscribe(theme -> log.info("Initialized default theme: {}",
theme.getMetadata().getName())); theme.getMetadata().getName()));
latch.await(); latch.await();
// Because default active theme is default, we don't need to enabled it manually. // Because default active theme is default, we don't need to enabled it manually.
} catch (IOException | URISyntaxException | InterruptedException e) { } catch (IOException | InterruptedException e) {
// we should skip the initialization error at here // we should skip the initialization error at here
log.warn("Failed to initialize theme from " + location, e); log.warn("Failed to initialize theme from " + location, e);
} }