aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils/FileUtils.java
blob: 2261144181d0d593db1cab051e72ee17bf24c29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package de.hysky.skyblocker.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.slf4j.Logger;

import com.mojang.logging.LogUtils;

public class FileUtils {
	private static final Logger LOGGER = LogUtils.getLogger();

	public static void recursiveDelete(Path dir) throws IOException {
		if (Files.isDirectory(dir) && !Files.isSymbolicLink(dir)) {
			Files.list(dir).forEach(child -> {
				try {
					recursiveDelete(child);
				} catch (Exception e) {
					LOGGER.error("[Skyblocker] Encountered an exception while deleting a file! Path: {}", child.toAbsolutePath(), e);
				}
			});
		}

		Files.delete(dir);
	}

	/**
	 * Replaces any characters that do not match the regex: [^a-z0-9_.-]
	 * 
	 * @implNote Designed to convert a file path to an {@link net.minecraft.util.Identifier}
	 */
	public static String normalizePath(Path path) {
		return path.toString().toLowerCase().replaceAll("[^a-z0-9_.-]", "");
	}
}