aae1a195fe4df2809774ff257c98372133513563
[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.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             // page header
194             html.append("<HTML>\n");
195             html.append("<HEAD>\n");
196             html.append("    <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
197             html.append("</HEAD>\n");
198             html.append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
199             html.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
200             html.append("<!-- " + Constants.HTML_MEMO + " -->\n");
201             html.append("<font color=\"#ffffff\">\n");
202         }
203
204         if (showTitle)
205             html.append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
206
207         html.append(showCurrentPath());
208         html.append(listDirectories());
209         html.append(directory.getHeaderMevizMetadataHtml());
210         html.append(filesHtml.toString());
211
212         if (doHtmlHeaderAndFooter)
213             html.append("</font></BODY></HTML>\n");
214
215         return html.toString();
216     }
217
218     private String getImageThumbnailUrl(final Picture picture) {
219         final Dimension imageSize = picture.getDimensions();
220
221         Dimension listingDimension = new Dimension(1500, 450);
222
223         if (listingDimension.width > imageSize.width)
224             if (listingDimension.height > imageSize.height)
225                 listingDimension = imageSize;
226
227         return indexer.getThumbnailUrl(picture, listingDimension,
228                 indexingContext);
229     }
230
231     @Override
232     public void init(final String galleryTitle, final List<String> path,
233                      final AbstractIndexer indexer, final IndexingContext indexingContext) {
234
235         this.indexer = indexer;
236         this.galleryTitle = galleryTitle;
237         this.path = path;
238         this.indexingContext = indexingContext;
239
240         dirHtml = new StringBuffer();
241         filesHtml = new StringBuffer();
242     }
243
244     private String listDirectories() {
245         final StringBuilder html = new StringBuilder();
246         html.append("<TABLE>\n");
247
248         if (path.size() > 0) {
249             html.append("<TR>");
250             html.append("<TD><b><a href=\""
251                     + indexer.getParentDirectoryUrl(indexingContext)
252                     + "\"><h2> &lt;== parent directory</h2></a></b></TD>");
253             html.append("</TR>\n");
254         }
255
256         html.append(dirHtml.toString());
257         html.append("\n<TR><TD><br></TD></TR>\n");
258         html.append("</TABLE>\n");
259
260         return html.toString();
261     }
262
263     private String showCurrentPath() {
264         final StringBuilder html = new StringBuilder();
265         html.append("<CENTER><H2>");
266         for (final String p : path)
267             html.append(" / " + p);
268         html.append("</H2></CENTER>\n");
269
270         return html.toString();
271     }
272
273     @Override
274     public void enlistWebm(AbstractFile abstractFile,
275                            DirectoryMetadata directory) {
276         final GeneralFile file = (GeneralFile) abstractFile;
277
278         filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
279
280         // watchable video
281
282         String fileUrl = indexingContext.getGlobalUrl()
283                 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
284                 + file.fileName);
285
286         filesHtml.append("<video controls>\n");
287         filesHtml.append("  <source src=\"" + fileUrl
288                 + "\" type='video/webm;codecs=\"vp8, opus\"'/>\n");
289         filesHtml.append("</video>\n");
290         filesHtml.append("<br/>\n");
291
292         // video title
293         filesHtml
294                 .append("Video: <b>"
295                         + FilePathParser
296                         .getFileNameWithoutExtension(file.fileName)
297                         + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
298
299         // video download link
300         filesHtml.append("<a href=\""
301                 + indexingContext.getGlobalUrl()
302                 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
303                 + file.fileName) + "\">download as WebM file</a>");
304
305         filesHtml.append("</div>\n");
306
307         filesHtml.append("\n");
308     }
309 }