2 * Meviz - Various tools collection to work with multimedia. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
7 package eu.svjatoslav.meviz.htmlindexer.layouts;
9 import eu.svjatoslav.commons.file.FilePathParser;
10 import eu.svjatoslav.meviz.htmlindexer.indexer.AbstractIndexer;
11 import eu.svjatoslav.meviz.htmlindexer.Constants;
12 import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
13 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
14 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
15 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
16 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
17 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Video;
18 import org.apache.log4j.Logger;
20 import java.io.IOException;
21 import java.util.List;
23 import static eu.svjatoslav.commons.file.FilePathParser.getFileNameWithoutExtension;
24 import static eu.svjatoslav.meviz.htmlindexer.Utils.urlEncode;
26 public class MixedLayout implements Layout {
28 public static final String encoding = "UTF-8";
30 private final Logger logger = Logger.getLogger(MixedLayout.class);
32 private StringBuffer dirHtml = new StringBuffer();
34 private StringBuffer filesHtml = new StringBuffer();
36 private String galleryTitle;
38 private List<String> path;
40 private AbstractIndexer indexer;
42 private IndexingContext indexingContext;
45 public void enlistDirectory(final AbstractFile directory,
46 final DirectoryMetadata parentDirectoryMetadata) {
47 dirHtml.append("<TR>");
48 dirHtml.append("<TD><b><a href=\""
49 + indexer.getDirectoryUrl(directory, indexingContext)
50 + "\"><h2> ==> " + directory.fileName + "</h2></a></b></TD>");
51 dirHtml.append("</TR>\n");
56 public void enlistFile(final AbstractFile file,
57 final DirectoryMetadata parentDirectoryMetadata) {
59 final String fullFileName = file.fileName;
61 filesHtml.append("<div style=\"clear: both;\"><a href=\""
62 + indexingContext.getGlobalUrl()
63 + urlEncode(indexingContext.getLocalUrl() + "/"
64 + fullFileName) + "\">" + fullFileName + "</a>\n");
65 filesHtml.append(" ("
66 + FilePathParser.getFileSizeDescription(file.getFileLength())
72 public void enlistImage(final Picture picture,
73 final DirectoryMetadata parentDirectoryMetadata) {
75 enlistImageInternal(picture);
76 } catch (final Exception e) {
78 "Error generating thumbnails for image file: "
80 + ", thumbnail generation skipped, enlisting as ordinary file instead.",
82 enlistFile(picture, parentDirectoryMetadata);
86 private String enlistImageDimensions(final Picture picture) {
87 final StringBuilder html = new StringBuilder();
89 final List<Dimension> recommendedDimensions = Picture
90 .getThumbnailDimensions(picture.getDimensions());
93 for (final Dimension dimension : recommendedDimensions) {
97 html.append("<a href=\""
98 + indexer.getThumbnailUrl(picture, dimension,
99 indexingContext) + "\">");
100 html.append(dimension.width + "x" + dimension.height);
104 return html.toString();
107 private void enlistImageInternal(final Picture picture) {
109 filesHtml.append("<div style=\"float:left; margin: 10px;\">");
111 // clickable image thumbnail
112 filesHtml.append("<a href=\""
113 + indexingContext.getGlobalUrl()
114 + urlEncode(indexingContext.getLocalUrl() + "/"
115 + picture.fileName) + "\">\n");
117 filesHtml.append("<img border=\"0\" src=\""
118 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
121 filesHtml.append("<b>"
122 + getFileNameWithoutExtension(picture.fileName)
125 // links to thumbnails of various sizes
126 filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
128 filesHtml.append("</div>");
135 public String getFileNameSuffix() {
142 * @throws IOException
145 public String getHtml(final boolean showTitle,
146 final boolean doHtmlHeaderAndFooter,
147 final DirectoryMetadata directory) {
149 // assemble final page HTML
150 final StringBuilder html = new StringBuilder();
152 if (doHtmlHeaderAndFooter)
153 appendHtmlHeader(html);
156 html.append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
158 html.append(showCurrentPath());
159 html.append(listDirectories());
160 html.append(directory.getHeaderMevizMetadataHtml());
161 html.append(filesHtml.toString());
163 if (doHtmlHeaderAndFooter)
164 appendHtmlFooter(html);
166 return html.toString();
169 private void appendHtmlFooter(StringBuilder html) {
170 html.append("</font></BODY></HTML>\n");
173 private void appendHtmlHeader(StringBuilder html) {
175 html.append("<HTML>\n");
176 html.append("<HEAD>\n");
177 html.append(" <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\"/>\n");
178 html.append("</HEAD>\n");
179 html.append("<BODY bgcolor=\"#000000\" alink=\"#50ffff\" link=\"#50ffff\" vlink=\"#ff50ff\" text=\"#ffffff\">\n");
180 html.append("<!-- " + Constants.HTML_MAGIC_STRING + " -->\n");
181 html.append("<!-- " + Constants.HTML_MEMO + " -->\n");
182 html.append("<font color=\"#ffffff\">\n");
185 private String getImageThumbnailUrl(final Picture picture) {
186 final Dimension imageSize = picture.getDimensions();
188 Dimension listingDimension = new Dimension(1500, 450);
190 if (listingDimension.width > imageSize.width)
191 if (listingDimension.height > imageSize.height)
192 listingDimension = imageSize;
194 return indexer.getThumbnailUrl(picture, listingDimension,
199 public void init(final String galleryTitle, final List<String> path,
200 final AbstractIndexer indexer, final IndexingContext indexingContext) {
202 this.indexer = indexer;
203 this.galleryTitle = galleryTitle;
205 this.indexingContext = indexingContext;
207 dirHtml = new StringBuffer();
208 filesHtml = new StringBuffer();
211 private String listDirectories() {
212 final StringBuilder html = new StringBuilder();
213 html.append("<TABLE>\n");
215 if (path.size() > 0) {
217 html.append("<TD><b><a href=\""
218 + indexer.getParentDirectoryUrl(indexingContext)
219 + "\"><h2> <== parent directory</h2></a></b></TD>");
220 html.append("</TR>\n");
223 html.append(dirHtml.toString());
224 html.append("\n<TR><TD><br></TD></TR>\n");
225 html.append("</TABLE>\n");
227 return html.toString();
230 private String showCurrentPath() {
231 final StringBuilder html = new StringBuilder();
232 html.append("<CENTER><H2>");
233 for (final String p : path)
234 html.append(" / " + p);
235 html.append("</H2></CENTER>\n");
237 return html.toString();
241 public void enlistVideo(Video video) {
243 filesHtml.append("<div style=\"margin: 30px; clear: both;\">\n");
246 filesHtml.append(" <video width=\"640\" controls>\n");
247 filesHtml.append(" <source src=\"" + getFileUrl(video) + "\" type=\"" + video.mimeType + "\"/>\n");
248 filesHtml.append(" </video>\n");
250 filesHtml.append(" <br/>\n");
253 filesHtml.append(" Video: <b>"
254 + getFileNameWithoutExtension(video.fileName)
255 + "<b/> ");
257 // video download link
258 filesHtml.append(" <a href=\"" + getFileUrl(video) + "\">download</a>\n");
260 filesHtml.append("</div>\n");
262 filesHtml.append("\n");
266 private String getFileUrl(AbstractFile file) {
267 return indexingContext.getGlobalUrl()
268 + urlEncode(indexingContext.getLocalUrl() + "/"