Skip to content

Commit

Permalink
recursively delete dir after session close (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
xgp authored Feb 20, 2024
1 parent d24f7f2 commit 0de4377
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public String getName() {

@Override
public String getParentName() {
return getAttribute("_providerConfig.theme.email.parent", "mustache");
return getAttribute(EMAIL_PARENT_ATTRIBUTE_PREFIX, "mustache");
}

@Override
Expand All @@ -95,8 +95,15 @@ public Type getType() {
return type;
}

public static final String EMAIL_TEMPLATE_ATTRIBUTE_PREFIX = "_providerConfig.templates.email";
public static final String EMAIL_MESSAGE_ATTRIBUTE_PREFIX = "_providerConfig.messages.email";
private static final String LEGACY_EMAIL_TEMPLATE_ATTRIBUTE_PREFIX =
"_providerConfig.templates.email";
private static final String LEGACY_EMAIL_MESSAGE_ATTRIBUTE_PREFIX =
"_providerConfig.messages.email";
private static final String EMAIL_TEMPLATE_ATTRIBUTE_PREFIX =
"_providerConfig.theme.email.templates";
private static final String EMAIL_MESSAGE_ATTRIBUTE_PREFIX =
"_providerConfig.theme.email.messages";
private static final String EMAIL_PARENT_ATTRIBUTE_PREFIX = "_providerConfig.theme.email.parent";

public static String templateKey(String templateName) {
return String.format("%s.%s", EMAIL_TEMPLATE_ATTRIBUTE_PREFIX, templateName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.google.auto.service.AutoService;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Comparator;
import lombok.extern.jbosslog.JBossLog;
import org.keycloak.Config;
import org.keycloak.models.KeycloakSession;
Expand Down Expand Up @@ -33,7 +36,24 @@ public void init(Config.Scope config) {}
public void postInit(KeycloakSessionFactory factory) {}

@Override
public void close() {}
public void close() {
try {
deleteRecursively(tmpdir);
} catch (Exception e) {
log.warnf(e, "Error removing tmpdir", tmpdir);
}
}

private static void deleteRecursively(File dir) throws IOException {
Path toDelete = dir.toPath();

java.nio.file.Files.walk(toDelete)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);

log.tracef("%s deleted? %b", toDelete, java.nio.file.Files.exists(toDelete));
}

@Override
public String getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ private EmailTemplate processMustacheTemplate(
// Expose locale_xy flags
Stream stream = realm.getSupportedLocalesStream();
if (stream != null) {
stream.forEach(loc -> {
String key = String.format("locale_%s", loc);
Boolean ok = locale.getLanguage().equals(loc);
attributes.put(key, ok);
});
stream.forEach(
loc -> {
String key = String.format("locale_%s", loc);
Boolean ok = locale.getLanguage().equals(loc);
attributes.put(key, ok);
});
}

KeycloakUriInfo uriInfo = session.getContext().getUri();
Expand Down

0 comments on commit 0de4377

Please sign in to comment.