20ec8d191a8636447bed7122d74c56c0c24ee9b4
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / WebIndexer.java
1 package eu.svjatoslav.meviz.htmlindexer;
2
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.ObjectInputStream;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8
9 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
10 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
11 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
12 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
13 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
14
15 public class WebIndexer extends AbstractIndexer {
16
17         private final MixedLayout layout = new MixedLayout();
18         private final String localPrefix;
19         private final String globalPrefix;
20
21         public WebIndexer(final String globalPrefix, final String localPrefix) {
22                 this.globalPrefix = globalPrefix;
23                 this.localPrefix = localPrefix;
24         }
25
26         @Override
27         public String getDirectoryUrl(final AbstractFile directory) {
28                 return "photos.jsp?path=/" + directory.fileName;
29         }
30
31         public String getHtml(final String path) throws MalformedURLException,
32                         IOException, ClassNotFoundException {
33
34                 layout.init("Photos", new String[] {}, this, localPrefix);
35
36                 final DirectoryMetadata metadata = getMetadataForPath();
37
38                 compileHtml(layout, metadata);
39
40                 return layout.getHtml();
41         }
42
43         public DirectoryMetadata getMetadataForPath() throws IOException,
44                         MalformedURLException, ClassNotFoundException {
45
46                 final BufferedInputStream in = new BufferedInputStream(
47                                 new URL(globalPrefix + localPrefix
48                                                 + ".thumbnails/metadata_6.dat").openStream());
49
50                 final ObjectInputStream oin = new ObjectInputStream(in);
51                 final DirectoryMetadata directory = (DirectoryMetadata) oin
52                                 .readObject();
53                 in.close();
54
55                 return directory;
56         }
57
58         @Override
59         public String getThumbnailPath(final Picture picture,
60                         final Dimension desiredDimension) {
61
62                 // in case thumbnail size was equal to original, then return original
63                 // file path
64                 if (picture.getDimensions().equals(desiredDimension))
65                         return localPrefix + picture.fileName;
66
67                 final String thumbnailFileName = picture
68                                 .getRelativeThumbnailFileName(desiredDimension);
69
70                 return localPrefix + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
71                                 + thumbnailFileName;
72         }
73
74 }