added webm support
[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 java.io.IOException;
13 import java.io.UnsupportedEncodingException;
14 import java.util.List;
15
16 import org.apache.log4j.Logger;
17
18 import eu.svjatoslav.commons.file.FilePathParser;
19 import eu.svjatoslav.commons.network.UrlParamEncoder;
20 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
21 import eu.svjatoslav.meviz.htmlindexer.Constants;
22 import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
23 import eu.svjatoslav.meviz.htmlindexer.Layout;
24 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
25 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
26 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
27 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
28 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
29
30 public class MixedLayout implements Layout {
31
32         public static final String encoding = "UTF-8";
33
34         Logger logger = Logger.getLogger(MixedLayout.class);
35
36         StringBuffer dirHtml = new StringBuffer();
37
38         StringBuffer filesHtml = new StringBuffer();
39
40         String galleryTitle;
41
42         List<String> path;
43
44         private AbstractIndexer indexer;
45
46         private IndexingContext indexingContext;
47
48         @Override
49         public void enlistDirectory(final AbstractFile directory,
50                         final DirectoryMetadata parentDirectoryMetadata) {
51                 dirHtml.append("<TR>");
52                 dirHtml.append("<TD><b><a href=\""
53                                 + indexer.getDirectoryUrl(directory, indexingContext)
54                                 + "\"><h2> ==&gt; " + directory.fileName + "</h2></a></b></TD>");
55                 dirHtml.append("</TR>\n");
56
57         }
58
59         @Override
60         public void enlistFile(final AbstractFile file,
61                         final DirectoryMetadata parentDirectoryMetadata) {
62
63                 final String fullFileName = file.fileName;
64
65                 filesHtml.append("<div style=\"clear: both;\"><a href=\""
66                                 + indexingContext.getGlobalUrl()
67                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
68                                                 + fullFileName) + "\">" + fullFileName + "</a>\n");
69                 filesHtml.append("&nbsp;&nbsp;("
70                                 + FilePathParser.getFileSizeDescription(file.getFileLength())
71                                 + ")</div><br/>\n");
72
73         }
74
75         @Override
76         public void enlistImage(final Picture picture,
77                         final DirectoryMetadata parentDirectoryMetadata) {
78                 try {
79                         enlistImageInternal(picture);
80                 } catch (final Exception e) {
81                         logger.error(
82                                         "Error generating thumbnails for image file: "
83                                                         + e.toString()
84                                                         + ", thumbnail generation skipped, enlisting as ordinary file instead.",
85                                         e);
86                         enlistFile(picture, parentDirectoryMetadata);
87                 }
88         }
89
90         private String enlistImageDimensions(final Picture picture) {
91                 final StringBuilder html = new StringBuilder();
92
93                 final List<Dimension> recommendedDimensions = Picture
94                                 .getThumbnailDimensions(picture.getDimensions());
95
96                 int count = 0;
97                 for (final Dimension dimension : recommendedDimensions) {
98                         count++;
99                         if (count > 1)
100                                 html.append(", ");
101                         html.append("<a href=\""
102                                         + indexer.getThumbnailUrl(picture, dimension,
103                                                         indexingContext) + "\">");
104                         html.append(dimension.width + "x" + dimension.height);
105                         html.append("</a>");
106                 }
107
108                 return html.toString();
109         }
110
111         private void enlistImageInternal(final Picture picture) {
112
113                 filesHtml.append("<div style=\"float:left; margin: 10px;\">");
114
115                 // clickable image thumbnail
116                 filesHtml.append("<a href=\""
117                                 + indexingContext.getGlobalUrl()
118                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
119                                                 + picture.fileName) + "\">\n");
120                 filesHtml.append("<img border=\"0\" src=\""
121                                 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
122
123                 // file name
124                 filesHtml.append("<b>"
125                                 + FilePathParser.getFileNameWithoutExtension(picture.fileName)
126                                 + "</b><br/>");
127
128                 // links to thumbnails of various sizes
129                 filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
130
131                 filesHtml.append("</div>");
132         }
133
134         /** {@inheritDoc} */
135         @Override
136         public void enlistOgv(final AbstractFile abstractFile,
137                         final DirectoryMetadata parentDirectoryMetadata)
138                         throws UnsupportedEncodingException {
139
140                 final GeneralFile file = (GeneralFile) abstractFile;
141
142                 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
143
144                 // watchable video
145                 filesHtml
146                                 .append("<video preload=\"metadata\" controls=\"controls\" tabindex=\"0\">\n");
147                 filesHtml
148                                 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
149                                                 + indexingContext.getGlobalUrl()
150                                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
151                                                                 + "/" + file.fileName) + "\">\n");
152                 filesHtml.append("</source>\n");
153                 filesHtml.append("</video>\n");
154                 filesHtml.append("<br/>\n");
155
156                 // video title
157                 filesHtml
158                                 .append("Video: <b>"
159                                                 + FilePathParser
160                                                                 .getFileNameWithoutExtension(file.fileName)
161                                                 + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
162
163                 // video download link
164                 filesHtml.append("<a href=\""
165                                 + indexingContext.getGlobalUrl()
166                                 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
167                                                 + file.fileName) + "\">download as OGV</a>");
168
169                 filesHtml.append("</div>\n");
170
171                 filesHtml.append("\n");
172
173         }
174
175         /** {@inheritDoc} */
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 StringBuffer html = new StringBuffer();
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 }