2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2019, 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 3 of the GNU Lesser General Public License
7 * or later 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.AbstractIndexer;
14 import eu.svjatoslav.meviz.htmlindexer.Constants;
15 import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
16 import eu.svjatoslav.meviz.htmlindexer.Layout;
17 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
18 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
19 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
20 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
21 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Video;
22 import org.apache.log4j.Logger;
24 import java.io.IOException;
25 import java.util.List;
27 import static eu.svjatoslav.commons.file.FilePathParser.getFileNameWithoutExtension;
28 import static eu.svjatoslav.meviz.htmlindexer.Utils.urlEncode;
30 public class MixedLayout implements Layout {
32 public static final String encoding = "UTF-8";
34 private final Logger logger = Logger.getLogger(MixedLayout.class);
36 private StringBuffer dirHtml = new StringBuffer();
38 private StringBuffer filesHtml = new StringBuffer();
40 private String galleryTitle;
42 private List<String> path;
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 + urlEncode(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 + urlEncode(indexingContext.getLocalUrl() + "/"
119 + picture.fileName) + "\">\n");
121 filesHtml.append("<img border=\"0\" src=\""
122 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
125 filesHtml.append("<b>"
126 + getFileNameWithoutExtension(picture.fileName)
129 // links to thumbnails of various sizes
130 filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
132 filesHtml.append("</div>");
139 public String getFileNameSuffix() {
146 * @throws IOException
149 public String getHtml(final boolean showTitle,
150 final boolean doHtmlHeaderAndFooter,
151 final DirectoryMetadata directory) {
153 // assemble final page HTML
154 final StringBuilder html = new StringBuilder();
156 if (doHtmlHeaderAndFooter)
157 appendHtmlHeader(html);
160 html.append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
162 html.append(showCurrentPath());
163 html.append(listDirectories());
164 html.append(directory.getHeaderMevizMetadataHtml());
165 html.append(filesHtml.toString());
167 if (doHtmlHeaderAndFooter)
168 appendHtmlFooter(html);
170 return html.toString();
173 private void appendHtmlFooter(StringBuilder html) {
174 html.append("</font></BODY></HTML>\n");
177 private void appendHtmlHeader(StringBuilder html) {
179 html.append("<HTML>\n");
180 html.append("<HEAD>\n");
181 html.append(" <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
182 html.append("</HEAD>\n");
183 html.append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
184 html.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
185 html.append("<!-- " + Constants.HTML_MEMO + " -->\n");
186 html.append("<font color=\"#ffffff\">\n");
189 private String getImageThumbnailUrl(final Picture picture) {
190 final Dimension imageSize = picture.getDimensions();
192 Dimension listingDimension = new Dimension(1500, 450);
194 if (listingDimension.width > imageSize.width)
195 if (listingDimension.height > imageSize.height)
196 listingDimension = imageSize;
198 return indexer.getThumbnailUrl(picture, listingDimension,
203 public void init(final String galleryTitle, final List<String> path,
204 final AbstractIndexer indexer, final IndexingContext indexingContext) {
206 this.indexer = indexer;
207 this.galleryTitle = galleryTitle;
209 this.indexingContext = indexingContext;
211 dirHtml = new StringBuffer();
212 filesHtml = new StringBuffer();
215 private String listDirectories() {
216 final StringBuilder html = new StringBuilder();
217 html.append("<TABLE>\n");
219 if (path.size() > 0) {
221 html.append("<TD><b><a href=\""
222 + indexer.getParentDirectoryUrl(indexingContext)
223 + "\"><h2> <== parent directory</h2></a></b></TD>");
224 html.append("</TR>\n");
227 html.append(dirHtml.toString());
228 html.append("\n<TR><TD><br></TD></TR>\n");
229 html.append("</TABLE>\n");
231 return html.toString();
234 private String showCurrentPath() {
235 final StringBuilder html = new StringBuilder();
236 html.append("<CENTER><H2>");
237 for (final String p : path)
238 html.append(" / " + p);
239 html.append("</H2></CENTER>\n");
241 return html.toString();
245 public void enlistVideo(Video video) {
247 filesHtml.append("<div style=\"margin: 30px; clear: both;\">\n");
250 filesHtml.append(" <video width=\"640\" controls>\n");
251 filesHtml.append(" <source src=\"" + getFileUrl(video) + "\" type=\"" + video.mimeType + "\"/>\n");
252 filesHtml.append(" </video>\n");
254 filesHtml.append(" <br/>\n");
257 filesHtml.append(" Video: <b>"
258 + getFileNameWithoutExtension(video.fileName)
259 + "<b/> ");
261 // video download link
262 filesHtml.append(" <a href=\"" + getFileUrl(video) + "\">download</a>\n");
264 filesHtml.append("</div>\n");
266 filesHtml.append("\n");
270 private String getFileUrl(AbstractFile file) {
271 return indexingContext.getGlobalUrl()
272 + urlEncode(indexingContext.getLocalUrl() + "/"