commit | author | age
|
aed034
|
1 |
/* |
U |
2 |
neon-fm - Dateiverwaltung fuer neon |
|
3 |
Copyright (C) 2024 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.neon.fm; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.HttpExchange; |
|
21 |
import de.uhilger.neon.FileServer; |
|
22 |
import de.uhilger.neon.HttpResponder; |
|
23 |
import de.uhilger.fm.Duplicator; |
|
24 |
import de.uhilger.fm.Mover; |
|
25 |
import de.uhilger.fm.Renamer; |
|
26 |
import de.uhilger.fm.Inflator; |
|
27 |
import de.uhilger.fm.Deflator; |
|
28 |
import java.io.IOException; |
|
29 |
|
|
30 |
/** |
|
31 |
* Ein Neon-Actor fuer Aenderungen an Dateien. |
|
32 |
* |
|
33 |
* Die folgenden Aktionen werden ausgefuehrt. |
|
34 |
* |
|
35 |
* PUT [url] [body] |
|
36 |
* |
|
37 |
* <pre> |
|
38 |
* - Inhalt einer einzelnen Datei anlegen oder ueberschreiben: url=ziel, Dateiinhalt im Body |
|
39 |
* - Liste mit Dateien kopieren (?copyFrom=quelle): url=ziel, Dateiliste im Body |
|
40 |
* - Liste mit Dateien verschieben (?moveFrom=quelle): url=Ziel, Dateiliste im Body |
|
41 |
* - einzelne Datei duplizieren (?duplicate): url=Zieldatei |
|
42 |
* - einzelne Datei umbenennen (?renameTo=neuer Name): url=Datei oder -odner |
|
43 |
* - Ordner packen (?zip): url=Ordner |
|
44 |
* - Zip-Datei entpacken (?unzip): url=Datei |
|
45 |
* </pre> |
|
46 |
* |
|
47 |
* @author Ulrich Hilger |
|
48 |
* @version 0.1, 07.11.2024 |
|
49 |
*/ |
|
50 |
public class FileManipulator extends AbstractFileActor { |
|
51 |
|
|
52 |
private static final String P_RENAME = "renameTo"; |
|
53 |
private static final String P_COPY = "copyFrom"; |
|
54 |
private static final String P_MOVE = "moveFrom"; |
|
55 |
private static final String P_DUPLICATE = "duplicate"; |
|
56 |
private static final String P_ZIP = "zip"; |
|
57 |
private static final String P_UNZIP = "unzip"; |
|
58 |
|
|
59 |
public void run(HttpExchange exchange) { |
|
60 |
try { |
|
61 |
super.run(exchange); |
|
62 |
String query = exchange.getRequestURI().getQuery(); |
|
63 |
if (query != null) { |
|
64 |
String[] params = query.split(FileServer.STR_EQUAL); |
|
65 |
switch (params[0]) { |
|
66 |
case P_RENAME: |
|
67 |
h.inspectFileName(params[1]); |
81b69f
|
68 |
String neuerDateiName = new Renamer().rename(/*fileName, */params[1], file); |
aed034
|
69 |
antwort(exchange, HttpResponder.SC_OK, neuerDateiName); |
U |
70 |
break; |
|
71 |
case P_COPY: |
|
72 |
h.inspectFileName(params[1]); |
876484
|
73 |
copyOrMove(exchange, params[1], fileName, Mover.OP_COPY); |
aed034
|
74 |
break; |
U |
75 |
case P_MOVE: |
|
76 |
h.inspectFileName(params[1]); |
876484
|
77 |
copyOrMove(exchange, params[1], fileName, Mover.OP_MOVE); |
aed034
|
78 |
break; |
U |
79 |
case P_DUPLICATE: |
|
80 |
if (Boolean.parseBoolean(params[1])) { |
|
81 |
String dup = new Duplicator().duplizieren(base, fileName); |
|
82 |
//logger.fine("neuer Name: " + neuerDateiName); |
|
83 |
antwort(exchange, HttpResponder.SC_OK, dup); |
|
84 |
} |
|
85 |
break; |
|
86 |
case P_ZIP: |
537b65
|
87 |
//String path = exchange.getRequestURI().toString(); |
U |
88 |
zipAntwort(exchange, new Deflator().packFolder(fileName, /*path, */base)); |
aed034
|
89 |
break; |
U |
90 |
case P_UNZIP: |
537b65
|
91 |
//String path = exchange.getRequestURI().toString(); |
U |
92 |
zipAntwort(exchange, new Inflator().extractZipfile(fileName, /*path,*/ base)); |
aed034
|
93 |
break; |
U |
94 |
default: |
|
95 |
antwort(exchange, HttpResponder.SC_NOT_FOUND, "ungueltige Anfrage"); |
|
96 |
break; |
|
97 |
} |
|
98 |
} else { |
|
99 |
if (fileName.endsWith(FileServer.STR_SLASH)) { |
|
100 |
antwort(exchange, HttpResponder.SC_METHOD_NOT_ALLOWED, "PUT nicht erlaubt, POST verwenden."); |
|
101 |
} else { |
|
102 |
speichern(exchange); |
|
103 |
} |
|
104 |
} |
|
105 |
} catch (IOException | IllegalArgumentException ex) { |
|
106 |
fehlerAntwort(exchange, ex); |
|
107 |
} finally { |
|
108 |
free(); |
|
109 |
} |
|
110 |
} |
|
111 |
|
|
112 |
private void copyOrMove(HttpExchange exchange, String quelle, String ziel, int op) throws IOException { |
|
113 |
String[] dateiNamen = dateiliste(exchange); |
|
114 |
new Mover().copyOrMoveFiles(quelle, ziel, dateiNamen, op, base); |
|
115 |
antwort(exchange, HttpResponder.SC_OK, "Dateien verarbeitet."); |
|
116 |
} |
|
117 |
|
|
118 |
private void zipAntwort(HttpExchange exchange, String antw) throws IOException { |
|
119 |
if (antw.equalsIgnoreCase("ok")) { |
|
120 |
antwort(exchange, HttpResponder.SC_OK, antw); |
|
121 |
} else { |
|
122 |
antwort(exchange, HttpResponder.SC_UNPROCESSABLE_ENTITY, antw); |
|
123 |
} |
|
124 |
} |
|
125 |
} |