Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an API to list uninstalled themes #2586

Merged
merged 3 commits into from
Oct 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.BaseStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -52,6 +52,7 @@
import run.halo.app.extension.ListResult;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.Unstructured;
import run.halo.app.extension.router.IListRequest;
import run.halo.app.extension.router.QueryParamBuildUtil;
import run.halo.app.infra.exception.ThemeInstallationException;
import run.halo.app.infra.properties.HaloProperties;
Expand Down Expand Up @@ -124,7 +125,7 @@ public RouterFunction<ServerResponse> endpoint() {
.build();
}

public static class ThemeQuery extends PluginEndpoint.ListRequest.QueryListRequest {
public static class ThemeQuery extends IListRequest.QueryListRequest {

public ThemeQuery(MultiValueMap<String, String> queryParams) {
super(queryParams);
Expand All @@ -148,9 +149,9 @@ Mono<ServerResponse> listThemes(ServerRequest request) {
}

Mono<ListResult<Theme>> listUninstalled(ThemeQuery query) {
return Mono.just(themePathPolicy.themesDir())
.map(ThemeEndpoint::listAllThemesFromThemeDir)
.subscribeOn(Schedulers.boundedElastic())
Path path = themePathPolicy.themesDir();
return ThemeUtils.listAllThemesFromThemeDir(path)
.collectList()
.flatMap(this::filterUnInstalledThemes)
.map(themes -> {
Integer page = query.getPage();
Expand All @@ -160,21 +161,6 @@ Mono<ListResult<Theme>> listUninstalled(ThemeQuery query) {
});
}

private static List<Theme> listAllThemesFromThemeDir(Path themesDir) {
try (Stream<Path> paths = Files.walk(themesDir, 2)) {
return paths.filter(Files::isDirectory)
.map(themePath -> ThemeUtils.loadUnstructured(themePath,
ThemeUtils.THEME_MANIFESTS))
.flatMap(List::stream)
.map(unstructured -> Unstructured.OBJECT_MAPPER.convertValue(unstructured,
Theme.class))
.sorted(Comparator.comparing(theme -> theme.getMetadata().getName()))
.toList();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private Mono<List<Theme>> filterUnInstalledThemes(@NonNull List<Theme> allThemes) {
guqing marked this conversation as resolved.
Show resolved Hide resolved
return client.list(Theme.class, null, null)
.map(theme -> theme.getMetadata().getName())
Expand Down Expand Up @@ -320,6 +306,23 @@ static class ThemeUtils {

private static final String[] THEME_SETTING = {"settings.yaml", "settings.yml"};

static Flux<Theme> listAllThemesFromThemeDir(Path themesDir) {
return walkThemesFromPath(themesDir)
.filter(Files::isDirectory)
.map(themePath -> loadUnstructured(themePath, THEME_MANIFESTS))
.map(unstructured -> Unstructured.OBJECT_MAPPER.convertValue(unstructured,
Theme.class))
.sort(Comparator.comparing(theme -> theme.getMetadata().getName()));
}

private static Flux<Path> walkThemesFromPath(Path path) {
return Flux.using(() -> Files.walk(path, 2),
JohnNiang marked this conversation as resolved.
Show resolved Hide resolved
Flux::fromStream,
BaseStream::close
)
.subscribeOn(Schedulers.boundedElastic());
}

static List<Unstructured> loadThemeSetting(Path themePath) {
return loadUnstructured(themePath, THEME_SETTING);
}
Expand Down