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