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)
193 appendHtmlHeader(html);
196 html.append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
198 html.append(showCurrentPath());
199 html.append(listDirectories());
200 html.append(directory.getHeaderMevizMetadataHtml());
201 html.append(filesHtml.toString());
203 if (doHtmlHeaderAndFooter)
204 appendHtmlFooter(html);
206 return html.toString();
209 private void appendHtmlFooter(StringBuilder html) {
210 html.append("</font></BODY></HTML>\n");
213 private void appendHtmlHeader(StringBuilder html) {
215 html.append("<HTML>\n");
216 html.append("<HEAD>\n");
217 html.append(" <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
218 html.append("</HEAD>\n");
219 html.append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
220 html.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
221 html.append("<!-- " + Constants.HTML_MEMO + " -->\n");
222 html.append("<font color=\"#ffffff\">\n");
225 private String getImageThumbnailUrl(final Picture picture) {
226 final Dimension imageSize = picture.getDimensions();
228 Dimension listingDimension = new Dimension(1500, 450);
230 if (listingDimension.width > imageSize.width)
231 if (listingDimension.height > imageSize.height)
232 listingDimension = imageSize;
234 return indexer.getThumbnailUrl(picture, listingDimension,
239 public void init(final String galleryTitle, final List<String> path,
240 final AbstractIndexer indexer, final IndexingContext indexingContext) {
242 this.indexer = indexer;
243 this.galleryTitle = galleryTitle;
245 this.indexingContext = indexingContext;
247 dirHtml = new StringBuffer();
248 filesHtml = new StringBuffer();
251 private String listDirectories() {
252 final StringBuilder html = new StringBuilder();
253 html.append("<TABLE>\n");
255 if (path.size() > 0) {
257 html.append("<TD><b><a href=\""
258 + indexer.getParentDirectoryUrl(indexingContext)
259 + "\"><h2> <== parent directory</h2></a></b></TD>");
260 html.append("</TR>\n");
263 html.append(dirHtml.toString());
264 html.append("\n<TR><TD><br></TD></TR>\n");
265 html.append("</TABLE>\n");
267 return html.toString();
270 private String showCurrentPath() {
271 final StringBuilder html = new StringBuilder();
272 html.append("<CENTER><H2>");
273 for (final String p : path)
274 html.append(" / " + p);
275 html.append("</H2></CENTER>\n");
277 return html.toString();
281 public void enlistWebm(AbstractFile abstractFile,
282 DirectoryMetadata directory) {
283 final GeneralFile file = (GeneralFile) abstractFile;
285 filesHtml.append("<div style=\"margin: 30px; clear: both;\">");
289 String fileUrl = indexingContext.getGlobalUrl()
290 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
293 filesHtml.append("<video controls>\n");
294 filesHtml.append(" <source src=\"" + fileUrl
295 + "\" type='video/webm;codecs=\"vp8, opus\"'/>\n");
296 filesHtml.append("</video>\n");
297 filesHtml.append("<br/>\n");
303 .getFileNameWithoutExtension(file.fileName)
304 + "<b/> ");
306 // video download link
307 filesHtml.append("<a href=\""
308 + indexingContext.getGlobalUrl()
309 + Utils.urlEncode(indexingContext.getLocalUrl() + "/"
310 + file.fileName) + "\">download as WebM file</a>");
312 filesHtml.append("</div>\n");
314 filesHtml.append("\n");