55c4e8016561f7e3decb484088c349aed5aa6be9
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / layouts / MixedLayout.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012 -- 2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.htmlindexer.layouts;
11
12 import eu.svjatoslav.commons.file.FilePathParser;
13 import eu.svjatoslav.meviz.htmlindexer.*;
14 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
15 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
16 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
17 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
18 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
19 import org.apache.log4j.Logger;
20
21 import java.io.IOException;
22 import java.util.List;
23
24 public class MixedLayout implements Layout {
25
26     public static final String encoding = "UTF-8";
27
28     private final Logger logger = Logger.getLogger(MixedLayout.class);
29
30     private StringBuffer dirHtml = new StringBuffer();
31
32     private StringBuffer filesHtml = new StringBuffer();
33
34     private String galleryTitle;
35
36     private List<String> path;
37
38     private AbstractIndexer indexer;
39
40     private IndexingContext indexingContext;
41
42     @Override
43     public void enlistDirectory(final AbstractFile directory,
44                                 final DirectoryMetadata parentDirectoryMetadata) {
45         dirHtml.append("<TR>");
46         dirHtml.append("<TD><b><a href=\""
47                 + indexer.getDirectoryUrl(directory, indexingContext)
48                 + "\"><h2> ==&gt; " + directory.fileName + "</h2></a></b></TD>");
49         dirHtml.append("</TR>\n");
50
51     }
52
53     @Override
54     public void enlistFile(final AbstractFile file,
55                            final DirectoryMetadata parentDirectoryMetadata) {
56
57         final String fullFileName = file.fileName;
58
59         filesHtml.append("<div style=\"clear: both;\"><a href=\""
60                 + indexingContext.getGlobalUrl()
61                 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
62                 + fullFileName) + "\">" + fullFileName + "</a>\n");
63         filesHtml.append("&nbsp;&nbsp;("
64                 + FilePathParser.getFileSizeDescription(file.getFileLength())
65                 + ")</div><br/>\n");
66
67     }
68
69     @Override
70     public void enlistImage(final Picture picture,
71                             final DirectoryMetadata parentDirectoryMetadata) {
72         try {
73             enlistImageInternal(picture);
74         } catch (final Exception e) {
75             logger.error(
76                     "Error generating thumbnails for image file: "
77                             + e.toString()
78                             + ", thumbnail generation skipped, enlisting as ordinary file instead.",
79                     e);
80             enlistFile(picture, parentDirectoryMetadata);
81         }
82     }
83
84     private String enlistImageDimensions(final Picture picture) {
85         final StringBuilder html = new StringBuilder();
86
87         final List<Dimension> recommendedDimensions = Picture
88                 .getThumbnailDimensions(picture.getDimensions());
89
90         int count = 0;
91         for (final Dimension dimension : recommendedDimensions) {
92             count++;
93             if (count > 1)
94                 html.append(", ");
95             html.append("<a href=\""
96                     + indexer.getThumbnailUrl(picture, dimension,
97                     indexingContext) + "\">");
98             html.append(dimension.width + "x" + dimension.height);
99             html.append("</a>");
100         }
101
102         return html.toString();
103     }
104
105     private void enlistImageInternal(final Picture picture) {
106
107         filesHtml.append("<div style=\"float:left; margin: 10px;\">");
108
109         // clickable image thumbnail
110         filesHtml.append("<a href=\""
111                 + indexingContext.getGlobalUrl()
112                 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
113                 + picture.fileName) + "\">\n");
114
115         filesHtml.append("<img border=\"0\" src=\""
116                 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
117
118         // file name
119         filesHtml.append("<b>"
120                 + FilePathParser.getFileNameWithoutExtension(picture.fileName)
121                 + "</b><br/>");
122
123         // links to thumbnails of various sizes
124         filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
125
126         filesHtml.append("</div>");
127     }
128
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public void enlistOgv(final AbstractFile abstractFile,
134                           final DirectoryMetadata parentDirectoryMetadata) {
135
136         final GeneralFile file = (GeneralFile) abstractFile;
137
138         filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
139
140         // watchable video
141         filesHtml
142                 .append("<video preload=\"metadata\" controls=\"controls\" tabindex=\"0\">\n");
143         filesHtml
144                 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
145                         + indexingContext.getGlobalUrl()
146                         + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
147                         + file.fileName) + "\">\n");
148         filesHtml.append("</source>\n");
149         filesHtml.append("</video>\n");
150         filesHtml.append("<br/>\n");
151
152         // video title
153         filesHtml
154                 .append("Video: <b>"
155                         + FilePathParser
156                         .getFileNameWithoutExtension(file.fileName)
157                         + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
158
159         // video download link
160         filesHtml.append("<a href=\""
161                 + indexingContext.getGlobalUrl()
162                 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
163                 + file.fileName) + "\">download as OGV file</a>");
164
165         filesHtml.append("</div>\n");
166
167         filesHtml.append("\n");
168
169     }
170
171     /**
172      * {@inheritDoc}
173      */
174     @Override
175     public String getFileNameSuffix() {
176         return "";
177     }
178
179     /**
180      * {@inheritDoc}
181      *
182      * @throws IOException
183      */
184     @Override
185     public String getHtml(final boolean showTitle,
186                           final boolean doHtmlHeaderAndFooter,
187                           final DirectoryMetadata directory) {
188
189         // assemble final page HTML
190         final StringBuilder html = new StringBuilder();
191
192         if (doHtmlHeaderAndFooter)
193             appendHtmlHeader(html);
194
195         if (showTitle)
196             html.append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
197
198         html.append(showCurrentPath());
199         html.append(listDirectories());
200         html.append(directory.getHeaderMevizMetadataHtml());
201         html.append(filesHtml.toString());
202
203         if (doHtmlHeaderAndFooter)
204             appendHtmlFooter(html);
205
206         return html.toString();
207     }
208
209     private void appendHtmlFooter(StringBuilder html) {
210         html.append("</font></BODY></HTML>\n");
211     }
212
213     private void appendHtmlHeader(StringBuilder html) {
214         // page header
215         html.append("<HTML>\n");
216         html.append("<HEAD>\n");
217         html.append("    <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
218         html.append("</HEAD>\n");
219         html.append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
220         html.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
221         html.append("<!-- " + Constants.HTML_MEMO + " -->\n");
222         html.append("<font color=\"#ffffff\">\n");
223     }
224
225     private String getImageThumbnailUrl(final Picture picture) {
226         final Dimension imageSize = picture.getDimensions();
227
228         Dimension listingDimension = new Dimension(1500, 450);
229
230         if (listingDimension.width > imageSize.width)
231             if (listingDimension.height > imageSize.height)
232                 listingDimension = imageSize;
233
234         return indexer.getThumbnailUrl(picture, listingDimension,
235                 indexingContext);
236     }
237
238     @Override
239     public void init(final String galleryTitle, final List<String> path,
240                      final AbstractIndexer indexer, final IndexingContext indexingContext) {
241
242         this.indexer = indexer;
243         this.galleryTitle = galleryTitle;
244         this.path = path;
245         this.indexingContext = indexingContext;
246
247         dirHtml = new StringBuffer();
248         filesHtml = new StringBuffer();
249     }
250
251     private String listDirectories() {
252         final StringBuilder html = new StringBuilder();
253         html.append("<TABLE>\n");
254
255         if (path.size() > 0) {
256             html.append("<TR>");
257             html.append("<TD><b><a href=\""
258                     + indexer.getParentDirectoryUrl(indexingContext)
259                     + "\"><h2> &lt;== parent directory</h2></a></b></TD>");
260             html.append("</TR>\n");
261         }
262
263         html.append(dirHtml.toString());
264         html.append("\n<TR><TD><br></TD></TR>\n");
265         html.append("</TABLE>\n");
266
267         return html.toString();
268     }
269
270     private String showCurrentPath() {
271         final StringBuilder html = new StringBuilder();
272         html.append("<CENTER><H2>");
273         for (final String p : path)
274             html.append(" / " + p);
275         html.append("</H2></CENTER>\n");
276
277         return html.toString();
278     }
279
280     @Override
281     public void enlistWebm(AbstractFile abstractFile,
282                            DirectoryMetadata directory) {
283         final GeneralFile file = (GeneralFile) abstractFile;
284
285         filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
286
287         // watchable video
288
289         String fileUrl = indexingContext.getGlobalUrl()
290                 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
291                 + file.fileName);
292
293         filesHtml.append("<video controls>\n");
294         filesHtml.append("  <source src=\"" + fileUrl
295                 + "\" type='video/webm;codecs=\"vp8, opus\"'/>\n");
296         filesHtml.append("</video>\n");
297         filesHtml.append("<br/>\n");
298
299         // video title
300         filesHtml
301                 .append("Video: <b>"
302                         + FilePathParser
303                         .getFileNameWithoutExtension(file.fileName)
304                         + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
305
306         // video download link
307         filesHtml.append("<a href=\""
308                 + indexingContext.getGlobalUrl()
309                 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
310                 + file.fileName) + "\">download as WebM file</a>");
311
312         filesHtml.append("</div>\n");
313
314         filesHtml.append("\n");
315     }
316 }