commit | author | age
|
7fdd7e
|
1 |
/* |
U |
2 |
http-cm - File management extensions to jdk.httpserver |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.httpserver.cm; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.io.IOException; |
|
22 |
import java.nio.file.FileVisitResult; |
|
23 |
import java.nio.file.FileVisitor; |
|
24 |
import java.nio.file.Files; |
|
25 |
import java.nio.file.Path; |
|
26 |
import java.nio.file.attribute.BasicFileAttributes; |
|
27 |
|
|
28 |
/** |
|
29 |
* Ein FileVisitor zum Verschieben oder Kopieren ganzer Ordnerstrukturen mit |
|
30 |
* Hilfe der Methode Files.walkFileTree von java.nio. |
|
31 |
* |
|
32 |
* @author Ulrich Hilger |
|
33 |
* @version 1, 14. Mai 2021 |
|
34 |
*/ |
|
35 |
public class OrdnerBearbeiter extends FileTransporter implements FileVisitor { |
|
36 |
|
|
37 |
private Path targetDir; |
|
38 |
private int operation; |
|
39 |
|
|
40 |
public void setTargetDir(Path targetDir) { |
|
41 |
this.targetDir = targetDir; |
5adf10
|
42 |
//logger.fine("targetDir: " + targetDir.toString()); |
7fdd7e
|
43 |
} |
U |
44 |
|
|
45 |
/** |
|
46 |
* OP_COPY oder OP_MOVE |
|
47 |
* |
|
48 |
* @param op |
|
49 |
*/ |
|
50 |
public void setOperation(int op) { |
|
51 |
this.operation = op; |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException { |
|
56 |
if (operation != FileManager.OP_DELETE) { |
|
57 |
if (dir instanceof Path) { |
|
58 |
Path sourceDir = (Path) dir; |
|
59 |
File destFile = targetDir.resolve(sourceDir.getFileName()).toFile(); |
5adf10
|
60 |
//logger.fine("sourceDir: " + sourceDir + ", destFile: " + destFile); |
7fdd7e
|
61 |
if (destFile.exists()) { |
U |
62 |
File newDir = getNewFileName(destFile); |
|
63 |
destFile.renameTo(newDir); |
|
64 |
} |
|
65 |
destFile.mkdir(); |
|
66 |
this.targetDir = destFile.toPath(); |
5adf10
|
67 |
//logger.fine("targetDir now: " + targetDir.toString()); |
7fdd7e
|
68 |
} |
U |
69 |
} |
|
70 |
return FileVisitResult.CONTINUE; |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException { |
|
75 |
if(operation != FileManager.OP_DELETE) { |
|
76 |
if (file instanceof Path) { |
|
77 |
Path source = (Path) file; |
|
78 |
File destFile = targetDir.resolve(source.getFileName()).toFile(); |
|
79 |
if (destFile.exists()) { |
|
80 |
destFile = getNewFileName(destFile); |
|
81 |
} |
|
82 |
if (operation == FileManager.OP_MOVE) { |
5adf10
|
83 |
//logger.fine("move source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
7fdd7e
|
84 |
Files.move(source, destFile.toPath()); |
U |
85 |
} else if (operation == FileManager.OP_COPY) { |
5adf10
|
86 |
//logger.fine("copy source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
7fdd7e
|
87 |
Files.copy(source, destFile.toPath()); |
U |
88 |
} |
|
89 |
} |
|
90 |
} else { |
|
91 |
Files.delete((Path) file); |
|
92 |
} |
|
93 |
return FileVisitResult.CONTINUE; |
|
94 |
} |
|
95 |
|
|
96 |
@Override |
|
97 |
public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException { |
|
98 |
return FileVisitResult.CONTINUE; |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException { |
|
103 |
if (operation != FileManager.OP_DELETE) { |
|
104 |
if (dir instanceof Path) { |
|
105 |
Path finishedDir = (Path) dir; |
|
106 |
targetDir = targetDir.getParent(); |
|
107 |
if(operation == FileManager.OP_MOVE) { |
5adf10
|
108 |
//logger.fine("delete " + finishedDir.toString()); |
7fdd7e
|
109 |
Files.delete(finishedDir); |
U |
110 |
} |
|
111 |
} |
5adf10
|
112 |
//logger.fine("targetDir now: " + targetDir.toString()); |
7fdd7e
|
113 |
} else { |
U |
114 |
Files.delete((Path) dir); |
|
115 |
} |
|
116 |
return FileVisitResult.CONTINUE; |
|
117 |
} |
|
118 |
|
|
119 |
} |