Fixed ResourceSvc file write if no directory exists

This commit is contained in:
Risto Lahtela 2020-03-21 16:01:15 +02:00
parent b7c84980e3
commit 6470832ef6
2 changed files with 9 additions and 1 deletions

View File

@ -35,6 +35,7 @@ import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.function.Supplier;
@ -171,7 +172,9 @@ public class ResourceSvc implements ResourceService {
WebResource original = source.get();
byte[] bytes = original.asBytes();
OpenOption[] overwrite = {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE};
Files.write(files.getCustomizationDirectory().resolve(fileName), bytes, overwrite);
Path to = files.getCustomizationDirectory().resolve(fileName);
Files.createDirectories(to.getParent());
Files.write(to, bytes, overwrite);
return original;
}

View File

@ -20,6 +20,7 @@ import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
public class ResourceSettings {
@ -33,13 +34,17 @@ public class ResourceSettings {
public boolean shouldBeCustomized(String plugin, String fileName) {
ConfigNode fileCustomization = config.getNode("Customized_files").orElseGet(() -> config.addNode("Customized_files"));
fileCustomization.setComment(Collections.singletonList("The files are placed in /Plan/web/ if the setting is 'true' when accessed."));
ConfigNode pluginCustomization = fileCustomization.getNode(plugin).orElseGet(() -> fileCustomization.addNode(plugin));
String fileNameNonPath = StringUtils.replaceChars(fileName, '.', ',');
if (pluginCustomization.contains(fileNameNonPath)) {
return pluginCustomization.getBoolean(fileNameNonPath);
} else {
pluginCustomization.set(fileNameNonPath, false);
try {
pluginCustomization.sort();
pluginCustomization.save();
} catch (IOException e) {
throw new UncheckedIOException("Could not save config.yml: " + e.getMessage(), e);