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