28 lines
1.6 KiB
Java
28 lines
1.6 KiB
Java
package online.astradev.content;
|
|||
|
|
|
||
|
|
import java.nio.file.*;
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
|
||
|
|
/** Install absent defaults before AstraPlus loads its catalog. Never overwrite administrator edits. */
|
||
|
|
public final class Defaults {
|
||
|
|
private Defaults() {}
|
||
|
|
public static void install(Path root) {
|
||
|
|
try (var index = Defaults.class.getResourceAsStream("/defaults/index.txt")) {
|
||
|
|
if (index == null) throw new IllegalStateException("Missing content index");
|
||
|
|
for (String name : new String(index.readAllBytes(), StandardCharsets.UTF_8).lines().toList()) {
|
||
|
|
if (name.isBlank()) continue;
|
||
|
|
Path target = root.toAbsolutePath().normalize().resolve("astraplus").resolve(name).normalize();
|
||
|
|
if (!target.startsWith(root.toAbsolutePath().normalize().resolve("astraplus"))) throw new IllegalArgumentException(name);
|
||
|
|
for (Path part = target; part != null; part = part.getParent())
|
||
|
|
if (Files.isSymbolicLink(part)) throw new IllegalStateException("Symbolic link: " + part);
|
||
|
|
if (Files.exists(target)) continue;
|
||
|
|
Files.createDirectories(target.getParent());
|
||
|
|
try (var stream = Defaults.class.getResourceAsStream("/defaults/" + name)) {
|
||
|
|
if (stream == null) throw new IllegalStateException("Missing default: " + name);
|
||
|
|
Files.copy(stream, target);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (java.io.IOException e) { throw new IllegalStateException("Cannot install AstraContent defaults", e); }
|
||
|
|
}
|
||
|
|
}
|