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 |
|
|
46 |
/**
|
|
47 |
* Den Inhalt eines Ordners als HTML exportieren
|
|
48 |
*
|
|
49 |
* Annahme hierbei ist, dass der Ordner, auf den im Parameter relPath
|
|
50 |
* verwiesen wird eine oder mehrere .htmi-Dateien enthaelt. Optional
|
|
51 |
* koennen Bilder im Ordner oder seinen Unterordnern enthalten sein, die
|
|
52 |
* dann in derselben Struktur im Ausgabeordner landen.
|
|
53 |
*
|
|
54 |
* Es werden nur .htmi-Dateien exortiert, die im mit relPath bezeichneten
|
|
55 |
* Ordner liegen. Eventuell in darin befindlichen Unterordnern befindliche
|
bf105c
|
56 |
* .htmi-Dateien werden nicht exportiert.
|
fad719
|
57 |
*
|
bf105c
|
58 |
* Es sollen keine komplexen Webseiten-Strukturen mit dieser Methode
|
fad719
|
59 |
* erzeugt werden sondern einfache Dokumente mit einer oder wenigen
|
U |
60 |
* einzelnen Dateien.
|
|
61 |
*
|
|
62 |
* Die Ausgabe erfolgt in den Ordner, in dem der mit relPath bezeichnete
|
|
63 |
* Ordner liegt, also in dessen Eltern-Ordner. Der ausgegebene Ordner heisst
|
|
64 |
* genauso wir der mit relPath bezeichnete Ordner und wird mit der
|
|
65 |
* Ergaenzung _html versehen.
|
|
66 |
*
|
|
67 |
* @param relPath
|
|
68 |
* @return 'ok' oder Fehlermeldung
|
|
69 |
*/
|
|
70 |
public String exportHtml(String relPath) {
|
|
71 |
String result = null;
|
|
72 |
File dir = getTargetDir(relPath);
|
|
73 |
File parentDir = dir.getParentFile();
|
|
74 |
File outDir = new File(parentDir, dir.getName() + "_html/");
|
|
75 |
File[] files = dir.listFiles(new HtmiFileFilter());
|
|
76 |
for(int i = 0; i < files.length; i++) {
|
|
77 |
if (files[i].isDirectory()) {
|
|
78 |
try {
|
|
79 |
FileUtils.copyDirectoryToDirectory(files[i], outDir);
|
|
80 |
} catch (IOException ex) {
|
|
81 |
result = ex.getLocalizedMessage();
|
|
82 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
83 |
}
|
|
84 |
} else {
|
|
85 |
BufferedReader br = null;
|
|
86 |
try {
|
|
87 |
File out = new File(outDir, stripExt(files[i].getName()) + ".html");
|
21589e
|
88 |
if(!out.exists()) {
|
U |
89 |
logger.fine(out.getAbsolutePath() + " existiert nicht, erzeuge Datei..");
|
|
90 |
out.getParentFile().mkdirs();
|
|
91 |
out.createNewFile();
|
|
92 |
}
|
fad719
|
93 |
PrintWriter w = new PrintWriter(out);
|
U |
94 |
printHeader(w);
|
|
95 |
br = new BufferedReader(new FileReader(files[i]));
|
|
96 |
String line = br.readLine();
|
|
97 |
while(line != null) {
|
21589e
|
98 |
w.print(line.replace("htmi", "html"));
|
1c4f4c
|
99 |
w.print("\r\n");
|
fad719
|
100 |
line = br.readLine();
|
U |
101 |
}
|
|
102 |
printFooter(w);
|
|
103 |
w.flush();
|
|
104 |
w.close();
|
|
105 |
br.close();
|
|
106 |
} catch (FileNotFoundException ex) {
|
|
107 |
result = ex.getLocalizedMessage();
|
|
108 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
109 |
} catch (IOException ex) {
|
|
110 |
result = ex.getLocalizedMessage();
|
|
111 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
112 |
} finally {
|
|
113 |
try {
|
|
114 |
if(br != null) {
|
|
115 |
br.close();
|
|
116 |
}
|
|
117 |
} catch (IOException ex) {
|
|
118 |
result = ex.getLocalizedMessage();
|
|
119 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
120 |
}
|
|
121 |
}
|
|
122 |
}
|
|
123 |
}
|
6e1a29
|
124 |
File catalinaBase = new File(getCatalinaBase(getServletContext()));
|
fad719
|
125 |
File bsDir = new File(catalinaBase, "webapps/jslib/bootstrap/css/");
|
U |
126 |
try {
|
|
127 |
FileUtils.copyFile(new File(bsDir, "bootstrap.min.css"), new File(outDir, "bootstrap.min.css"), true);
|
|
128 |
File stile = new File(dir, "stile.css");
|
|
129 |
if(stile.exists()) {
|
|
130 |
FileUtils.copyFile(stile, new File(outDir, "stile.css"), true);
|
|
131 |
}
|
bf105c
|
132 |
buildThumbnailImages(outDir, new Bild());
|
1c4f4c
|
133 |
|
U |
134 |
File lbDir = new File(catalinaBase, "webapps/jslib/lightbox/");
|
|
135 |
FileUtils.copyDirectoryToDirectory(lbDir, outDir);
|
|
136 |
|
|
137 |
File jqDir = new File(catalinaBase, "webapps/jslib/jquery/");
|
|
138 |
FileUtils.copyDirectoryToDirectory(jqDir, outDir);
|
|
139 |
|
|
140 |
File lbimgDir = new File(catalinaBase, "webapps/jslib/lightbox/img");
|
|
141 |
FileUtils.copyDirectoryToDirectory(lbimgDir, outDir);
|
|
142 |
|
|
143 |
|
21589e
|
144 |
result = "Export nach HTML ausgefuehrt.";
|
fad719
|
145 |
} catch (IOException ex) {
|
U |
146 |
result = ex.getLocalizedMessage();
|
|
147 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
148 |
}
|
|
149 |
return result;
|
bf105c
|
150 |
}
|
U |
151 |
|
|
152 |
/**
|
|
153 |
* Minituransichten der Bilddateien erzeugen, die beim HTML-Export
|
|
154 |
* entstanden sind.
|
|
155 |
*
|
|
156 |
* Die Methode erstellt fuer alle Dateien mit Endung png oder jpg
|
|
157 |
* eine weitere Bilddatei mit Namen [Name]_tn.[Endung]. Also z.B.
|
|
158 |
* bild_tn.jpg fuer die Datei bild.jpg
|
|
159 |
*/
|
|
160 |
private void buildThumbnailImages(File dir, Bild bild) {
|
|
161 |
logger.fine("Minituransichten dir: " + dir.getAbsolutePath());
|
|
162 |
File[] files = dir.listFiles(new ImageFileFilter());
|
|
163 |
if(files != null) {
|
|
164 |
for(int i = 0; i < files.length; i++) {
|
|
165 |
if(files[i].isDirectory()) {
|
|
166 |
buildThumbnailImages(files[i], bild);
|
|
167 |
} else {
|
|
168 |
String absPath = files[i].getAbsolutePath();
|
|
169 |
logger.fine("Miniaturansicht fuer Bild " + absPath);
|
|
170 |
String fname = files[i].getName();
|
|
171 |
logger.fine("fname: " + fname);
|
|
172 |
int dotPos = fname.lastIndexOf(".");
|
|
173 |
logger.fine("dotPos: " + dotPos);
|
|
174 |
StringBuffer tnFileName = new StringBuffer();
|
|
175 |
if(dotPos > -1) {
|
|
176 |
String fname_no_ext = fname.substring(0, dotPos);
|
|
177 |
logger.fine("fname_no_ext: " + fname_no_ext);
|
|
178 |
String fext = fname.substring(dotPos);
|
|
179 |
logger.fine("fext: " + fext);
|
|
180 |
tnFileName.append(fname_no_ext);
|
|
181 |
tnFileName.append("_tn");
|
|
182 |
//tnFileName.append(".");
|
|
183 |
tnFileName.append(fext);
|
|
184 |
} else {
|
|
185 |
tnFileName.append(fname);
|
|
186 |
tnFileName.append("_tn");
|
|
187 |
}
|
|
188 |
String outFileName = new File(dir, tnFileName.toString()).getAbsolutePath();
|
|
189 |
logger.fine("outFileName: " + outFileName);
|
bb9f8c
|
190 |
|
U |
191 |
try {
|
|
192 |
Thumbnails.of(absPath)
|
|
193 |
.size(bild.getVariantenGroesse(Bild.WINZIG), bild.getVariantenGroesse(Bild.WINZIG))
|
|
194 |
.keepAspectRatio(true)
|
8a8152
|
195 |
.outputQuality(0.7)
|
bb9f8c
|
196 |
.toFile(outFileName);
|
U |
197 |
} catch (IOException ex) {
|
|
198 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
199 |
}
|
|
200 |
/*
|
bf105c
|
201 |
Image image = Toolkit.getDefaultToolkit().getImage(absPath);
|
U |
202 |
MediaTracker mediaTracker = new MediaTracker(new Container());
|
|
203 |
mediaTracker.addImage(image, 0);
|
|
204 |
try {
|
|
205 |
mediaTracker.waitForID(0);
|
|
206 |
|
|
207 |
if (!mediaTracker.isErrorAny()) {
|
|
208 |
bild.writeImageFile(image, bild.getVariantenGroesse(Bild.WINZIG), bild.getMimeType(files[i]), outFileName);
|
|
209 |
} else {
|
|
210 |
logger.fine("Fehler: Miniaturansicht konnte nicht erzeugt werden");
|
|
211 |
}
|
|
212 |
} catch (InterruptedException | IOException ex) {
|
|
213 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
bb9f8c
|
214 |
}
|
U |
215 |
*/
|
bf105c
|
216 |
}
|
U |
217 |
}
|
|
218 |
} else {
|
|
219 |
logger.fine("files ist null");
|
|
220 |
}
|
fad719
|
221 |
}
|
U |
222 |
|
|
223 |
private String stripExt(String name) {
|
|
224 |
int dotpos = name.indexOf(".");
|
|
225 |
if(dotpos > -1) {
|
|
226 |
name = name.substring(0, dotpos);
|
|
227 |
}
|
|
228 |
return name;
|
|
229 |
}
|
|
230 |
|
|
231 |
public class HtmiFileFilter implements FileFilter {
|
|
232 |
|
|
233 |
@Override
|
|
234 |
public boolean accept(File pathname) {
|
|
235 |
boolean doAccept = false;
|
|
236 |
if(pathname.getName().endsWith(".htmi") || pathname.isDirectory()) {
|
|
237 |
doAccept = true;
|
|
238 |
}
|
|
239 |
return doAccept;
|
|
240 |
}
|
|
241 |
|
|
242 |
}
|
|
243 |
|
bf105c
|
244 |
public class ImageFileFilter implements FileFilter {
|
U |
245 |
|
|
246 |
@Override
|
|
247 |
public boolean accept(File pathname) {
|
|
248 |
boolean doAccept = false;
|
|
249 |
String lcName = pathname.getName().toLowerCase();
|
|
250 |
if( lcName.endsWith(".jpg") ||
|
|
251 |
lcName.endsWith(".jpeg") ||
|
|
252 |
lcName.endsWith(".png") ||
|
|
253 |
pathname.isDirectory()) {
|
|
254 |
if(!lcName.contains("_tn")) {
|
|
255 |
doAccept = true;
|
|
256 |
}
|
|
257 |
}
|
|
258 |
return doAccept;
|
|
259 |
}
|
|
260 |
|
|
261 |
}
|
|
262 |
|
fad719
|
263 |
private void printHeader(PrintWriter out) throws IOException {
|
U |
264 |
out.print("<!DOCTYPE html><html><head>\r\n");
|
|
265 |
out.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>");
|
|
266 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"bootstrap.min.css\">\r\n");
|
1c4f4c
|
267 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"lightbox/lightbox.css\">\r\n");
|
fad719
|
268 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"stile.css\">\r\n");
|
U |
269 |
out.print("</head><body class=\"p-3\">\r\n");
|
|
270 |
}
|
|
271 |
|
|
272 |
private void printFooter(PrintWriter out) throws IOException {
|
1c4f4c
|
273 |
out.print("<script src=\"jquery/jquery.min.js\"></script>\r\n");
|
U |
274 |
out.print("<script src=\"lightbox/lightbox.min.js\"></script>\r\n");
|
fad719
|
275 |
out.print("</body></html>");
|
U |
276 |
}
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
}
|