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 |
|
ea7193
|
48 |
public String hallo() { |
U |
49 |
return "Hallo Welt!"; |
|
50 |
} |
|
51 |
|
7342b1
|
52 |
public List<FileRef> list(String relPath) { |
5dfab6
|
53 |
List<FileRef> files = new ArrayList(); |
7342b1
|
54 |
if(relPath.length() == 0) { |
2121cc
|
55 |
FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
5dfab6
|
56 |
logger.finer(namedPublicFolder.getAbsolutePath()); |
2121cc
|
57 |
FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
5dfab6
|
58 |
logger.finer(namedHomeFolder.getAbsolutePath()); |
2121cc
|
59 |
files = new ArrayList(); |
7342b1
|
60 |
files.add(namedHomeFolder); |
U |
61 |
files.add(namedPublicFolder); |
ea7193
|
62 |
} else { |
2121cc
|
63 |
String path = getTargetDir(relPath).getAbsolutePath(); |
U |
64 |
logger.fine(path); |
5efd94
|
65 |
LocalFileSystem fs = new LocalFileSystem(); |
2121cc
|
66 |
FileRef[] fileRefs = fs.list(new FileRef(getTargetDir(relPath).getAbsolutePath(), true)); |
5dfab6
|
67 |
for(int i = 0; i < fileRefs.length; i++) { |
U |
68 |
files.add(fileRefs[i]); |
2121cc
|
69 |
logger.finer("added " + fileRefs[i].getAbsolutePath()); |
5dfab6
|
70 |
} |
c509a0
|
71 |
} |
7342b1
|
72 |
return files; |
2121cc
|
73 |
} |
U |
74 |
|
c509a0
|
75 |
public FileRef newFolder(String relPath, String folderName) { |
U |
76 |
logger.finer(relPath); |
|
77 |
String targetPath = null; |
|
78 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
79 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName; |
|
80 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
81 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName; |
|
82 |
} else { |
|
83 |
// kann eigentlich nicht sein.. |
|
84 |
} |
|
85 |
logger.finer(targetPath); |
|
86 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
87 |
targetDir.mkdirs(); |
|
88 |
return new FileRef(targetDir.getAbsolutePath(), true); |
5dfab6
|
89 |
} |
U |
90 |
|
3ad4db
|
91 |
public String getCode(String relPath, String fileName) { |
U |
92 |
String code = null; |
|
93 |
|
|
94 |
Object p = getRequest().getUserPrincipal(); |
|
95 |
if(p instanceof Principal) { |
|
96 |
FileReader reader = null; |
|
97 |
try { |
|
98 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
99 |
reader = new FileReader(targetFile); |
|
100 |
StringBuffer buf = new StringBuffer(); |
|
101 |
char[] readBuffer = new char[1024]; |
|
102 |
int charsRead = reader.read(readBuffer); |
|
103 |
while(charsRead > -1) { |
|
104 |
buf.append(readBuffer, 0, charsRead); |
|
105 |
charsRead = reader.read(readBuffer); |
|
106 |
} |
|
107 |
code = buf.toString(); |
|
108 |
} catch (FileNotFoundException ex) { |
|
109 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
110 |
} catch (IOException ex) { |
|
111 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
112 |
} finally { |
|
113 |
try { |
|
114 |
reader.close(); |
|
115 |
} catch (IOException ex) { |
|
116 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
} |
|
121 |
|
|
122 |
return code; |
|
123 |
} |
|
124 |
|
fc1897
|
125 |
public String deleteFiles(String relPath, List fileNames) { |
U |
126 |
String result = null; |
|
127 |
try { |
|
128 |
FileRef[] delRefs = new FileRef[fileNames.size()]; |
|
129 |
logger.fine(fileNames.toString()); |
|
130 |
File targetDir = getTargetDir(relPath); |
|
131 |
for(int i=0; i < fileNames.size(); i++) { |
|
132 |
Object o = fileNames.get(i); |
|
133 |
if(o instanceof ArrayList) { |
|
134 |
ArrayList al = (ArrayList) o; |
|
135 |
logger.fine(al.get(0).toString()); |
|
136 |
File targetFile = new File(targetDir, al.get(0).toString()); |
|
137 |
logger.fine(targetFile.getAbsolutePath()); |
|
138 |
delRefs[i] = new FileRef(targetFile.getAbsolutePath(), targetFile.isDirectory()); |
|
139 |
} |
|
140 |
} |
|
141 |
FileSystem fs = new LocalFileSystem(); |
|
142 |
fs.delete(delRefs); |
|
143 |
result = "deleted"; |
|
144 |
} catch (Throwable ex) { |
|
145 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
146 |
} |
|
147 |
return result; |
|
148 |
} |
|
149 |
|
ea7193
|
150 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
U |
151 |
FileRef savedFile = null; |
e5ff42
|
152 |
try { |
U |
153 |
FileRef datenRef = getBase(); |
3ad4db
|
154 |
//File daten = new File(datenRef.getAbsolutePath()); |
e5ff42
|
155 |
Object p = getRequest().getUserPrincipal(); |
U |
156 |
if(p instanceof Principal) { |
2121cc
|
157 |
File targetFile = new File(getTargetDir(relPath), fileName); |
7342b1
|
158 |
if(targetFile.exists()) { |
U |
159 |
/* |
|
160 |
muss delete() sein? |
|
161 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
162 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
163 |
Inhalt? |
|
164 |
*/ |
|
165 |
targetFile.delete(); |
|
166 |
} else { |
915927
|
167 |
targetFile.getParentFile().mkdirs(); |
e5ff42
|
168 |
} |
7342b1
|
169 |
targetFile.createNewFile(); |
U |
170 |
FileWriter w = new FileWriter(targetFile); |
|
171 |
w.write(contents); |
|
172 |
w.flush(); |
|
173 |
w.close(); |
|
174 |
savedFile = new FileRef( |
|
175 |
targetFile.getAbsolutePath(), |
|
176 |
targetFile.isDirectory(), |
|
177 |
targetFile.isHidden(), |
|
178 |
targetFile.lastModified(), |
|
179 |
targetFile.length()); |
e5ff42
|
180 |
} |
U |
181 |
} catch (IOException ex) { |
|
182 |
logger.log(Level.SEVERE, null, ex); |
|
183 |
} |
ea7193
|
184 |
return savedFile; |
U |
185 |
} |
5efd94
|
186 |
|
U |
187 |
/* ---- Hilfsfunktionen ---- */ |
ea7193
|
188 |
|
5efd94
|
189 |
private File getTargetDir(String relPath) { |
U |
190 |
logger.finer(relPath); |
|
191 |
String targetPath = null; |
|
192 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
193 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()); |
|
194 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
195 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()); |
|
196 |
} else { |
|
197 |
// kann eigentlich nicht sein.. |
|
198 |
} |
|
199 |
logger.finer(targetPath); |
|
200 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
201 |
return targetDir; |
|
202 |
} |
|
203 |
private FileRef getBase() { |
|
204 |
FileRef base = null; |
|
205 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
|
206 |
if(o instanceof FileRef) { |
|
207 |
base = (FileRef) o; |
|
208 |
} |
|
209 |
return base; |
|
210 |
} |
|
211 |
|
|
212 |
private String getUserName() { |
|
213 |
String userName = null; |
|
214 |
Object p = getRequest().getUserPrincipal(); |
|
215 |
if(p instanceof Principal) { |
|
216 |
userName = ((Principal) p).getName(); |
|
217 |
} |
|
218 |
return userName; |
|
219 |
} |
|
220 |
|
|
221 |
/* |
8e51b7
|
222 |
private File getWebappsDir() { |
8931b7
|
223 |
File cfile = new File(this.getClass().getResource( |
U |
224 |
this.getClass().getSimpleName() + ".class").getFile()); |
8e51b7
|
225 |
String path = cfile.getAbsolutePath(); |
8931b7
|
226 |
return new File(path.substring(0, path.indexOf(getRequest().getContextPath()))); |
c7c502
|
227 |
} |
5efd94
|
228 |
*/ |
7342b1
|
229 |
|
c7c502
|
230 |
} |