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)
|
|
195 |
.toFile(outFileName);
|
|
196 |
} catch (IOException ex) {
|
|
197 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
198 |
}
|
|
199 |
/*
|
bf105c
|
200 |
Image image = Toolkit.getDefaultToolkit().getImage(absPath);
|
U |
201 |
MediaTracker mediaTracker = new MediaTracker(new Container());
|
|
202 |
mediaTracker.addImage(image, 0);
|
|
203 |
try {
|
|
204 |
mediaTracker.waitForID(0);
|
|
205 |
|
|
206 |
if (!mediaTracker.isErrorAny()) {
|
|
207 |
bild.writeImageFile(image, bild.getVariantenGroesse(Bild.WINZIG), bild.getMimeType(files[i]), outFileName);
|
|
208 |
} else {
|
|
209 |
logger.fine("Fehler: Miniaturansicht konnte nicht erzeugt werden");
|
|
210 |
}
|
|
211 |
} catch (InterruptedException | IOException ex) {
|
|
212 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
bb9f8c
|
213 |
}
|
U |
214 |
*/
|
bf105c
|
215 |
}
|
U |
216 |
}
|
|
217 |
} else {
|
|
218 |
logger.fine("files ist null");
|
|
219 |
}
|
fad719
|
220 |
}
|
U |
221 |
|
|
222 |
private String stripExt(String name) {
|
|
223 |
int dotpos = name.indexOf(".");
|
|
224 |
if(dotpos > -1) {
|
|
225 |
name = name.substring(0, dotpos);
|
|
226 |
}
|
|
227 |
return name;
|
|
228 |
}
|
|
229 |
|
|
230 |
public class HtmiFileFilter implements FileFilter {
|
|
231 |
|
|
232 |
@Override
|
|
233 |
public boolean accept(File pathname) {
|
|
234 |
boolean doAccept = false;
|
|
235 |
if(pathname.getName().endsWith(".htmi") || pathname.isDirectory()) {
|
|
236 |
doAccept = true;
|
|
237 |
}
|
|
238 |
return doAccept;
|
|
239 |
}
|
|
240 |
|
|
241 |
}
|
|
242 |
|
bf105c
|
243 |
public class ImageFileFilter implements FileFilter {
|
U |
244 |
|
|
245 |
@Override
|
|
246 |
public boolean accept(File pathname) {
|
|
247 |
boolean doAccept = false;
|
|
248 |
String lcName = pathname.getName().toLowerCase();
|
|
249 |
if( lcName.endsWith(".jpg") ||
|
|
250 |
lcName.endsWith(".jpeg") ||
|
|
251 |
lcName.endsWith(".png") ||
|
|
252 |
pathname.isDirectory()) {
|
|
253 |
if(!lcName.contains("_tn")) {
|
|
254 |
doAccept = true;
|
|
255 |
}
|
|
256 |
}
|
|
257 |
return doAccept;
|
|
258 |
}
|
|
259 |
|
|
260 |
}
|
|
261 |
|
fad719
|
262 |
private void printHeader(PrintWriter out) throws IOException {
|
U |
263 |
out.print("<!DOCTYPE html><html><head>\r\n");
|
|
264 |
out.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>");
|
|
265 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"bootstrap.min.css\">\r\n");
|
1c4f4c
|
266 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"lightbox/lightbox.css\">\r\n");
|
fad719
|
267 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"stile.css\">\r\n");
|
U |
268 |
out.print("</head><body class=\"p-3\">\r\n");
|
|
269 |
}
|
|
270 |
|
|
271 |
private void printFooter(PrintWriter out) throws IOException {
|
1c4f4c
|
272 |
out.print("<script src=\"jquery/jquery.min.js\"></script>\r\n");
|
U |
273 |
out.print("<script src=\"lightbox/lightbox.min.js\"></script>\r\n");
|
fad719
|
274 |
out.print("</body></html>");
|
U |
275 |
}
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
}
|