2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2017, 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.util.List;
24 public class MixedLayout implements Layout {
26 public static final String encoding = "UTF-8";
28 private final Logger logger = Logger.getLogger(MixedLayout.class);
30 private StringBuffer dirHtml = new StringBuffer();
32 private StringBuffer filesHtml = new StringBuffer();
34 private String galleryTitle;
36 private List<String> path;
38 private AbstractIndexer indexer;
40 private IndexingContext indexingContext;
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> ==> " + directory.fileName + "</h2></a></b></TD>");
49 dirHtml.append("</TR>\n");
54 public void enlistFile(final AbstractFile file,
55 final DirectoryMetadata parentDirectoryMetadata) {
57 final String fullFileName = file.fileName;
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(" ("
64 + FilePathParser.getFileSizeDescription(file.getFileLength())
70 public void enlistImage(final Picture picture,
71 final DirectoryMetadata parentDirectoryMetadata) {
73 enlistImageInternal(picture);
74 } catch (final Exception e) {
76 "Error generating thumbnails for image file: "
78 + ", thumbnail generation skipped, enlisting as ordinary file instead.",
80 enlistFile(picture, parentDirectoryMetadata);
84 private String enlistImageDimensions(final Picture picture) {
85 final StringBuilder html = new StringBuilder();
87 final List<Dimension> recommendedDimensions = Picture
88 .getThumbnailDimensions(picture.getDimensions());
91 for (final Dimension dimension : recommendedDimensions) {
95 html.append("<a href=\""
96 + indexer.getThumbnailUrl(picture, dimension,
97 indexingContext) + "\">");
98 html.append(dimension.width + "x" + dimension.height);
102 return html.toString();
105 private void enlistImageInternal(final Picture picture) {
107 filesHtml.append("<div style=\"float:left; margin: 10px;\">");
109 // clickable image thumbnail
110 filesHtml.append("<a href=\""
111 + indexingContext.getGlobalUrl()
112 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
113 + picture.fileName) + "\">\n");
115 filesHtml.append("<img border=\"0\" src=\""
116 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
119 filesHtml.append("<b>"
120 + FilePathParser.getFileNameWithoutExtension(picture.fileName)
123 // links to thumbnails of various sizes
124 filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
126 filesHtml.append("</div>");
133 public void enlistOgv(final AbstractFile abstractFile,
134 final DirectoryMetadata parentDirectoryMetadata) {
136 final GeneralFile file = (GeneralFile) abstractFile;
138 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
142 .append("<video preload=\"metadata\" controls=\"controls\" tabindex=\"0\">\n");
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");
156 .getFileNameWithoutExtension(file.fileName)
157 + "<b/> ");
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>");
165 filesHtml.append("</div>\n");
167 filesHtml.append("\n");
175 public String getFileNameSuffix() {
182 * @throws IOException
185 public String getHtml(final boolean showTitle,
186 final boolean doHtmlHeaderAndFooter,
187 final DirectoryMetadata directory) {
189 // assemble final page HTML
190 final StringBuilder html = new StringBuilder();
192 if (doHtmlHeaderAndFooter) {
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");
205 html.append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
207 html.append(showCurrentPath());
208 html.append(listDirectories());
209 html.append(directory.getHeaderMevizMetadataHtml());
210 html.append(filesHtml.toString());
212 if (doHtmlHeaderAndFooter)
213 html.append("</font></BODY></HTML>\n");
215 return html.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 String listDirectories() {
245 final StringBuilder html = new StringBuilder();
246 html.append("<TABLE>\n");
248 if (path.size() > 0) {
250 html.append("<TD><b><a href=\""
251 + indexer.getParentDirectoryUrl(indexingContext)
252 + "\"><h2> <== parent directory</h2></a></b></TD>");
253 html.append("</TR>\n");
256 html.append(dirHtml.toString());
257 html.append("\n<TR><TD><br></TD></TR>\n");
258 html.append("</TABLE>\n");
260 return html.toString();
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");
270 return html.toString();
274 public void enlistWebm(AbstractFile abstractFile,
275 DirectoryMetadata directory) {
276 final GeneralFile file = (GeneralFile) abstractFile;
278 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
282 String fileUrl = indexingContext.getGlobalUrl()
283 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
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");
296 .getFileNameWithoutExtension(file.fileName)
297 + "<b/> ");
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>");
305 filesHtml.append("</div>\n");
307 filesHtml.append("\n");