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