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.indexer;
9 import eu.svjatoslav.meviz.htmlindexer.CommandlineHandler;
10 import eu.svjatoslav.meviz.htmlindexer.Constants;
11 import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
12 import eu.svjatoslav.meviz.htmlindexer.Utils;
13 import eu.svjatoslav.meviz.htmlindexer.layouts.Layout;
14 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
15 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
16 import eu.svjatoslav.meviz.htmlindexer.metadata.MetadadaHelper;
17 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
18 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
19 import org.apache.log4j.Logger;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
27 import static eu.svjatoslav.commons.file.IOHelper.overwriteFileIfContentDiffers;
28 import static eu.svjatoslav.meviz.htmlindexer.Constants.THUMBNAILS_DIRECTORY_NAME;
29 import static eu.svjatoslav.meviz.htmlindexer.Utils.getThumbnailsDirectoryPath;
30 import static eu.svjatoslav.meviz.htmlindexer.Utils.urlEncode;
31 import static eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture.makeThumbnail;
34 * Main class of HTML indexer.
37 public class FilesystemIndexer extends AbstractIndexer {
39 private static final Logger logger = Logger
40 .getLogger(FilesystemIndexer.class);
42 private final HashSet<String> validMetadataFiles = new HashSet<>();
44 private final File directoryToIndex;
45 private final List<String> pathElements;
46 private final CommandlineHandler commandlineHandler;
47 private File metadataFile;
48 private DirectoryMetadata metadata;
49 private HashSet<Layout> layouts;
51 public FilesystemIndexer(final File directoryToIndex,
52 final CommandlineHandler commandlineHandler,
53 final List<String> pathElements) throws Exception {
55 this.directoryToIndex = directoryToIndex;
56 this.commandlineHandler = commandlineHandler;
57 this.pathElements = pathElements;
59 if (!directoryToIndex.canRead())
62 validMetadataFiles.add(Constants.METADATA_FILE_NAME);
64 initializeThumbnailsDirectory();
65 loadOrCreateMetadata();
71 metadata.removeUnusedMetadataEntries();
73 generateHtmlFromMetadata(layouts);
75 removeUnusedThumbnailFiles();
77 // save directory metadata
79 MetadadaHelper.saveDirectoryMetadata(metadataFile, metadata);
83 private boolean canWriteIndexFile(final File indexFile)
86 return !indexFile.exists() || Utils.isMevizGeneratedIndexFile(indexFile);
90 private void generateHtmlFromMetadata(final HashSet<Layout> layouts) {
91 for (final Layout layout : layouts)
93 final File indexFile = Utils.getLayoutIndexFile(layout,
96 if (canWriteIndexFile(indexFile)) {
98 compileHtml(layout, metadata);
100 overwriteFileIfContentDiffers(indexFile, layout
101 .getHtml(true, true, metadata).getBytes());
103 } catch (final Exception e) {
104 logger.error("Error writing index file. ", e);
108 private List<String> getChildPath(final File file) {
110 final List<String> result = new ArrayList<>();
111 result.addAll(pathElements);
112 result.add(file.getName());
118 public String getDirectoryUrl(final AbstractFile directory,
119 final IndexingContext context) {
120 return urlEncode(directory.fileName) + "/index.html";
124 public String getParentDirectoryUrl(final IndexingContext context) {
125 return "../index.html";
129 public String getThumbnailUrl(final Picture picture,
130 final Dimension desiredDimension, final IndexingContext context) {
132 // in case thumbnail size was equal to original, then return original
134 if (picture.getDimensions().equals(desiredDimension))
135 return picture.fileName;
137 final String thumbnailFileName = picture
138 .getRelativeThumbnailFileName(desiredDimension);
140 validMetadataFiles.add(thumbnailFileName);
141 final File thumbnailFile = new File(
142 getThumbnailsDirectoryPath(directoryToIndex) + thumbnailFileName);
144 if (!thumbnailFile.exists()) {
146 final File originalFile = new File(
147 directoryToIndex.getAbsolutePath() + "/" + picture.fileName);
149 makeThumbnail(originalFile, thumbnailFile, desiredDimension.getAwtDimension());
151 return THUMBNAILS_DIRECTORY_NAME + "/" + urlEncode(thumbnailFileName);
154 private void initializeLayouts() {
156 layouts = Utils.getLayouts();
158 final String galleryTitle = commandlineHandler.getGalleryTitle();
160 for (final Layout layout : layouts)
161 layout.init(galleryTitle, pathElements, this, new IndexingContext(
165 private void initializeThumbnailsDirectory() {
166 final File thumbnailsDirectory = Utils
167 .getThumbnailsDirectory(directoryToIndex);
169 if (!thumbnailsDirectory.exists())
170 thumbnailsDirectory.mkdirs();
173 private void loadOrCreateMetadata() {
175 metadataFile = new File(
176 getThumbnailsDirectoryPath(directoryToIndex)
177 + Constants.METADATA_FILE_NAME);
179 metadata = MetadadaHelper.initDirectoryMetadata(metadataFile);
182 private void removeUnusedThumbnailFiles() {
184 for (final File file : Utils.getThumbnailsDirectory(directoryToIndex)
186 if (!validMetadataFiles.contains(file.getName()))
191 private void updateMetadata() throws Exception {
193 for (final File file : directoryToIndex.listFiles())
194 if (shallFileBeIndexed(file)) {
196 metadata.ensureFileMetadataIsUpToDate(directoryToIndex, file);
198 if (file.isDirectory())
199 new FilesystemIndexer(file, commandlineHandler,