Skip to content

Commit

Permalink
BE: Chore: Dynamic config file checks adjustments (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
Haarolean authored Jan 16, 2025
1 parent 1c5242f commit 96b2c8b
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions api/src/main/java/io/kafbat/ui/util/DynamicConfigOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -125,26 +126,30 @@ public Mono<Path> uploadConfigRelatedFile(FilePart file) {
String targetDirStr = ctx.getEnvironment()
.getProperty(CONFIG_RELATED_UPLOADS_DIR_PROPERTY, CONFIG_RELATED_UPLOADS_DIR_DEFAULT);

Path targetDir = Path.of(targetDirStr);
if (!Files.exists(targetDir)) {
try {
Files.createDirectories(targetDir);
} catch (IOException e) {
return Mono.error(
new FileUploadException("Error creating directory for uploads %s".formatted(targetDir), e));
Mono<Path> directoryCreationMono = Mono.fromCallable(() -> {
Path targetDir = Path.of(targetDirStr);
if (!Files.exists(targetDir)) {
try {
Files.createDirectories(targetDir);
} catch (IOException e) {
throw new FileUploadException("Error creating directory for uploads %s".formatted(targetDir), e);
}
}
return targetDir;
}).subscribeOn(Schedulers.boundedElastic());

return directoryCreationMono.flatMap(dir -> {
Path targetFilePath = dir.resolve(file.filename() + "-" + Instant.now().getEpochSecond());
log.info("Uploading config-related file {}", targetFilePath);
if (Files.exists(targetFilePath)) {
log.info("File {} already exists, it will be overwritten", targetFilePath);
}
}

Path targetFilePath = targetDir.resolve(file.filename() + "-" + Instant.now().getEpochSecond());
log.info("Uploading config-related file {}", targetFilePath);
if (Files.exists(targetFilePath)) {
log.info("File {} already exists, it will be overwritten", targetFilePath);
}

return file.transferTo(targetFilePath)
.thenReturn(targetFilePath)
.doOnError(th -> log.error("Error uploading file {}", targetFilePath, th))
.onErrorMap(th -> new FileUploadException(targetFilePath, th));
return file.transferTo(targetFilePath)
.thenReturn(targetFilePath)
.doOnError(th -> log.error("Error uploading file {}", targetFilePath, th))
.onErrorMap(th -> new FileUploadException(targetFilePath, th));
});
}

private void checkIfDynamicConfigEnabled() {
Expand All @@ -163,8 +168,8 @@ private void writeYamlToFile(String yaml, Path path) {
if (!Files.exists(path.getParent())) {
Files.createDirectories(path.getParent());
}
if (Files.exists(path) && !Files.isWritable(path)) {
throw new ValidationException("File already exists and is not writable");
if (Files.exists(path) && (!Files.isReadable(path) || !Files.isWritable(path))) {
throw new ValidationException("File already exists and is not readable or writable");
}
try {
Files.writeString(
Expand Down

0 comments on commit 96b2c8b

Please sign in to comment.