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 java.io.UnsupportedEncodingException;
13 import java.util.List;
15 import org.apache.log4j.Logger;
17 import eu.svjatoslav.commons.file.FilePathParser;
18 import eu.svjatoslav.commons.network.UrlParamEncoder;
19 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
20 import eu.svjatoslav.meviz.htmlindexer.Constants;
21 import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
22 import eu.svjatoslav.meviz.htmlindexer.Layout;
23 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
24 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
25 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
26 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
27 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
29 public class MixedLayout implements Layout {
31 Logger logger = Logger.getLogger(MixedLayout.class);
33 StringBuffer dirHtml = new StringBuffer();
35 StringBuffer filesHtml = new StringBuffer();
41 public static final String encoding = "UTF-8";
43 private AbstractIndexer indexer;
45 private IndexingContext indexingContext;
48 public void enlistDirectory(final AbstractFile directory,
49 final DirectoryMetadata parentDirectoryMetadata) {
50 dirHtml.append("<TR>");
51 dirHtml.append("<TD><b><a href=\""
52 + indexer.getDirectoryUrl(directory, indexingContext)
53 + "\"><h2> ==> " + directory.fileName + "</h2></a></b></TD>");
54 dirHtml.append("</TR>\n");
59 public void enlistFile(final AbstractFile file,
60 final DirectoryMetadata parentDirectoryMetadata) {
62 final String fullFileName = file.fileName;
64 filesHtml.append("<div style=\"clear: both;\"><a href=\""
65 + indexingContext.getGlobalUrl()
66 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
67 + fullFileName) + "\">" + fullFileName + "</a>\n");
68 filesHtml.append(" ("
69 + FilePathParser.getFileSizeDescription(file.getFileLength())
75 public void enlistImage(final Picture picture,
76 final DirectoryMetadata parentDirectoryMetadata) {
78 enlistImageInternal(picture);
79 } catch (final Exception e) {
81 "Error generating thumbnails for image file: "
83 + ", thumbnail generation skipped, enlisting as ordinary file instead.",
85 enlistFile(picture, parentDirectoryMetadata);
89 private String enlistImageDimensions(final Picture picture) {
90 final StringBuilder html = new StringBuilder();
92 final List<Dimension> recommendedDimensions = Picture
93 .getThumbnailDimensions(picture.getDimensions());
96 for (final Dimension dimension : recommendedDimensions) {
100 html.append("<a href=\""
101 + indexer.getThumbnailUrl(picture, dimension,
102 indexingContext) + "\">");
103 html.append(dimension.width + "x" + dimension.height);
107 return html.toString();
110 private void enlistImageInternal(final Picture picture) {
112 filesHtml.append("<div style=\"float:left; margin: 10px;\">");
114 // clickable image thumbnail
115 filesHtml.append("<a href=\""
116 + indexingContext.getGlobalUrl()
117 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
118 + picture.fileName) + "\">\n");
119 filesHtml.append("<img border=\"0\" src=\""
120 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
123 filesHtml.append("<b>"
124 + FilePathParser.getFileNameWithoutExtension(picture.fileName)
127 // links to thumbnails of various sizes
128 filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
130 filesHtml.append("</div>");
135 public void enlistOgv(final AbstractFile abstractFile,
136 final DirectoryMetadata parentDirectoryMetadata)
137 throws UnsupportedEncodingException {
139 final GeneralFile file = (GeneralFile) abstractFile;
141 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
145 .append("<video preload=\"metadata\" controls=\"controls\" tabindex=\"0\">\n");
147 .append("<source type=\"video/ogg\" codecs=\"theora, vorbis\" src=\""
148 + indexingContext.getGlobalUrl()
149 + UrlParamEncoder.encode(indexingContext.getLocalUrl()
150 + "/" + file.fileName) + "\">\n");
151 filesHtml.append("</source>\n");
152 filesHtml.append("</video>\n");
153 filesHtml.append("<br/>\n");
159 .getFileNameWithoutExtension(file.fileName)
160 + "<b/> ");
162 // video download link
163 filesHtml.append("<a href=\""
164 + indexingContext.getGlobalUrl()
165 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
166 + file.fileName) + "\">download as OGV</a>");
168 filesHtml.append("</div>\n");
170 filesHtml.append("\n");
176 public String getFileNameSuffix() {
182 public String getHtml(final boolean showTitle,
183 final boolean doHtmlHeaderAndFooter) {
185 // assemble final page HTML
186 final StringBuffer finalHtml = new StringBuffer();
188 if (doHtmlHeaderAndFooter) {
190 finalHtml.append("<HTML>\n");
191 finalHtml.append("<HEAD>\n");
193 .append(" <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
194 finalHtml.append("</HEAD>\n");
196 .append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
197 finalHtml.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
198 finalHtml.append("<!-- " + Constants.HTML_MEMO + " -->\n");
199 finalHtml.append("<font color=\"#ffffff\">\n");
204 .append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
206 showCurrentPath(finalHtml);
208 listDirectories(finalHtml);
210 finalHtml.append(filesHtml.toString());
212 if (doHtmlHeaderAndFooter)
213 finalHtml.append("</font></BODY></HTML>\n");
215 return finalHtml.toString();
218 private String getImageThumbnailUrl(final Picture picture) {
219 final Dimension imageSize = picture.getDimensions();
221 Dimension listingDimension = new Dimension(1500, 450);
223 if (listingDimension.width > imageSize.width)
224 if (listingDimension.height > imageSize.height)
225 listingDimension = imageSize;
227 return indexer.getThumbnailUrl(picture, listingDimension,
232 public void init(final String galleryTitle, final List<String> path,
233 final AbstractIndexer indexer, final IndexingContext indexingContext) {
235 this.indexer = indexer;
236 this.galleryTitle = galleryTitle;
238 this.indexingContext = indexingContext;
240 dirHtml = new StringBuffer();
241 filesHtml = new StringBuffer();
244 private void listDirectories(final StringBuffer finalHtml) {
245 finalHtml.append("<TABLE>\n");
247 if (path.size() > 0) {
248 finalHtml.append("<TR>");
249 finalHtml.append("<TD><b><a href=\""
250 + indexer.getParentDirectoryUrl(indexingContext)
251 + "\"><h2> <== parent directory</h2></a></b></TD>");
252 finalHtml.append("</TR>\n");
255 finalHtml.append(dirHtml.toString());
256 finalHtml.append("\n<TR><TD><br></TD></TR>\n");
257 finalHtml.append("</TABLE>\n");
260 private void showCurrentPath(final StringBuffer finalHtml) {
261 finalHtml.append("<CENTER><H2>");
262 for (final String p : path)
263 finalHtml.append(" / " + p);
264 finalHtml.append("</H2></CENTER>\n");