commit | author | age
|
fad719
|
1 |
/*
|
U |
2 |
|
|
3 |
Dateiverwaltung - File management in your browser
|
|
4 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
|
|
5 |
|
|
6 |
This program is free software: you can redistribute it and/or modify
|
|
7 |
it under the terms of the GNU Affero General Public License as
|
|
8 |
published by the Free Software Foundation, either version 3 of the
|
|
9 |
License, or (at your option) any later version.
|
|
10 |
|
|
11 |
This program is distributed in the hope that it will be useful,
|
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
GNU Affero General Public License for more details.
|
|
15 |
|
|
16 |
You should have received a copy of the GNU Affero General Public License
|
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 |
|
|
19 |
*/
|
|
20 |
|
|
21 |
package de.uhilger.filecms.api;
|
|
22 |
|
bb9f8c
|
23 |
import de.uhilger.wbx.Bild;
|
fad719
|
24 |
import java.io.BufferedReader;
|
U |
25 |
import java.io.File;
|
|
26 |
import java.io.FileFilter;
|
|
27 |
import java.io.FileNotFoundException;
|
|
28 |
import java.io.FileReader;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.PrintWriter;
|
|
31 |
import java.util.logging.Level;
|
|
32 |
import java.util.logging.Logger;
|
bb9f8c
|
33 |
import net.coobird.thumbnailator.Thumbnails;
|
fad719
|
34 |
import org.apache.commons.io.FileUtils;
|
U |
35 |
|
|
36 |
/**
|
|
37 |
* Die Klasse HtmlExportService stellt Methoden bereit, mit denen
|
|
38 |
* .htmi-Dateien der WebBox zusammen mit Bildern und Stylesheets als
|
|
39 |
* Ordner exportierten werden koennen, aus denen heraus die Inhalte
|
|
40 |
* als HTML-Seiten im Browser zu oeffnen sind.
|
|
41 |
*/
|
|
42 |
public class HtmlExportService extends Api {
|
|
43 |
|
|
44 |
private static final Logger logger = Logger.getLogger(HtmlExportService.class.getName());
|
|
45 |
|
012d5f
|
46 |
public static final String WBX_APPS_DIR = "wbx.apps";
|
U |
47 |
|
fad719
|
48 |
/**
|
U |
49 |
* Den Inhalt eines Ordners als HTML exportieren
|
|
50 |
*
|
|
51 |
* Annahme hierbei ist, dass der Ordner, auf den im Parameter relPath
|
|
52 |
* verwiesen wird eine oder mehrere .htmi-Dateien enthaelt. Optional
|
|
53 |
* koennen Bilder im Ordner oder seinen Unterordnern enthalten sein, die
|
|
54 |
* dann in derselben Struktur im Ausgabeordner landen.
|
|
55 |
*
|
|
56 |
* Es werden nur .htmi-Dateien exortiert, die im mit relPath bezeichneten
|
|
57 |
* Ordner liegen. Eventuell in darin befindlichen Unterordnern befindliche
|
bf105c
|
58 |
* .htmi-Dateien werden nicht exportiert.
|
fad719
|
59 |
*
|
bf105c
|
60 |
* Es sollen keine komplexen Webseiten-Strukturen mit dieser Methode
|
fad719
|
61 |
* erzeugt werden sondern einfache Dokumente mit einer oder wenigen
|
U |
62 |
* einzelnen Dateien.
|
|
63 |
*
|
|
64 |
* Die Ausgabe erfolgt in den Ordner, in dem der mit relPath bezeichnete
|
|
65 |
* Ordner liegt, also in dessen Eltern-Ordner. Der ausgegebene Ordner heisst
|
|
66 |
* genauso wir der mit relPath bezeichnete Ordner und wird mit der
|
|
67 |
* Ergaenzung _html versehen.
|
|
68 |
*
|
|
69 |
* @param relPath
|
|
70 |
* @return 'ok' oder Fehlermeldung
|
|
71 |
*/
|
|
72 |
public String exportHtml(String relPath) {
|
|
73 |
String result = null;
|
|
74 |
File dir = getTargetDir(relPath);
|
|
75 |
File parentDir = dir.getParentFile();
|
|
76 |
File outDir = new File(parentDir, dir.getName() + "_html/");
|
|
77 |
File[] files = dir.listFiles(new HtmiFileFilter());
|
|
78 |
for(int i = 0; i < files.length; i++) {
|
|
79 |
if (files[i].isDirectory()) {
|
|
80 |
try {
|
|
81 |
FileUtils.copyDirectoryToDirectory(files[i], outDir);
|
|
82 |
} catch (IOException ex) {
|
|
83 |
result = ex.getLocalizedMessage();
|
|
84 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
85 |
}
|
|
86 |
} else {
|
|
87 |
BufferedReader br = null;
|
|
88 |
try {
|
|
89 |
File out = new File(outDir, stripExt(files[i].getName()) + ".html");
|
21589e
|
90 |
if(!out.exists()) {
|
U |
91 |
logger.fine(out.getAbsolutePath() + " existiert nicht, erzeuge Datei..");
|
|
92 |
out.getParentFile().mkdirs();
|
|
93 |
out.createNewFile();
|
|
94 |
}
|
fad719
|
95 |
PrintWriter w = new PrintWriter(out);
|
U |
96 |
printHeader(w);
|
|
97 |
br = new BufferedReader(new FileReader(files[i]));
|
|
98 |
String line = br.readLine();
|
|
99 |
while(line != null) {
|
21589e
|
100 |
w.print(line.replace("htmi", "html"));
|
1c4f4c
|
101 |
w.print("\r\n");
|
fad719
|
102 |
line = br.readLine();
|
U |
103 |
}
|
|
104 |
printFooter(w);
|
|
105 |
w.flush();
|
|
106 |
w.close();
|
|
107 |
br.close();
|
|
108 |
} catch (FileNotFoundException ex) {
|
|
109 |
result = ex.getLocalizedMessage();
|
|
110 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
111 |
} catch (IOException ex) {
|
|
112 |
result = ex.getLocalizedMessage();
|
|
113 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
114 |
} finally {
|
|
115 |
try {
|
|
116 |
if(br != null) {
|
|
117 |
br.close();
|
|
118 |
}
|
|
119 |
} catch (IOException ex) {
|
|
120 |
result = ex.getLocalizedMessage();
|
|
121 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
122 |
}
|
|
123 |
}
|
|
124 |
}
|
|
125 |
}
|
012d5f
|
126 |
//File catalinaBase = new File(getCatalinaBase(getServletContext()));
|
U |
127 |
File catalinaBase = new File(System.getProperty(WBX_APPS_DIR));
|
|
128 |
File bsDir = new File(catalinaBase, "jslib/bootstrap/css/");
|
fad719
|
129 |
try {
|
U |
130 |
FileUtils.copyFile(new File(bsDir, "bootstrap.min.css"), new File(outDir, "bootstrap.min.css"), true);
|
|
131 |
File stile = new File(dir, "stile.css");
|
|
132 |
if(stile.exists()) {
|
|
133 |
FileUtils.copyFile(stile, new File(outDir, "stile.css"), true);
|
|
134 |
}
|
bf105c
|
135 |
buildThumbnailImages(outDir, new Bild());
|
1c4f4c
|
136 |
|
012d5f
|
137 |
File lbDir = new File(catalinaBase, "jslib/lightbox/");
|
1c4f4c
|
138 |
FileUtils.copyDirectoryToDirectory(lbDir, outDir);
|
U |
139 |
|
012d5f
|
140 |
File jqDir = new File(catalinaBase, "jslib/jquery/");
|
1c4f4c
|
141 |
FileUtils.copyDirectoryToDirectory(jqDir, outDir);
|
U |
142 |
|
012d5f
|
143 |
File lbimgDir = new File(catalinaBase, "jslib/lightbox/img");
|
1c4f4c
|
144 |
FileUtils.copyDirectoryToDirectory(lbimgDir, outDir);
|
U |
145 |
|
|
146 |
|
21589e
|
147 |
result = "Export nach HTML ausgefuehrt.";
|
fad719
|
148 |
} catch (IOException ex) {
|
U |
149 |
result = ex.getLocalizedMessage();
|
|
150 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
151 |
}
|
|
152 |
return result;
|
bf105c
|
153 |
}
|
U |
154 |
|
|
155 |
/**
|
|
156 |
* Minituransichten der Bilddateien erzeugen, die beim HTML-Export
|
|
157 |
* entstanden sind.
|
|
158 |
*
|
|
159 |
* Die Methode erstellt fuer alle Dateien mit Endung png oder jpg
|
|
160 |
* eine weitere Bilddatei mit Namen [Name]_tn.[Endung]. Also z.B.
|
|
161 |
* bild_tn.jpg fuer die Datei bild.jpg
|
|
162 |
*/
|
|
163 |
private void buildThumbnailImages(File dir, Bild bild) {
|
|
164 |
logger.fine("Minituransichten dir: " + dir.getAbsolutePath());
|
|
165 |
File[] files = dir.listFiles(new ImageFileFilter());
|
|
166 |
if(files != null) {
|
|
167 |
for(int i = 0; i < files.length; i++) {
|
|
168 |
if(files[i].isDirectory()) {
|
|
169 |
buildThumbnailImages(files[i], bild);
|
|
170 |
} else {
|
|
171 |
String absPath = files[i].getAbsolutePath();
|
|
172 |
logger.fine("Miniaturansicht fuer Bild " + absPath);
|
|
173 |
String fname = files[i].getName();
|
|
174 |
logger.fine("fname: " + fname);
|
|
175 |
int dotPos = fname.lastIndexOf(".");
|
|
176 |
logger.fine("dotPos: " + dotPos);
|
|
177 |
StringBuffer tnFileName = new StringBuffer();
|
|
178 |
if(dotPos > -1) {
|
|
179 |
String fname_no_ext = fname.substring(0, dotPos);
|
|
180 |
logger.fine("fname_no_ext: " + fname_no_ext);
|
|
181 |
String fext = fname.substring(dotPos);
|
|
182 |
logger.fine("fext: " + fext);
|
|
183 |
tnFileName.append(fname_no_ext);
|
|
184 |
tnFileName.append("_tn");
|
|
185 |
//tnFileName.append(".");
|
|
186 |
tnFileName.append(fext);
|
|
187 |
} else {
|
|
188 |
tnFileName.append(fname);
|
|
189 |
tnFileName.append("_tn");
|
|
190 |
}
|
|
191 |
String outFileName = new File(dir, tnFileName.toString()).getAbsolutePath();
|
|
192 |
logger.fine("outFileName: " + outFileName);
|
bb9f8c
|
193 |
|
U |
194 |
try {
|
|
195 |
Thumbnails.of(absPath)
|
|
196 |
.size(bild.getVariantenGroesse(Bild.WINZIG), bild.getVariantenGroesse(Bild.WINZIG))
|
|
197 |
.keepAspectRatio(true)
|
8a8152
|
198 |
.outputQuality(0.7)
|
bb9f8c
|
199 |
.toFile(outFileName);
|
U |
200 |
} catch (IOException ex) {
|
|
201 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
202 |
}
|
|
203 |
/*
|
bf105c
|
204 |
Image image = Toolkit.getDefaultToolkit().getImage(absPath);
|
U |
205 |
MediaTracker mediaTracker = new MediaTracker(new Container());
|
|
206 |
mediaTracker.addImage(image, 0);
|
|
207 |
try {
|
|
208 |
mediaTracker.waitForID(0);
|
|
209 |
|
|
210 |
if (!mediaTracker.isErrorAny()) {
|
|
211 |
bild.writeImageFile(image, bild.getVariantenGroesse(Bild.WINZIG), bild.getMimeType(files[i]), outFileName);
|
|
212 |
} else {
|
|
213 |
logger.fine("Fehler: Miniaturansicht konnte nicht erzeugt werden");
|
|
214 |
}
|
|
215 |
} catch (InterruptedException | IOException ex) {
|
|
216 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
bb9f8c
|
217 |
}
|
U |
218 |
*/
|
bf105c
|
219 |
}
|
U |
220 |
}
|
|
221 |
} else {
|
|
222 |
logger.fine("files ist null");
|
|
223 |
}
|
fad719
|
224 |
}
|
U |
225 |
|
|
226 |
private String stripExt(String name) {
|
|
227 |
int dotpos = name.indexOf(".");
|
|
228 |
if(dotpos > -1) {
|
|
229 |
name = name.substring(0, dotpos);
|
|
230 |
}
|
|
231 |
return name;
|
|
232 |
}
|
|
233 |
|
|
234 |
public class HtmiFileFilter implements FileFilter {
|
|
235 |
|
|
236 |
@Override
|
|
237 |
public boolean accept(File pathname) {
|
|
238 |
boolean doAccept = false;
|
|
239 |
if(pathname.getName().endsWith(".htmi") || pathname.isDirectory()) {
|
|
240 |
doAccept = true;
|
|
241 |
}
|
|
242 |
return doAccept;
|
|
243 |
}
|
|
244 |
|
|
245 |
}
|
|
246 |
|
bf105c
|
247 |
public class ImageFileFilter implements FileFilter {
|
U |
248 |
|
|
249 |
@Override
|
|
250 |
public boolean accept(File pathname) {
|
|
251 |
boolean doAccept = false;
|
|
252 |
String lcName = pathname.getName().toLowerCase();
|
|
253 |
if( lcName.endsWith(".jpg") ||
|
|
254 |
lcName.endsWith(".jpeg") ||
|
|
255 |
lcName.endsWith(".png") ||
|
|
256 |
pathname.isDirectory()) {
|
|
257 |
if(!lcName.contains("_tn")) {
|
|
258 |
doAccept = true;
|
|
259 |
}
|
|
260 |
}
|
|
261 |
return doAccept;
|
|
262 |
}
|
|
263 |
|
|
264 |
}
|
|
265 |
|
fad719
|
266 |
private void printHeader(PrintWriter out) throws IOException {
|
U |
267 |
out.print("<!DOCTYPE html><html><head>\r\n");
|
012d5f
|
268 |
out.print("<meta charset=\"UTF-8\">\r\n");
|
fad719
|
269 |
out.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>");
|
U |
270 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"bootstrap.min.css\">\r\n");
|
1c4f4c
|
271 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"lightbox/lightbox.css\">\r\n");
|
fad719
|
272 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"stile.css\">\r\n");
|
U |
273 |
out.print("</head><body class=\"p-3\">\r\n");
|
|
274 |
}
|
|
275 |
|
|
276 |
private void printFooter(PrintWriter out) throws IOException {
|
1c4f4c
|
277 |
out.print("<script src=\"jquery/jquery.min.js\"></script>\r\n");
|
U |
278 |
out.print("<script src=\"lightbox/lightbox.min.js\"></script>\r\n");
|
fad719
|
279 |
out.print("</body></html>");
|
U |
280 |
}
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
}
|