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.IOException;
13 import java.io.UnsupportedEncodingException;
14 import java.util.List;
16 import org.apache.log4j.Logger;
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;
30 public class MixedLayout implements Layout {
32 Logger logger = Logger.getLogger(MixedLayout.class);
34 StringBuffer dirHtml = new StringBuffer();
36 StringBuffer filesHtml = new StringBuffer();
42 public static final String encoding = "UTF-8";
44 private AbstractIndexer indexer;
46 private IndexingContext indexingContext;
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> ==> " + directory.fileName + "</h2></a></b></TD>");
55 dirHtml.append("</TR>\n");
60 public void enlistFile(final AbstractFile file,
61 final DirectoryMetadata parentDirectoryMetadata) {
63 final String fullFileName = file.fileName;
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(" ("
70 + FilePathParser.getFileSizeDescription(file.getFileLength())
76 public void enlistImage(final Picture picture,
77 final DirectoryMetadata parentDirectoryMetadata) {
79 enlistImageInternal(picture);
80 } catch (final Exception e) {
82 "Error generating thumbnails for image file: "
84 + ", thumbnail generation skipped, enlisting as ordinary file instead.",
86 enlistFile(picture, parentDirectoryMetadata);
90 private String enlistImageDimensions(final Picture picture) {
91 final StringBuilder html = new StringBuilder();
93 final List<Dimension> recommendedDimensions = Picture
94 .getThumbnailDimensions(picture.getDimensions());
97 for (final Dimension dimension : recommendedDimensions) {
101 html.append("<a href=\""
102 + indexer.getThumbnailUrl(picture, dimension,
103 indexingContext) + "\">");
104 html.append(dimension.width + "x" + dimension.height);
108 return html.toString();
111 private void enlistImageInternal(final Picture picture) {
113 filesHtml.append("<div style=\"float:left; margin: 10px;\">");
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");
124 filesHtml.append("<b>"
125 + FilePathParser.getFileNameWithoutExtension(picture.fileName)
128 // links to thumbnails of various sizes
129 filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
131 filesHtml.append("</div>");
136 public void enlistOgv(final AbstractFile abstractFile,
137 final DirectoryMetadata parentDirectoryMetadata)
138 throws UnsupportedEncodingException {
140 final GeneralFile file = (GeneralFile) abstractFile;
142 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
146 .append("<video preload=\"metadata\" controls=\"controls\" tabindex=\"0\">\n");
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");
160 .getFileNameWithoutExtension(file.fileName)
161 + "<b/> ");
163 // video download link
164 filesHtml.append("<a href=\""
165 + indexingContext.getGlobalUrl()
166 + UrlParamEncoder.encode(indexingContext.getLocalUrl() + "/"
167 + file.fileName) + "\">download as OGV</a>");
169 filesHtml.append("</div>\n");
171 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 StringBuffer html = new StringBuffer();
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();