2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.meviz.htmlindexer.layouts;
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;
21 import java.io.IOException;
22 import java.io.UnsupportedEncodingException;
23 import java.util.List;
25 public class MixedLayout implements Layout {
27 public static final String encoding = "UTF-8";
29 Logger logger = Logger.getLogger(MixedLayout.class);
31 StringBuffer dirHtml = new StringBuffer();
33 StringBuffer filesHtml = new StringBuffer();
39 private AbstractIndexer indexer;
41 private IndexingContext indexingContext;
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> ==> " + directory.fileName + "</h2></a></b></TD>");
50 dirHtml.append("</TR>\n");
55 public void enlistFile(final AbstractFile file,
56 final DirectoryMetadata parentDirectoryMetadata) {
58 final String fullFileName = file.fileName;
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(" ("
65 + FilePathParser.getFileSizeDescription(file.getFileLength())
71 public void enlistImage(final Picture picture,
72 final DirectoryMetadata parentDirectoryMetadata) {
74 enlistImageInternal(picture);
75 } catch (final Exception e) {
77 "Error generating thumbnails for image file: "
79 + ", thumbnail generation skipped, enlisting as ordinary file instead.",
81 enlistFile(picture, parentDirectoryMetadata);
85 private String enlistImageDimensions(final Picture picture) {
86 final StringBuilder html = new StringBuilder();
88 final List<Dimension> recommendedDimensions = Picture
89 .getThumbnailDimensions(picture.getDimensions());
92 for (final Dimension dimension : recommendedDimensions) {
96 html.append("<a href=\""
97 + indexer.getThumbnailUrl(picture, dimension,
98 indexingContext) + "\">");
99 html.append(dimension.width + "x" + dimension.height);
103 return html.toString();
106 private void enlistImageInternal(final Picture picture) {
108 filesHtml.append("<div style=\"float:left; margin: 10px;\">");
110 // clickable image thumbnail
111 filesHtml.append("<a href=\""
112 + indexingContext.getGlobalUrl()
113 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
114 + picture.fileName) + "\">\n");
116 filesHtml.append("<img border=\"0\" src=\""
117 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
120 filesHtml.append("<b>"
121 + FilePathParser.getFileNameWithoutExtension(picture.fileName)
124 // links to thumbnails of various sizes
125 filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
127 filesHtml.append("</div>");
134 public void enlistOgv(final AbstractFile abstractFile,
135 final DirectoryMetadata parentDirectoryMetadata)
136 throws UnsupportedEncodingException {
138 final GeneralFile file = (GeneralFile) abstractFile;
140 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
144 .append("<video preload=\"metadata\" controls=\"controls\" tabindex=\"0\">\n");
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");
158 .getFileNameWithoutExtension(file.fileName)
159 + "<b/> ");
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>");
167 filesHtml.append("</div>\n");
169 filesHtml.append("\n");
177 public String getFileNameSuffix() {
184 * @throws IOException
187 public String getHtml(final boolean showTitle,
188 final boolean doHtmlHeaderAndFooter,
189 final DirectoryMetadata directory) {
191 // assemble final page HTML
192 final StringBuilder html = new StringBuilder();
194 if (doHtmlHeaderAndFooter) {
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");
207 html.append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
209 html.append(showCurrentPath());
210 html.append(listDirectories());
211 html.append(directory.getHeaderMevizMetadataHtml());
212 html.append(filesHtml.toString());
214 if (doHtmlHeaderAndFooter)
215 html.append("</font></BODY></HTML>\n");
217 return html.toString();
220 private String getImageThumbnailUrl(final Picture picture) {
221 final Dimension imageSize = picture.getDimensions();
223 Dimension listingDimension = new Dimension(1500, 450);
225 if (listingDimension.width > imageSize.width)
226 if (listingDimension.height > imageSize.height)
227 listingDimension = imageSize;
229 return indexer.getThumbnailUrl(picture, listingDimension,
234 public void init(final String galleryTitle, final List<String> path,
235 final AbstractIndexer indexer, final IndexingContext indexingContext) {
237 this.indexer = indexer;
238 this.galleryTitle = galleryTitle;
240 this.indexingContext = indexingContext;
242 dirHtml = new StringBuffer();
243 filesHtml = new StringBuffer();
246 private String listDirectories() {
247 final StringBuilder html = new StringBuilder();
248 html.append("<TABLE>\n");
250 if (path.size() > 0) {
252 html.append("<TD><b><a href=\""
253 + indexer.getParentDirectoryUrl(indexingContext)
254 + "\"><h2> <== parent directory</h2></a></b></TD>");
255 html.append("</TR>\n");
258 html.append(dirHtml.toString());
259 html.append("\n<TR><TD><br></TD></TR>\n");
260 html.append("</TABLE>\n");
262 return html.toString();
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");
272 return html.toString();
276 public void enlistWebm(AbstractFile abstractFile,
277 DirectoryMetadata directory) {
278 final GeneralFile file = (GeneralFile) abstractFile;
280 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
284 String fileUrl = indexingContext.getGlobalUrl()
285 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
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");
298 .getFileNameWithoutExtension(file.fileName)
299 + "<b/> ");
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>");
307 filesHtml.append("</div>\n");
309 filesHtml.append("\n");