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