Skip to content

Commit 96b2c8b

Browse files
authored
BE: Chore: Dynamic config file checks adjustments (#229)
1 parent 1c5242f commit 96b2c8b

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

api/src/main/java/io/kafbat/ui/util/DynamicConfigOperations.java

+25-20
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.yaml.snakeyaml.nodes.Tag;
3939
import org.yaml.snakeyaml.representer.Representer;
4040
import reactor.core.publisher.Mono;
41+
import reactor.core.scheduler.Schedulers;
4142

4243
@Slf4j
4344
@RequiredArgsConstructor
@@ -125,26 +126,30 @@ public Mono<Path> uploadConfigRelatedFile(FilePart file) {
125126
String targetDirStr = ctx.getEnvironment()
126127
.getProperty(CONFIG_RELATED_UPLOADS_DIR_PROPERTY, CONFIG_RELATED_UPLOADS_DIR_DEFAULT);
127128

128-
Path targetDir = Path.of(targetDirStr);
129-
if (!Files.exists(targetDir)) {
130-
try {
131-
Files.createDirectories(targetDir);
132-
} catch (IOException e) {
133-
return Mono.error(
134-
new FileUploadException("Error creating directory for uploads %s".formatted(targetDir), e));
129+
Mono<Path> directoryCreationMono = Mono.fromCallable(() -> {
130+
Path targetDir = Path.of(targetDirStr);
131+
if (!Files.exists(targetDir)) {
132+
try {
133+
Files.createDirectories(targetDir);
134+
} catch (IOException e) {
135+
throw new FileUploadException("Error creating directory for uploads %s".formatted(targetDir), e);
136+
}
137+
}
138+
return targetDir;
139+
}).subscribeOn(Schedulers.boundedElastic());
140+
141+
return directoryCreationMono.flatMap(dir -> {
142+
Path targetFilePath = dir.resolve(file.filename() + "-" + Instant.now().getEpochSecond());
143+
log.info("Uploading config-related file {}", targetFilePath);
144+
if (Files.exists(targetFilePath)) {
145+
log.info("File {} already exists, it will be overwritten", targetFilePath);
135146
}
136-
}
137-
138-
Path targetFilePath = targetDir.resolve(file.filename() + "-" + Instant.now().getEpochSecond());
139-
log.info("Uploading config-related file {}", targetFilePath);
140-
if (Files.exists(targetFilePath)) {
141-
log.info("File {} already exists, it will be overwritten", targetFilePath);
142-
}
143147

144-
return file.transferTo(targetFilePath)
145-
.thenReturn(targetFilePath)
146-
.doOnError(th -> log.error("Error uploading file {}", targetFilePath, th))
147-
.onErrorMap(th -> new FileUploadException(targetFilePath, th));
148+
return file.transferTo(targetFilePath)
149+
.thenReturn(targetFilePath)
150+
.doOnError(th -> log.error("Error uploading file {}", targetFilePath, th))
151+
.onErrorMap(th -> new FileUploadException(targetFilePath, th));
152+
});
148153
}
149154

150155
private void checkIfDynamicConfigEnabled() {
@@ -163,8 +168,8 @@ private void writeYamlToFile(String yaml, Path path) {
163168
if (!Files.exists(path.getParent())) {
164169
Files.createDirectories(path.getParent());
165170
}
166-
if (Files.exists(path) && !Files.isWritable(path)) {
167-
throw new ValidationException("File already exists and is not writable");
171+
if (Files.exists(path) && (!Files.isReadable(path) || !Files.isWritable(path))) {
172+
throw new ValidationException("File already exists and is not readable or writable");
168173
}
169174
try {
170175
Files.writeString(

0 commit comments

Comments
 (0)