13aaff3a310c166a1650eca9975c6b8f27e1a0cd
[meviz.git] / MixedLayout.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012 -- 2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
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.
8 */
9
10 package eu.svjatoslav.meviz.htmlindexer.layouts;
11
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;
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import static eu.svjatoslav.commons.file.FilePathParser.getFileNameWithoutExtension;
28 import static eu.svjatoslav.meviz.htmlindexer.Utils.urlEncode;
29
30 public class MixedLayout implements Layout {
31
32     public static final String encoding = "UTF-8";
33
34     private final Logger logger = Logger.getLogger(MixedLayout.class);
35
36     private StringBuffer dirHtml = new StringBuffer();
37
38     private StringBuffer filesHtml = new StringBuffer();
39
40     private String galleryTitle;
41
42     private List<String> path;
43
44     private AbstractIndexer indexer;
45
46     private IndexingContext indexingContext;
47
48     @Override
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> ==&gt; " + directory.fileName + "</h2></a></b></TD>");
55         dirHtml.append("</TR>\n");
56
57     }
58
59     @Override
60     public void enlistFile(final AbstractFile file,
61                            final DirectoryMetadata parentDirectoryMetadata) {
62
63         final String fullFileName = file.fileName;
64
65         filesHtml.append("<div style=\"clear: both;\"><a href=\""
66                 + indexingContext.getGlobalUrl()
67                 + urlEncode(indexingContext.getLocalUrl() + "/"
68                 + fullFileName) + "\">" + fullFileName + "</a>\n");
69         filesHtml.append("&nbsp;&nbsp;("
70                 + FilePathParser.getFileSizeDescription(file.getFileLength())
71                 + ")</div><br/>\n");
72
73     }
74
75     @Override
76     public void enlistImage(final Picture picture,
77                             final DirectoryMetadata parentDirectoryMetadata) {
78         try {
79             enlistImageInternal(picture);
80         } catch (final Exception e) {
81             logger.error(
82                     "Error generating thumbnails for image file: "
83                             + e.toString()
84                             + ", thumbnail generation skipped, enlisting as ordinary file instead.",
85                     e);
86             enlistFile(picture, parentDirectoryMetadata);
87         }
88     }
89
90     private String enlistImageDimensions(final Picture picture) {
91         final StringBuilder html = new StringBuilder();
92
93         final List<Dimension> recommendedDimensions = Picture
94                 .getThumbnailDimensions(picture.getDimensions());
95
96         int count = 0;
97         for (final Dimension dimension : recommendedDimensions) {
98             count++;
99             if (count > 1)
100                 html.append(", ");
101             html.append("<a href=\""
102                     + indexer.getThumbnailUrl(picture, dimension,
103                     indexingContext) + "\">");
104             html.append(dimension.width + "x" + dimension.height);
105             html.append("</a>");
106         }
107
108         return html.toString();
109     }
110
111     private void enlistImageInternal(final Picture picture) {
112
113         filesHtml.append("<div style=\"float:left; margin: 10px;\">");
114
115         // clickable image thumbnail
116         filesHtml.append("<a href=\""
117                 + indexingContext.getGlobalUrl()
118                 + urlEncode(indexingContext.getLocalUrl() + "/"
119                 + picture.fileName) + "\">\n");
120
121         filesHtml.append("<img border=\"0\" src=\""
122                 + getImageThumbnailUrl(picture) + "\"/></a><br/>\n");
123
124         // file name
125         filesHtml.append("<b>"
126                 + getFileNameWithoutExtension(picture.fileName)
127                 + "</b><br/>");
128
129         // links to thumbnails of various sizes
130         filesHtml.append("(" + enlistImageDimensions(picture) + ")\n");
131
132         filesHtml.append("</div>");
133     }
134
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public String getFileNameSuffix() {
140         return "";
141     }
142
143     /**
144      * {@inheritDoc}
145      *
146      * @throws IOException
147      */
148     @Override
149     public String getHtml(final boolean showTitle,
150                           final boolean doHtmlHeaderAndFooter,
151                           final DirectoryMetadata directory) {
152
153         // assemble final page HTML
154         final StringBuilder html = new StringBuilder();
155
156         if (doHtmlHeaderAndFooter)
157             appendHtmlHeader(html);
158
159         if (showTitle)
160             html.append("<CENTER><H1>" + galleryTitle + "</H1></CENTER>\n");
161
162         html.append(showCurrentPath());
163         html.append(listDirectories());
164         html.append(directory.getHeaderMevizMetadataHtml());
165         html.append(filesHtml.toString());
166
167         if (doHtmlHeaderAndFooter)
168             appendHtmlFooter(html);
169
170         return html.toString();
171     }
172
173     private void appendHtmlFooter(StringBuilder html) {
174         html.append("</font></BODY></HTML>\n");
175     }
176
177     private void appendHtmlHeader(StringBuilder html) {
178         // page header
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");
187     }
188
189     private String getImageThumbnailUrl(final Picture picture) {
190         final Dimension imageSize = picture.getDimensions();
191
192         Dimension listingDimension = new Dimension(1500, 450);
193
194         if (listingDimension.width > imageSize.width)
195             if (listingDimension.height > imageSize.height)
196                 listingDimension = imageSize;
197
198         return indexer.getThumbnailUrl(picture, listingDimension,
199                 indexingContext);
200     }
201
202     @Override
203     public void init(final String galleryTitle, final List<String> path,
204                      final AbstractIndexer indexer, final IndexingContext indexingContext) {
205
206         this.indexer = indexer;
207         this.galleryTitle = galleryTitle;
208         this.path = path;
209         this.indexingContext = indexingContext;
210
211         dirHtml = new StringBuffer();
212         filesHtml = new StringBuffer();
213     }
214
215     private String listDirectories() {
216         final StringBuilder html = new StringBuilder();
217         html.append("<TABLE>\n");
218
219         if (path.size() > 0) {
220             html.append("<TR>");
221             html.append("<TD><b><a href=\""
222                     + indexer.getParentDirectoryUrl(indexingContext)
223                     + "\"><h2> &lt;== parent directory</h2></a></b></TD>");
224             html.append("</TR>\n");
225         }
226
227         html.append(dirHtml.toString());
228         html.append("\n<TR><TD><br></TD></TR>\n");
229         html.append("</TABLE>\n");
230
231         return html.toString();
232     }
233
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");
240
241         return html.toString();
242     }
243
244     @Override
245     public void enlistVideo(Video video) {
246
247         filesHtml.append("<div style=\"margin: 30px; clear: both;\">\n");
248
249         // watchable video
250         filesHtml.append("  <video width=\"640\" controls>\n");
251         filesHtml.append("    <source src=\"" + getFileUrl(video) + "\" type=\"" + video.mimeType + "\"/>\n");
252         filesHtml.append("  </video>\n");
253
254         filesHtml.append("  <br/>\n");
255
256         // video title
257         filesHtml.append("  Video: <b>"
258                         + getFileNameWithoutExtension(video.fileName)
259                         + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
260
261         // video download link
262         filesHtml.append("  <a href=\"" + getFileUrl(video) + "\">download</a>\n");
263
264         filesHtml.append("</div>\n");
265
266         filesHtml.append("\n");
267     }
268
269
270     private String getFileUrl(AbstractFile file) {
271         return indexingContext.getGlobalUrl()
272                     + urlEncode(indexingContext.getLocalUrl() + "/"
273                     + file.fileName);
274     }
275 }