commit | author | age
|
8931b7
|
1 |
/* |
U |
2 |
Dateiverwaltung - File management in your browser |
|
3 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de |
|
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 <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
|
c7c502
|
19 |
package de.uhilger.filecms.api; |
U |
20 |
|
7342b1
|
21 |
import de.uhilger.filecms.web.Initialiser; |
c7c502
|
22 |
import de.uhilger.filesystem.FileRef; |
fc1897
|
23 |
import de.uhilger.filesystem.FileSystem; |
5dfab6
|
24 |
import de.uhilger.filesystem.LocalFileSystem; |
8e51b7
|
25 |
import java.io.File; |
3ad4db
|
26 |
import java.io.FileNotFoundException; |
U |
27 |
import java.io.FileReader; |
e5ff42
|
28 |
import java.io.FileWriter; |
U |
29 |
import java.io.IOException; |
|
30 |
import java.security.Principal; |
7342b1
|
31 |
import java.util.ArrayList; |
U |
32 |
import java.util.List; |
e5ff42
|
33 |
import java.util.logging.Level; |
8e51b7
|
34 |
import java.util.logging.Logger; |
c7c502
|
35 |
|
U |
36 |
/** |
|
37 |
* |
|
38 |
* @author ulrich |
|
39 |
*/ |
8931b7
|
40 |
public class FileMgr extends Api { |
8e51b7
|
41 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
42 |
|
7342b1
|
43 |
public static final String PUB_DIR_PATH = "www/"; |
U |
44 |
public static final String HOME_DIR_PATH = "home/"; |
|
45 |
public static final String PUB_DIR_NAME = "Oeffentlich"; |
|
46 |
public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; |
c7c502
|
47 |
|
9e2964
|
48 |
public static final int OP_COPY = 1; |
U |
49 |
public static final int OP_MOVE = 2; |
|
50 |
|
ea7193
|
51 |
public String hallo() { |
U |
52 |
return "Hallo Welt!"; |
|
53 |
} |
|
54 |
|
7342b1
|
55 |
public List<FileRef> list(String relPath) { |
5dfab6
|
56 |
List<FileRef> files = new ArrayList(); |
7342b1
|
57 |
if(relPath.length() == 0) { |
2121cc
|
58 |
FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
5dfab6
|
59 |
logger.finer(namedPublicFolder.getAbsolutePath()); |
2121cc
|
60 |
FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
5dfab6
|
61 |
logger.finer(namedHomeFolder.getAbsolutePath()); |
2121cc
|
62 |
files = new ArrayList(); |
7342b1
|
63 |
files.add(namedHomeFolder); |
U |
64 |
files.add(namedPublicFolder); |
ea7193
|
65 |
} else { |
2121cc
|
66 |
String path = getTargetDir(relPath).getAbsolutePath(); |
9e2964
|
67 |
logger.fine("path: " + path); |
5efd94
|
68 |
LocalFileSystem fs = new LocalFileSystem(); |
9e2964
|
69 |
logger.fine("listing " + getTargetDir(relPath).getAbsolutePath()); |
2121cc
|
70 |
FileRef[] fileRefs = fs.list(new FileRef(getTargetDir(relPath).getAbsolutePath(), true)); |
5dfab6
|
71 |
for(int i = 0; i < fileRefs.length; i++) { |
U |
72 |
files.add(fileRefs[i]); |
9e2964
|
73 |
logger.fine("added " + fileRefs[i].getAbsolutePath()); |
5dfab6
|
74 |
} |
c509a0
|
75 |
} |
7342b1
|
76 |
return files; |
2121cc
|
77 |
} |
U |
78 |
|
c509a0
|
79 |
public FileRef newFolder(String relPath, String folderName) { |
U |
80 |
logger.finer(relPath); |
|
81 |
String targetPath = null; |
|
82 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
83 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName; |
|
84 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
85 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName; |
|
86 |
} else { |
|
87 |
// kann eigentlich nicht sein.. |
|
88 |
} |
|
89 |
logger.finer(targetPath); |
|
90 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
91 |
targetDir.mkdirs(); |
|
92 |
return new FileRef(targetDir.getAbsolutePath(), true); |
5dfab6
|
93 |
} |
U |
94 |
|
3ad4db
|
95 |
public String getCode(String relPath, String fileName) { |
U |
96 |
String code = null; |
|
97 |
|
|
98 |
Object p = getRequest().getUserPrincipal(); |
|
99 |
if(p instanceof Principal) { |
|
100 |
FileReader reader = null; |
|
101 |
try { |
|
102 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
103 |
reader = new FileReader(targetFile); |
|
104 |
StringBuffer buf = new StringBuffer(); |
|
105 |
char[] readBuffer = new char[1024]; |
|
106 |
int charsRead = reader.read(readBuffer); |
|
107 |
while(charsRead > -1) { |
|
108 |
buf.append(readBuffer, 0, charsRead); |
|
109 |
charsRead = reader.read(readBuffer); |
|
110 |
} |
|
111 |
code = buf.toString(); |
|
112 |
} catch (FileNotFoundException ex) { |
|
113 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
114 |
} catch (IOException ex) { |
|
115 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
116 |
} finally { |
|
117 |
try { |
|
118 |
reader.close(); |
|
119 |
} catch (IOException ex) { |
|
120 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
} |
|
125 |
|
|
126 |
return code; |
|
127 |
} |
|
128 |
|
fc1897
|
129 |
public String deleteFiles(String relPath, List fileNames) { |
U |
130 |
String result = null; |
|
131 |
try { |
|
132 |
FileRef[] delRefs = new FileRef[fileNames.size()]; |
|
133 |
logger.fine(fileNames.toString()); |
|
134 |
File targetDir = getTargetDir(relPath); |
|
135 |
for(int i=0; i < fileNames.size(); i++) { |
|
136 |
Object o = fileNames.get(i); |
|
137 |
if(o instanceof ArrayList) { |
|
138 |
ArrayList al = (ArrayList) o; |
|
139 |
logger.fine(al.get(0).toString()); |
|
140 |
File targetFile = new File(targetDir, al.get(0).toString()); |
|
141 |
logger.fine(targetFile.getAbsolutePath()); |
|
142 |
delRefs[i] = new FileRef(targetFile.getAbsolutePath(), targetFile.isDirectory()); |
|
143 |
} |
|
144 |
} |
|
145 |
FileSystem fs = new LocalFileSystem(); |
|
146 |
fs.delete(delRefs); |
|
147 |
result = "deleted"; |
|
148 |
} catch (Throwable ex) { |
|
149 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
150 |
} |
|
151 |
return result; |
|
152 |
} |
|
153 |
|
9e2964
|
154 |
public String copyFiles(String fromPath, String toPath, List fileNames) { |
U |
155 |
return copyOrMoveFiles(fromPath, toPath, fileNames, OP_COPY); |
|
156 |
} |
|
157 |
|
|
158 |
public String moveFiles(String fromPath, String toPath, List fileNames) { |
|
159 |
return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE); |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* Dateien kopieren |
|
164 |
* |
|
165 |
* @param fromPath der relative Pfad, aus dem die Dateien stammen, die kopiert werden sollen |
|
166 |
* @param toPath der relative Pfad, an den die Dateien kopiert werden sollen |
|
167 |
* @param fileNames Liste mit Namen der Dateien, die kopiert werden sollen |
|
168 |
* @param operation OP_COPY oder OP_MOVE |
|
169 |
* @return null bei Fehler oder Quittungstext, wenn erfolgreich |
|
170 |
*/ |
|
171 |
private String copyOrMoveFiles(String fromPath, String toPath, List fileNames, int operation) { |
|
172 |
String result = null; |
|
173 |
try { |
|
174 |
FileRef[] files = new FileRef[fileNames.size()]; |
|
175 |
logger.fine(fileNames.toString()); |
|
176 |
File srcDir = getTargetDir(fromPath); |
|
177 |
for(int i=0; i < fileNames.size(); i++) { |
|
178 |
Object o = fileNames.get(i); |
|
179 |
if(o instanceof ArrayList) { |
|
180 |
ArrayList al = (ArrayList) o; |
|
181 |
logger.fine(al.get(0).toString()); |
|
182 |
File srcFile = new File(srcDir, al.get(0).toString()); |
|
183 |
logger.fine(srcFile.getAbsolutePath()); |
|
184 |
files[i] = new FileRef(srcFile.getAbsolutePath(), srcFile.isDirectory()); |
|
185 |
} |
|
186 |
} |
|
187 |
File targetDir = getTargetDir(toPath); |
|
188 |
FileSystem fs = new LocalFileSystem(); |
|
189 |
switch(operation) { |
|
190 |
case OP_COPY: |
|
191 |
fs.copy(files, new FileRef(targetDir.getAbsolutePath(), true)); |
|
192 |
break; |
|
193 |
case OP_MOVE: |
|
194 |
fs.move(files, new FileRef(targetDir.getAbsolutePath(), true)); |
|
195 |
break; |
|
196 |
} |
|
197 |
result = "kopiert"; |
|
198 |
} catch (Throwable ex) { |
|
199 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
200 |
} |
|
201 |
return result; |
|
202 |
} |
|
203 |
|
ea7193
|
204 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
U |
205 |
FileRef savedFile = null; |
e5ff42
|
206 |
try { |
U |
207 |
FileRef datenRef = getBase(); |
3ad4db
|
208 |
//File daten = new File(datenRef.getAbsolutePath()); |
e5ff42
|
209 |
Object p = getRequest().getUserPrincipal(); |
U |
210 |
if(p instanceof Principal) { |
2121cc
|
211 |
File targetFile = new File(getTargetDir(relPath), fileName); |
7342b1
|
212 |
if(targetFile.exists()) { |
U |
213 |
/* |
|
214 |
muss delete() sein? |
|
215 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
216 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
217 |
Inhalt? |
|
218 |
*/ |
|
219 |
targetFile.delete(); |
|
220 |
} else { |
915927
|
221 |
targetFile.getParentFile().mkdirs(); |
e5ff42
|
222 |
} |
7342b1
|
223 |
targetFile.createNewFile(); |
U |
224 |
FileWriter w = new FileWriter(targetFile); |
|
225 |
w.write(contents); |
|
226 |
w.flush(); |
|
227 |
w.close(); |
|
228 |
savedFile = new FileRef( |
|
229 |
targetFile.getAbsolutePath(), |
|
230 |
targetFile.isDirectory(), |
|
231 |
targetFile.isHidden(), |
|
232 |
targetFile.lastModified(), |
|
233 |
targetFile.length()); |
e5ff42
|
234 |
} |
U |
235 |
} catch (IOException ex) { |
|
236 |
logger.log(Level.SEVERE, null, ex); |
|
237 |
} |
ea7193
|
238 |
return savedFile; |
U |
239 |
} |
5efd94
|
240 |
|
U |
241 |
/* ---- Hilfsfunktionen ---- */ |
ea7193
|
242 |
|
5efd94
|
243 |
private File getTargetDir(String relPath) { |
3c4b10
|
244 |
logger.fine(relPath); |
5efd94
|
245 |
String targetPath = null; |
U |
246 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
9e2964
|
247 |
targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length()); |
5efd94
|
248 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
9e2964
|
249 |
targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length()); |
5efd94
|
250 |
} else { |
U |
251 |
// kann eigentlich nicht sein.. |
|
252 |
} |
3c4b10
|
253 |
logger.fine(targetPath); |
5efd94
|
254 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
U |
255 |
return targetDir; |
|
256 |
} |
9e2964
|
257 |
|
5efd94
|
258 |
private FileRef getBase() { |
U |
259 |
FileRef base = null; |
|
260 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
3c4b10
|
261 |
if(o instanceof String) { |
U |
262 |
String baseStr = (String) o; |
|
263 |
logger.fine(baseStr); |
|
264 |
File file = new File(baseStr); |
|
265 |
base = new FileRef(file.getAbsolutePath(), file.isDirectory()); |
5efd94
|
266 |
} |
U |
267 |
return base; |
|
268 |
} |
|
269 |
|
|
270 |
private String getUserName() { |
|
271 |
String userName = null; |
|
272 |
Object p = getRequest().getUserPrincipal(); |
|
273 |
if(p instanceof Principal) { |
|
274 |
userName = ((Principal) p).getName(); |
|
275 |
} |
|
276 |
return userName; |
|
277 |
} |
|
278 |
|
|
279 |
/* |
8e51b7
|
280 |
private File getWebappsDir() { |
8931b7
|
281 |
File cfile = new File(this.getClass().getResource( |
U |
282 |
this.getClass().getSimpleName() + ".class").getFile()); |
8e51b7
|
283 |
String path = cfile.getAbsolutePath(); |
8931b7
|
284 |
return new File(path.substring(0, path.indexOf(getRequest().getContextPath()))); |
c7c502
|
285 |
} |
5efd94
|
286 |
*/ |
7342b1
|
287 |
|
c7c502
|
288 |
} |