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 |
import java.util.logging.Logger; |
|
28 |
|
|
29 |
/** |
|
30 |
* Ein FileVisitor zum Verschieben oder Kopieren ganzer Ordnerstrukturen mit |
|
31 |
* Hilfe der Methode Files.walkFileTree von java.nio. |
|
32 |
* |
|
33 |
* @author Ulrich Hilger |
|
34 |
* @version 1, 14. Mai 2021 |
|
35 |
*/ |
|
36 |
public class OrdnerBearbeiter extends FileTransporter implements FileVisitor { |
|
37 |
|
|
38 |
/* Der Logger fuer diesen ListFileHandler */ |
|
39 |
private static final Logger logger = Logger.getLogger(OrdnerBearbeiter.class.getName()); |
|
40 |
|
|
41 |
private Path targetDir; |
|
42 |
private int operation; |
|
43 |
|
|
44 |
public void setTargetDir(Path targetDir) { |
|
45 |
this.targetDir = targetDir; |
|
46 |
logger.fine("targetDir: " + targetDir.toString()); |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* OP_COPY oder OP_MOVE |
|
51 |
* |
|
52 |
* @param op |
|
53 |
*/ |
|
54 |
public void setOperation(int op) { |
|
55 |
this.operation = op; |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException { |
|
60 |
if (operation != FileManager.OP_DELETE) { |
|
61 |
if (dir instanceof Path) { |
|
62 |
Path sourceDir = (Path) dir; |
|
63 |
File destFile = targetDir.resolve(sourceDir.getFileName()).toFile(); |
|
64 |
logger.fine("sourceDir: " + sourceDir + ", destFile: " + destFile); |
|
65 |
if (destFile.exists()) { |
|
66 |
File newDir = getNewFileName(destFile); |
|
67 |
destFile.renameTo(newDir); |
|
68 |
} |
|
69 |
destFile.mkdir(); |
|
70 |
this.targetDir = destFile.toPath(); |
|
71 |
logger.fine("targetDir now: " + targetDir.toString()); |
|
72 |
} |
|
73 |
} |
|
74 |
return FileVisitResult.CONTINUE; |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException { |
|
79 |
if(operation != FileManager.OP_DELETE) { |
|
80 |
if (file instanceof Path) { |
|
81 |
Path source = (Path) file; |
|
82 |
File destFile = targetDir.resolve(source.getFileName()).toFile(); |
|
83 |
if (destFile.exists()) { |
|
84 |
destFile = getNewFileName(destFile); |
|
85 |
} |
|
86 |
if (operation == FileManager.OP_MOVE) { |
|
87 |
logger.fine("move source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
|
88 |
Files.move(source, destFile.toPath()); |
|
89 |
} else if (operation == FileManager.OP_COPY) { |
|
90 |
logger.fine("copy source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
|
91 |
Files.copy(source, destFile.toPath()); |
|
92 |
} |
|
93 |
} |
|
94 |
} else { |
|
95 |
Files.delete((Path) file); |
|
96 |
} |
|
97 |
return FileVisitResult.CONTINUE; |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException { |
|
102 |
return FileVisitResult.CONTINUE; |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException { |
|
107 |
if (operation != FileManager.OP_DELETE) { |
|
108 |
if (dir instanceof Path) { |
|
109 |
Path finishedDir = (Path) dir; |
|
110 |
targetDir = targetDir.getParent(); |
|
111 |
if(operation == FileManager.OP_MOVE) { |
|
112 |
logger.fine("delete " + finishedDir.toString()); |
|
113 |
Files.delete(finishedDir); |
|
114 |
} |
|
115 |
} |
|
116 |
logger.fine("targetDir now: " + targetDir.toString()); |
|
117 |
} else { |
|
118 |
Files.delete((Path) dir); |
|
119 |
} |
|
120 |
return FileVisitResult.CONTINUE; |
|
121 |
} |
|
122 |
|
|
123 |
} |