commit | author | age
|
a01889
|
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 |
|
|
19 |
package de.uhilger.wbx; |
|
20 |
|
708f66
|
21 |
import de.uhilger.wbx.data.Inhalt; |
96d8cf
|
22 |
import java.io.File; |
27d922
|
23 |
import java.io.FileReader; |
96d8cf
|
24 |
import java.util.ArrayList; |
U |
25 |
import java.util.Collections; |
|
26 |
import java.util.Comparator; |
|
27 |
import java.util.Iterator; |
|
28 |
import java.util.List; |
5ebac8
|
29 |
import java.util.logging.Level; |
a01889
|
30 |
import java.util.logging.Logger; |
5ebac8
|
31 |
import javax.naming.Context; |
U |
32 |
import javax.naming.InitialContext; |
|
33 |
import javax.naming.NamingException; |
882cad
|
34 |
import javax.servlet.ServletContext; |
a01889
|
35 |
|
U |
36 |
/** |
|
37 |
* |
|
38 |
*/ |
|
39 |
public class WbxUtils { |
|
40 |
|
|
41 |
private static final Logger logger = Logger.getLogger(WbxUtils.class.getName()); |
|
42 |
|
5ebac8
|
43 |
public static final String JNDI_CTX_NAME = "java:comp/env"; |
U |
44 |
|
|
45 |
public static final String NOT_FOUND = " nicht gefunden"; |
|
46 |
public static final String NO_STRING = " ist kein String"; |
|
47 |
public static final String EMPTY_STRING = ""; |
96d8cf
|
48 |
|
708f66
|
49 |
public static final String WBX_FILE_BASE = "wbxFileBase"; |
U |
50 |
|
96d8cf
|
51 |
public static final String WBX_PUB_DIR_NAME = "wbxPubDirName"; |
U |
52 |
public static final String WBX_DEFAULT_PUB_DIR_NAME = "/www"; |
|
53 |
|
708f66
|
54 |
public static final String WBX_PUB_URL_NAME = "wbxPubUrlName"; |
U |
55 |
public static final String WBX_DEFAULT_PUB_URL_NAME = "/data"; |
|
56 |
|
882cad
|
57 |
/** |
U |
58 |
* Bei der WebBox ist das Datenverzeichnis relativ zum Verzeichnis |
|
59 |
* $CATALINA_BASE/webapps untergebracht. |
|
60 |
* Die Abfrage ServletContext.getRealPath |
|
61 |
* liefert das Verzeichnis des Context dieser Webanwendung, also |
|
62 |
* $CATALINA_BASE/webapps/file-cms |
|
63 |
* oder |
|
64 |
* $WBX/sys/base/webapps/file-cms |
|
65 |
* |
|
66 |
* Unter Windows z.B. |
|
67 |
* C:\Users\fred\Documents\srv\wbx\sys\base\webapps\file-cms |
|
68 |
* Unter Linux oder Mac OS z.B. |
|
69 |
* /home/fred/srv/wbx/sys/base/webapps/file-cms |
|
70 |
* |
|
71 |
* Das Datenverzeichis liegt dann auf |
|
72 |
* $WBX/daten |
|
73 |
* |
|
74 |
* Mit dem Verzeichnis des Context dieser Webanwendung ist das |
|
75 |
* Datenverzeichnis der WebBox hart kodierbar mit dieser Methode |
|
76 |
* |
|
77 |
* @return Verzeichnis 'daten' der WebBox |
|
78 |
*/ |
|
79 |
public File getWbxDataDir() { |
|
80 |
File file = getWbxDir(); |
|
81 |
file = new File(file, "daten/"); |
|
82 |
logger.fine("WebBox Datenbasis: " + file.getAbsolutePath()); |
|
83 |
return file; |
|
84 |
} |
|
85 |
|
|
86 |
public File getWbxDir() { |
|
87 |
logger.fine("Catalina Base: " + System.getProperty("catalina.base")); |
|
88 |
File catalinaBase = new File(System.getProperty("catalina.base")); |
|
89 |
File wbxDir = catalinaBase.getParentFile().getParentFile(); |
|
90 |
return wbxDir; |
|
91 |
} |
708f66
|
92 |
|
U |
93 |
public List<Inhalt> collectFiles(String requestUrl, String contextPath, |
7a8d6a
|
94 |
String relativePath, int maxTiefe, int maxAnzahl, int length) { |
708f66
|
95 |
Bild bild = new Bild(); |
U |
96 |
//WbxUtils wu = new WbxUtils(); |
|
97 |
String basis = getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING); |
a505a5
|
98 |
if(basis.equals(WbxUtils.EMPTY_STRING)) { |
U |
99 |
basis = getWbxDataDir().getAbsolutePath(); |
|
100 |
} |
708f66
|
101 |
String pubDirName = getJNDIParameter(WbxUtils.WBX_PUB_DIR_NAME, WbxUtils.WBX_DEFAULT_PUB_DIR_NAME); |
U |
102 |
String pubUrlName = getJNDIParameter(WbxUtils.WBX_PUB_URL_NAME, WbxUtils.WBX_DEFAULT_PUB_URL_NAME); |
|
103 |
String relPath = relativePath.replace(pubUrlName, pubDirName); |
|
104 |
String absPath = basis + relPath; |
|
105 |
|
|
106 |
ArrayList beitraege = new ArrayList(); |
|
107 |
ArrayList<Inhalt> files = new ArrayList<>(); |
|
108 |
collectFiles(new File(absPath), 0, beitraege, maxTiefe, maxAnzahl); |
|
109 |
|
|
110 |
Iterator i = beitraege.iterator(); |
|
111 |
while(i.hasNext()) { |
|
112 |
File beitrag = (File) i.next(); |
|
113 |
Inhalt cont = new Inhalt(); |
|
114 |
cont.setMimetype(bild.getMimeType(beitrag)); |
|
115 |
cont.setIsDirectory(beitrag.isDirectory()); |
|
116 |
cont.setIsHidden(beitrag.isHidden()); |
|
117 |
cont.setLastModified(beitrag.lastModified()); |
|
118 |
cont.setLength(beitrag.length()); |
7a8d6a
|
119 |
if(length > 0) { |
U |
120 |
cont.setAbst(getFileContent(beitrag, length)); |
|
121 |
} |
708f66
|
122 |
|
U |
123 |
/* |
|
124 |
den 'https://..'-Teil bis vor dem |
|
125 |
ContextPath ermitteln |
|
126 |
*/ |
|
127 |
//String requestUrl = getRequest().getRequestURL().toString(); |
|
128 |
//String contextPath = getRequest().getContextPath(); |
|
129 |
int pos = requestUrl.indexOf(contextPath); |
|
130 |
|
|
131 |
/* |
|
132 |
den Teil des Pfades ermitteln, der zwischen dem |
|
133 |
ContextPath zum oeffentlichen Ordner und dem Dateiname |
|
134 |
steht |
|
135 |
*/ |
|
136 |
String absolutePath = beitrag.getAbsolutePath(); |
|
137 |
absolutePath = absolutePath.replace(beitrag.getName(), ""); |
|
138 |
absolutePath = absolutePath.replace(pubDirName, ""); |
|
139 |
String part = relativePath.replace(pubUrlName, ""); |
|
140 |
int pos2 = absolutePath.indexOf(part); |
|
141 |
String mittelteil = absolutePath.substring(pos2); |
|
142 |
mittelteil = mittelteil.replace(part, ""); |
|
143 |
cont.setBase(requestUrl.substring(0, pos)); |
|
144 |
|
|
145 |
cont.setUrl(/*requestUrl.substring(0, pos) + "/data" + */ mittelteil + beitrag.getName()); |
|
146 |
files.add(cont); |
|
147 |
} |
|
148 |
return files; |
|
149 |
} |
|
150 |
|
7a8d6a
|
151 |
private String getFileContent(File file, int len) { |
27d922
|
152 |
try { |
U |
153 |
StringBuffer readBuffer = new StringBuffer(); |
7a8d6a
|
154 |
char[] buf = new char[1]; |
U |
155 |
FileReader fr = new FileReader(file); |
|
156 |
int bytesRead = fr.read(buf); |
|
157 |
int read = 0; |
|
158 |
while(bytesRead > -1 && read < len) { |
27d922
|
159 |
read += bytesRead; |
7a8d6a
|
160 |
readBuffer.append(buf); |
U |
161 |
bytesRead = fr.read(buf); |
27d922
|
162 |
} |
U |
163 |
readBuffer.append(buf); |
7a8d6a
|
164 |
fr.close(); |
U |
165 |
logger.fine("read: " + read + ", readBuffer.len: " + readBuffer.length()); |
27d922
|
166 |
return readBuffer.toString(); |
U |
167 |
} catch (Exception ex) { |
7a8d6a
|
168 |
logger.log(Level.SEVERE, ex.getMessage(), ex); |
27d922
|
169 |
return EMPTY_STRING; |
U |
170 |
} |
|
171 |
} |
|
172 |
|
708f66
|
173 |
|
96d8cf
|
174 |
/** |
U |
175 |
* Diese Methode funktioniert nur, wenn entweder ein Ordner uebergeben |
|
176 |
* wird, der keine Unterordner enthaelt wie zum Beispiel der Ordner 'neu' |
|
177 |
* der Bildersammlung oder ein Ordner, dessen Unterordner |
|
178 |
* nach dem Schema Jahr, Monat benannt sind wie bei einem Journal, das |
|
179 |
* die Beitraege wie folgt enthaelt: |
|
180 |
* Journal-Ordner |
|
181 |
* 2018 |
|
182 |
* 12 |
|
183 |
* 11 |
|
184 |
* 10 |
|
185 |
* usw. |
|
186 |
* 2017 |
|
187 |
* 12 |
|
188 |
* 11 |
|
189 |
* 10 |
|
190 |
* usw. |
|
191 |
* |
|
192 |
* @param out |
|
193 |
* @param dir |
|
194 |
* @param tiefe |
|
195 |
* @param dateizaehler |
|
196 |
*/ |
|
197 |
public void collectFiles(File dir, int tiefe, List beitraege, int maxTiefe, int maxBeitraege) { |
a505a5
|
198 |
logger.fine(dir.getAbsolutePath()); |
96d8cf
|
199 |
List dirs = new ArrayList(); |
U |
200 |
List beitraegeHier = new ArrayList(); |
|
201 |
File[] files = dir.listFiles(); |
|
202 |
for(int i = 0; i < files.length; i++) { |
|
203 |
if(files[i].isDirectory()) { |
|
204 |
if(tiefe < maxTiefe) { |
|
205 |
dirs.add(files[i]); |
|
206 |
} |
|
207 |
} else { |
|
208 |
beitraegeHier.add(files[i]); |
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 |
if(dirs.size() > 0) { |
|
213 |
// hier zuvor die Verzeichnissse absteigend nach Name sortieren |
|
214 |
Collections.sort(dirs, new Comparator<File>() { |
|
215 |
@Override |
|
216 |
public int compare(File o1, File o2) { |
|
217 |
return o2.getName().compareTo(o1.getName()); |
|
218 |
} |
|
219 |
}); |
|
220 |
|
|
221 |
Iterator i = dirs.iterator(); |
|
222 |
while(i.hasNext() && beitraege.size() < maxBeitraege) { |
|
223 |
collectFiles((File) i.next(), tiefe+1, beitraege, maxTiefe, maxBeitraege); |
|
224 |
} |
|
225 |
} |
|
226 |
if(beitraegeHier.size() > 0) { |
|
227 |
// hier zuvor die Liste der Beitraege dieses Ordners nach lastModified absteigend sortieren |
|
228 |
// dann die neuesten in beitraege aufnehmen, bis die maximale Zahl gesuchter |
|
229 |
// neuer Beitraege erreicht ist. |
|
230 |
|
|
231 |
Collections.sort(beitraegeHier, new Comparator<File>() { |
|
232 |
@Override |
|
233 |
public int compare(File o1, File o2) { |
|
234 |
int ergebnis; |
|
235 |
if(o1.lastModified() > o2.lastModified()) { |
|
236 |
ergebnis = -1; |
|
237 |
} else if(o2.lastModified() > o1.lastModified()) { |
|
238 |
ergebnis = 1; |
|
239 |
} else { |
|
240 |
ergebnis = 0; |
|
241 |
} |
|
242 |
return ergebnis; |
|
243 |
} |
|
244 |
}); |
|
245 |
|
|
246 |
Iterator i = beitraegeHier.iterator(); |
|
247 |
while(i.hasNext() && beitraege.size() < maxBeitraege) { |
|
248 |
File bf = (File) i.next(); |
|
249 |
String nm = bf.getName().toLowerCase(); |
|
250 |
if(nm.endsWith(".htmi") || nm.endsWith(".html") || nm.endsWith(".htm") || |
|
251 |
nm.endsWith(".jpg") || nm.endsWith(".jpeg") || nm.endsWith(".png") || |
4ca69b
|
252 |
nm.endsWith(".txt") || nm.endsWith(".md")) { |
96d8cf
|
253 |
beitraege.add(bf); |
U |
254 |
} |
|
255 |
} |
|
256 |
|
|
257 |
} |
|
258 |
} |
|
259 |
|
5ebac8
|
260 |
|
U |
261 |
public int getJNDIInt(String paramName, int defaultVal) { |
|
262 |
String jndiStr = getJNDIParameter(paramName, Integer.toString(defaultVal)); |
|
263 |
try { |
|
264 |
return Integer.parseInt(jndiStr); |
|
265 |
} catch(NumberFormatException ex) { |
|
266 |
logger.log(Level.FINE, ex.getMessage()); |
|
267 |
return defaultVal; |
|
268 |
} |
|
269 |
} |
|
270 |
|
|
271 |
public String getJNDIParameter(String pname, String defaultVal) { |
|
272 |
try { |
|
273 |
// unseren environment naming context ermitteln |
|
274 |
Context initCtx = new InitialContext(); |
|
275 |
Context envCtx = (Context) initCtx.lookup(JNDI_CTX_NAME); |
|
276 |
|
|
277 |
// unseren Parameter lesen |
|
278 |
Object o = envCtx.lookup(pname); |
|
279 |
if(o instanceof String) { |
|
280 |
return o.toString(); |
|
281 |
} else { |
|
282 |
return defaultVal; |
|
283 |
} |
|
284 |
} catch (NamingException ex) { |
|
285 |
logger.log(Level.FINE, ex.getMessage()); |
|
286 |
return defaultVal; |
|
287 |
} |
|
288 |
} |
a01889
|
289 |
} |