2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
10 package eu.svjatoslav.meviz.htmlindexer;
12 import eu.svjatoslav.commons.file.IOHelper;
13 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
14 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
15 import eu.svjatoslav.meviz.htmlindexer.metadata.MetadadaHelper;
16 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
17 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
18 import org.apache.log4j.Logger;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.List;
27 * Main class of HTML indexer.
30 public class FilesystemIndexer extends AbstractIndexer {
32 private static final Logger logger = Logger
33 .getLogger(FilesystemIndexer.class);
35 private final HashSet<String> validMetadataFiles = new HashSet<>();
37 private final File directoryToIndex;
38 private final List<String> pathElements;
39 private final CommandlineHandler commandlineHandler;
40 private File metadataFile;
41 private DirectoryMetadata metadata;
42 private HashSet<Layout> layouts;
44 public FilesystemIndexer(final File directoryToIndex,
45 final CommandlineHandler commandlineHandler,
46 final List<String> pathElements) throws Exception {
48 this.directoryToIndex = directoryToIndex;
49 this.commandlineHandler = commandlineHandler;
50 this.pathElements = pathElements;
52 if (!directoryToIndex.canRead())
55 validMetadataFiles.add(Constants.METADATA_FILE_NAME);
57 initializeThumbnailsDirectory();
58 loadOrCreateMetadata();
64 metadata.removeUnusedMetadataEntries();
66 generateHtmlFromMetadata(layouts);
68 removeUnusedThumbnailFiles();
70 // save directory metadata
72 MetadadaHelper.saveDirectoryMetadata(metadataFile, metadata);
76 private boolean canWriteIndexFile(final File indexFile)
79 return !indexFile.exists() || Utils.isMevizGeneratedIndexFile(indexFile);
83 private void generateHtmlFromMetadata(final HashSet<Layout> layouts) {
84 // Generate HTML from metadata
85 for (final Layout layout : layouts)
87 final File indexFile = Utils.getLayoutIndexFile(layout,
90 if (canWriteIndexFile(indexFile)) {
92 compileHtml(layout, metadata);
94 IOHelper.overwriteFileIfContentDiffers(indexFile, layout
95 .getHtml(true, true, metadata).getBytes());
97 } catch (final Exception e) {
98 logger.error("Error writing index file. ", e);
102 private List<String> getChildPath(final File file) {
104 final List<String> result = new ArrayList<>();
105 result.addAll(pathElements);
106 result.add(file.getName());
112 public String getDirectoryUrl(final AbstractFile directory,
113 final IndexingContext context) {
114 return Utils.urlEncode(directory.fileName) + "/index.html";
118 public String getParentDirectoryUrl(final IndexingContext context) {
119 return "../index.html";
123 public String getThumbnailUrl(final Picture picture,
124 final Dimension desiredDimension, final IndexingContext context) {
126 // in case thumbnail size was equal to original, then return original
128 if (picture.getDimensions().equals(desiredDimension))
129 return picture.fileName;
131 final String thumbnailFileName = picture
132 .getRelativeThumbnailFileName(desiredDimension);
134 validMetadataFiles.add(thumbnailFileName);
135 final File thumbnailFile = new File(
136 Utils.getThumbnailsDirectoryPath(directoryToIndex)
137 + thumbnailFileName);
139 if (!thumbnailFile.exists()) {
141 final File originalFile = new File(
142 directoryToIndex.getAbsolutePath() + "/" + picture.fileName);
144 // generate new thumbnail
145 Picture.makeThumbnail(originalFile, thumbnailFile,
146 desiredDimension.getAwtDimension());
148 return Constants.THUMBNAILS_DIRECTORY_NAME + "/"
149 + Utils.urlEncode(thumbnailFileName);
152 private void initializeLayouts() {
154 layouts = Utils.getLayouts();
156 final String galleryTitle = commandlineHandler.getGalleryTitle();
158 for (final Layout layout : layouts)
159 layout.init(galleryTitle, pathElements, this, new IndexingContext(
163 private void initializeThumbnailsDirectory() {
164 final File thumbnailsDirectory = Utils
165 .getThumbnailsDirectory(directoryToIndex);
167 if (!thumbnailsDirectory.exists())
168 thumbnailsDirectory.mkdirs();
171 private void loadOrCreateMetadata() {
173 metadataFile = new File(
174 Utils.getThumbnailsDirectoryPath(directoryToIndex)
175 + Constants.METADATA_FILE_NAME);
177 metadata = MetadadaHelper.initDirectoryMetadata(metadataFile);
180 private void removeUnusedThumbnailFiles() {
182 for (final File file : Utils.getThumbnailsDirectory(directoryToIndex)
184 if (!validMetadataFiles.contains(file.getName()))
189 private void updateMetadata() throws Exception {
191 for (final File file : directoryToIndex.listFiles())
192 if (shallFileBeIndexed(file)) {
194 metadata.ensureFileMetadataIsUpToDate(directoryToIndex, file);
196 if (file.isDirectory())
197 new FilesystemIndexer(file, commandlineHandler,