commit | author | age
|
cc154b
|
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.actor; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.HttpExchange; |
|
21 |
import de.uhilger.httpserver.base.HttpHelper; |
|
22 |
import de.uhilger.httpserver.base.handler.FileHandler; |
|
23 |
import static de.uhilger.httpserver.base.handler.FileHandler.SC_OK; |
|
24 |
import static de.uhilger.httpserver.cm.FileManager.OP_MOVE; |
|
25 |
import static de.uhilger.httpserver.cm.FileManager.STR_DOT; |
|
26 |
import de.uhilger.httpserver.cm.FileTransporter; |
|
27 |
import de.uhilger.httpserver.cm.OrdnerBearbeiter; |
|
28 |
import de.uhilger.httpserver.image.ImageActor; |
|
29 |
import java.io.File; |
|
30 |
import java.io.IOException; |
|
31 |
import java.nio.file.DirectoryStream; |
|
32 |
import java.nio.file.Files; |
|
33 |
import java.nio.file.Path; |
|
34 |
|
|
35 |
/** |
|
36 |
* Eine Klasse mit Methoden zum Kopieren und Verschieben von Dateien |
|
37 |
* |
|
38 |
* @author Ulrich Hilger, 15. Janaur 2024 |
|
39 |
*/ |
|
40 |
public class Mover { |
|
41 |
public String copyOrMoveFiles(String fromPath, String toPath, String[] fileNames, int operation, String base) throws IOException { |
|
42 |
String result = null; |
|
43 |
File srcDir = new File(base, fromPath); |
|
44 |
File targetDir = new File(base, toPath); |
|
45 |
for (String fileName : fileNames) { |
|
46 |
File srcFile = new File(srcDir, fileName); |
|
47 |
//logger.fine("srcFile: " + srcFile); |
|
48 |
if (srcFile.isDirectory()) { |
|
49 |
//logger.fine("srcFile is directory."); |
|
50 |
OrdnerBearbeiter bearbeiter = new OrdnerBearbeiter(); |
|
51 |
bearbeiter.setTargetDir(targetDir.toPath()); |
|
52 |
bearbeiter.setOperation(operation); |
|
53 |
Files.walkFileTree(srcFile.toPath(), bearbeiter); |
|
54 |
} else { |
|
55 |
Path source = srcFile.toPath(); |
|
56 |
File destFile = targetDir.toPath().resolve(source.getFileName()).toFile(); |
|
57 |
if (destFile.exists()) { |
|
58 |
FileTransporter trans = new FileTransporter(); |
|
59 |
destFile = trans.getNewFileName(destFile); |
|
60 |
} |
|
61 |
if (operation == OP_MOVE) { |
|
62 |
String fname = srcFile.getName().toLowerCase(); |
|
63 |
if (fname.endsWith(ImageActor.JPEG) |
|
64 |
|| fname.endsWith(ImageActor.JPG) |
|
65 |
|| fname.endsWith(ImageActor.PNG)) { |
|
66 |
moveImgFilesToDirectory(srcFile, srcDir, targetDir, false); |
|
67 |
} else { |
|
68 |
Files.move(source, destFile.toPath()); |
|
69 |
} |
|
70 |
} else { |
|
71 |
Files.copy(source, destFile.toPath()); |
|
72 |
} |
|
73 |
} |
|
74 |
} |
|
75 |
return result; |
|
76 |
} |
|
77 |
|
|
78 |
private void moveImgFilesToDirectory(File srcFile, File srcDir, File targetDir, boolean createDestDir) throws IOException { |
|
79 |
String fnameext = srcFile.getName(); |
|
80 |
int dotpos = fnameext.lastIndexOf(STR_DOT); |
|
81 |
String fname = fnameext.substring(0, dotpos); |
|
82 |
String ext = fnameext.substring(dotpos); |
|
83 |
//logger.fine("fname: " + fname + ", ext: " + ext); |
|
84 |
Path targetPath = targetDir.toPath(); |
|
85 |
DirectoryStream<Path> stream = Files.newDirectoryStream(srcDir.toPath(), fname + "*" + ext); //"*.{txt,doc,pdf,ppt}" |
|
86 |
for (Path path : stream) { |
|
87 |
//logger.fine(path.getFileName().toString()); |
|
88 |
//Files.delete(path); |
|
89 |
Files.move(path, targetPath.resolve(path.getFileName())); |
|
90 |
} |
|
91 |
stream.close(); |
|
92 |
} |
|
93 |
|
|
94 |
public String duplizieren(String base, String relPfad) throws IOException { |
|
95 |
File srcFile = new File(base, relPfad); |
|
96 |
String fnameext = srcFile.getName(); |
|
97 |
int dotpos = fnameext.lastIndexOf(STR_DOT); |
|
98 |
String fname = fnameext.substring(0, dotpos); |
|
99 |
String ext = fnameext.substring(dotpos); |
|
100 |
File srcDir = srcFile.getParentFile(); |
|
101 |
File destFile = new File(srcDir, fname + "-Kopie" + ext); |
|
102 |
int i = 1; |
|
103 |
while (destFile.exists()) { |
|
104 |
destFile = new File(srcDir, fname + "-Kopie-" + Integer.toString(++i) + ext); |
|
105 |
} |
|
106 |
Files.copy(srcFile.toPath(), destFile.toPath()); |
|
107 |
return destFile.getName(); |
|
108 |
} |
|
109 |
|
|
110 |
|
|
111 |
} |