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); |
|
98 |
String pubDirName = getJNDIParameter(WbxUtils.WBX_PUB_DIR_NAME, WbxUtils.WBX_DEFAULT_PUB_DIR_NAME); |
|
99 |
String pubUrlName = getJNDIParameter(WbxUtils.WBX_PUB_URL_NAME, WbxUtils.WBX_DEFAULT_PUB_URL_NAME); |
|
100 |
String relPath = relativePath.replace(pubUrlName, pubDirName); |
|
101 |
String absPath = basis + relPath; |
|
102 |
|
|
103 |
ArrayList beitraege = new ArrayList(); |
|
104 |
ArrayList<Inhalt> files = new ArrayList<>(); |
|
105 |
collectFiles(new File(absPath), 0, beitraege, maxTiefe, maxAnzahl); |
|
106 |
|
|
107 |
Iterator i = beitraege.iterator(); |
|
108 |
while(i.hasNext()) { |
|
109 |
File beitrag = (File) i.next(); |
|
110 |
Inhalt cont = new Inhalt(); |
|
111 |
cont.setMimetype(bild.getMimeType(beitrag)); |
|
112 |
cont.setIsDirectory(beitrag.isDirectory()); |
|
113 |
cont.setIsHidden(beitrag.isHidden()); |
|
114 |
cont.setLastModified(beitrag.lastModified()); |
|
115 |
cont.setLength(beitrag.length()); |
7a8d6a
|
116 |
if(length > 0) { |
U |
117 |
cont.setAbst(getFileContent(beitrag, length)); |
|
118 |
} |
708f66
|
119 |
|
U |
120 |
/* |
|
121 |
den 'https://..'-Teil bis vor dem |
|
122 |
ContextPath ermitteln |
|
123 |
*/ |
|
124 |
//String requestUrl = getRequest().getRequestURL().toString(); |
|
125 |
//String contextPath = getRequest().getContextPath(); |
|
126 |
int pos = requestUrl.indexOf(contextPath); |
|
127 |
|
|
128 |
/* |
|
129 |
den Teil des Pfades ermitteln, der zwischen dem |
|
130 |
ContextPath zum oeffentlichen Ordner und dem Dateiname |
|
131 |
steht |
|
132 |
*/ |
|
133 |
String absolutePath = beitrag.getAbsolutePath(); |
|
134 |
absolutePath = absolutePath.replace(beitrag.getName(), ""); |
|
135 |
absolutePath = absolutePath.replace(pubDirName, ""); |
|
136 |
String part = relativePath.replace(pubUrlName, ""); |
|
137 |
int pos2 = absolutePath.indexOf(part); |
|
138 |
String mittelteil = absolutePath.substring(pos2); |
|
139 |
mittelteil = mittelteil.replace(part, ""); |
|
140 |
cont.setBase(requestUrl.substring(0, pos)); |
|
141 |
|
|
142 |
cont.setUrl(/*requestUrl.substring(0, pos) + "/data" + */ mittelteil + beitrag.getName()); |
|
143 |
files.add(cont); |
|
144 |
} |
|
145 |
return files; |
|
146 |
} |
|
147 |
|
7a8d6a
|
148 |
private String getFileContent(File file, int len) { |
27d922
|
149 |
try { |
U |
150 |
StringBuffer readBuffer = new StringBuffer(); |
7a8d6a
|
151 |
char[] buf = new char[1]; |
U |
152 |
FileReader fr = new FileReader(file); |
|
153 |
int bytesRead = fr.read(buf); |
|
154 |
int read = 0; |
|
155 |
while(bytesRead > -1 && read < len) { |
27d922
|
156 |
read += bytesRead; |
7a8d6a
|
157 |
readBuffer.append(buf); |
U |
158 |
bytesRead = fr.read(buf); |
27d922
|
159 |
} |
U |
160 |
readBuffer.append(buf); |
7a8d6a
|
161 |
fr.close(); |
U |
162 |
logger.fine("read: " + read + ", readBuffer.len: " + readBuffer.length()); |
27d922
|
163 |
return readBuffer.toString(); |
U |
164 |
} catch (Exception ex) { |
7a8d6a
|
165 |
logger.log(Level.SEVERE, ex.getMessage(), ex); |
27d922
|
166 |
return EMPTY_STRING; |
U |
167 |
} |
|
168 |
} |
|
169 |
|
708f66
|
170 |
|
96d8cf
|
171 |
/** |
U |
172 |
* Diese Methode funktioniert nur, wenn entweder ein Ordner uebergeben |
|
173 |
* wird, der keine Unterordner enthaelt wie zum Beispiel der Ordner 'neu' |
|
174 |
* der Bildersammlung oder ein Ordner, dessen Unterordner |
|
175 |
* nach dem Schema Jahr, Monat benannt sind wie bei einem Journal, das |
|
176 |
* die Beitraege wie folgt enthaelt: |
|
177 |
* Journal-Ordner |
|
178 |
* 2018 |
|
179 |
* 12 |
|
180 |
* 11 |
|
181 |
* 10 |
|
182 |
* usw. |
|
183 |
* 2017 |
|
184 |
* 12 |
|
185 |
* 11 |
|
186 |
* 10 |
|
187 |
* usw. |
|
188 |
* |
|
189 |
* @param out |
|
190 |
* @param dir |
|
191 |
* @param tiefe |
|
192 |
* @param dateizaehler |
|
193 |
*/ |
|
194 |
public void collectFiles(File dir, int tiefe, List beitraege, int maxTiefe, int maxBeitraege) { |
|
195 |
List dirs = new ArrayList(); |
|
196 |
List beitraegeHier = new ArrayList(); |
|
197 |
File[] files = dir.listFiles(); |
|
198 |
for(int i = 0; i < files.length; i++) { |
|
199 |
if(files[i].isDirectory()) { |
|
200 |
if(tiefe < maxTiefe) { |
|
201 |
dirs.add(files[i]); |
|
202 |
} |
|
203 |
} else { |
|
204 |
beitraegeHier.add(files[i]); |
|
205 |
} |
|
206 |
} |
|
207 |
|
|
208 |
if(dirs.size() > 0) { |
|
209 |
// hier zuvor die Verzeichnissse absteigend nach Name sortieren |
|
210 |
Collections.sort(dirs, new Comparator<File>() { |
|
211 |
@Override |
|
212 |
public int compare(File o1, File o2) { |
|
213 |
return o2.getName().compareTo(o1.getName()); |
|
214 |
} |
|
215 |
}); |
|
216 |
|
|
217 |
Iterator i = dirs.iterator(); |
|
218 |
while(i.hasNext() && beitraege.size() < maxBeitraege) { |
|
219 |
collectFiles((File) i.next(), tiefe+1, beitraege, maxTiefe, maxBeitraege); |
|
220 |
} |
|
221 |
} |
|
222 |
if(beitraegeHier.size() > 0) { |
|
223 |
// hier zuvor die Liste der Beitraege dieses Ordners nach lastModified absteigend sortieren |
|
224 |
// dann die neuesten in beitraege aufnehmen, bis die maximale Zahl gesuchter |
|
225 |
// neuer Beitraege erreicht ist. |
|
226 |
|
|
227 |
Collections.sort(beitraegeHier, new Comparator<File>() { |
|
228 |
@Override |
|
229 |
public int compare(File o1, File o2) { |
|
230 |
int ergebnis; |
|
231 |
if(o1.lastModified() > o2.lastModified()) { |
|
232 |
ergebnis = -1; |
|
233 |
} else if(o2.lastModified() > o1.lastModified()) { |
|
234 |
ergebnis = 1; |
|
235 |
} else { |
|
236 |
ergebnis = 0; |
|
237 |
} |
|
238 |
return ergebnis; |
|
239 |
} |
|
240 |
}); |
|
241 |
|
|
242 |
Iterator i = beitraegeHier.iterator(); |
|
243 |
while(i.hasNext() && beitraege.size() < maxBeitraege) { |
|
244 |
File bf = (File) i.next(); |
|
245 |
String nm = bf.getName().toLowerCase(); |
|
246 |
if(nm.endsWith(".htmi") || nm.endsWith(".html") || nm.endsWith(".htm") || |
|
247 |
nm.endsWith(".jpg") || nm.endsWith(".jpeg") || nm.endsWith(".png") || |
|
248 |
nm.endsWith(".txt")) { |
|
249 |
beitraege.add(bf); |
|
250 |
} |
|
251 |
} |
|
252 |
|
|
253 |
} |
|
254 |
} |
|
255 |
|
5ebac8
|
256 |
|
U |
257 |
public int getJNDIInt(String paramName, int defaultVal) { |
|
258 |
String jndiStr = getJNDIParameter(paramName, Integer.toString(defaultVal)); |
|
259 |
try { |
|
260 |
return Integer.parseInt(jndiStr); |
|
261 |
} catch(NumberFormatException ex) { |
|
262 |
logger.log(Level.FINE, ex.getMessage()); |
|
263 |
return defaultVal; |
|
264 |
} |
|
265 |
} |
|
266 |
|
|
267 |
public String getJNDIParameter(String pname, String defaultVal) { |
|
268 |
try { |
|
269 |
// unseren environment naming context ermitteln |
|
270 |
Context initCtx = new InitialContext(); |
|
271 |
Context envCtx = (Context) initCtx.lookup(JNDI_CTX_NAME); |
|
272 |
|
|
273 |
// unseren Parameter lesen |
|
274 |
Object o = envCtx.lookup(pname); |
|
275 |
if(o instanceof String) { |
|
276 |
return o.toString(); |
|
277 |
} else { |
|
278 |
return defaultVal; |
|
279 |
} |
|
280 |
} catch (NamingException ex) { |
|
281 |
logger.log(Level.FINE, ex.getMessage()); |
|
282 |
return defaultVal; |
|
283 |
} |
|
284 |
} |
a01889
|
285 |
} |