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