ad5be295747fa741915b708f9ca8b8b0266de051
[mediacube.git] /
1 package hu.user.mediacube.integration.safedelete;
2
3 import lombok.extern.log4j.Log4j2;
4
5 import java.nio.file.DirectoryStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.Paths;
9
10 @Log4j2
11 public class FileOperations {
12     public static final String ERROR_FILENAME_PREFIX = "ERROR-";
13     public static final String KILLDATE_POSTFIX = ".killdate";
14     public static final String STATUS = "/.STATUS";
15
16     static public Path getStatusPath(Path inputPath) {
17         Path result = null;
18         if (Files.isDirectory(inputPath)) {
19             result = Paths.get(inputPath.toAbsolutePath().toString(), STATUS);
20         } else {
21             result = Paths.get(inputPath.getParent().toAbsolutePath().toString(), STATUS);
22         }
23         return result;
24     }
25
26     static public boolean silentDelete(Path path) {
27         boolean result = false;
28         try {
29             Files.delete(path);
30             log.info("File deleted: {}", path);
31             result = true;
32         } catch (Exception e) {
33             log.catching(e);
34         }
35         return result;
36     }
37
38     static public void silentRename(Path source, String prefix) {
39         try {
40             String fileName = String.format("%s%s", prefix, source.getFileName().toString());
41             Path target = Paths.get(source.getParent().toAbsolutePath().toString(), fileName);
42             Files.move(source, target);
43             log.info("File renamed to: {}", target);
44         } catch (Exception e) {
45             log.catching(e);
46         }
47     }
48
49     public static DirectoryStream.Filter<Path> filterOnlyFiles() {
50         return entry -> !Files.isDirectory(entry) && !entry.getFileName().toString().startsWith(ERROR_FILENAME_PREFIX);
51     }
52
53     public static DirectoryStream.Filter<Path> filterKillDateFiles() {
54         return entry -> !Files.isDirectory(entry) && entry.getFileName().toString().endsWith(KILLDATE_POSTFIX);
55     }
56
57     public static DirectoryStream.Filter<Path> filterRelatedFiles(String originalFileName) {
58         return entry -> !Files.isDirectory(entry) && entry.getFileName().toString().startsWith(originalFileName);
59     }
60 }