2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, 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;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
19 import org.apache.log4j.Logger;
21 import eu.svjatoslav.commons.file.IOHelper;
22 import eu.svjatoslav.commons.network.UrlParamEncoder;
23 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
24 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
25 import eu.svjatoslav.meviz.htmlindexer.metadata.MetadadaHelper;
26 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
27 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
30 * Main class of HTML indexer.
33 public class FilesystemIndexer extends AbstractIndexer {
35 public static final Logger logger = Logger
36 .getLogger(FilesystemIndexer.class);
38 private final HashSet<String> validMetadataFiles = new HashSet<String>();
40 private final File directoryToIndex;
42 private File metadataFile;
44 private DirectoryMetadata metadata;
46 private final List<String> pathElements;
48 private HashSet<Layout> layouts;
50 private final CommandlineHandler commandlineHandler;
52 public FilesystemIndexer(final File directoryToIndex,
53 final CommandlineHandler commandlineHandler,
54 final List<String> pathElements) throws Exception {
56 this.directoryToIndex = directoryToIndex;
57 this.commandlineHandler = commandlineHandler;
58 this.pathElements = pathElements;
60 if (!directoryToIndex.canRead())
63 validMetadataFiles.add(Constants.METADATA_FILE_NAME);
65 initializeThumbnailsDirectory();
66 loadOrCreateMetadata();
72 metadata.removeUnusedMetadataEntries();
74 generateHtmlFromMetadata(layouts);
76 removeUnusedThumbnailFiles();
78 // save directory metadata
80 MetadadaHelper.saveDirectoryMetadata(metadataFile, metadata);
84 public boolean canWriteIndexFile(final File indexFile)
85 throws FileNotFoundException, IOException {
87 if (!indexFile.exists())
90 return Utils.isMevizGeneratedIndexFile(indexFile);
93 public void generateHtmlFromMetadata(final HashSet<Layout> layouts) {
94 // Generate HTML from metadata
95 for (final Layout layout : layouts)
97 final File indexFile = Utils.getLayoutIndexFile(layout,
100 if (canWriteIndexFile(indexFile)) {
102 compileHtml(layout, metadata);
104 IOHelper.overwriteFileIfContentDiffers(indexFile, layout
105 .getHtml(true, true, metadata).getBytes());
107 } catch (final Exception e) {
108 logger.error("Error writing index file. ", e);
112 public List<String> getChildPath(final File file) {
114 final List<String> result = new ArrayList<String>();
115 result.addAll(pathElements);
116 result.add(file.getName());
122 public String getDirectoryUrl(final AbstractFile directory,
123 final IndexingContext context) {
124 return directory.fileName + "/index.html";
128 public String getParentDirectoryUrl(final IndexingContext context) {
129 return "../index.html";
133 public String getThumbnailUrl(final Picture picture,
134 final Dimension desiredDimension, final IndexingContext context) {
136 // in case thumbnail size was equal to original, then return original
138 if (picture.getDimensions().equals(desiredDimension))
139 return picture.fileName;
141 final String thumbnailFileName = picture
142 .getRelativeThumbnailFileName(desiredDimension);
144 validMetadataFiles.add(thumbnailFileName);
145 final File thumbnailFile = new File(
146 Utils.getThumbnailsDirectoryPath(directoryToIndex)
147 + thumbnailFileName);
149 if (!thumbnailFile.exists()) {
151 final File originalFile = new File(
152 directoryToIndex.getAbsolutePath() + "/" + picture.fileName);
154 // generate new thumbnail
155 Picture.makeThumbnail(originalFile, thumbnailFile,
156 desiredDimension.getAwtDimension());
158 return Constants.THUMBNAILS_DIRECTORY_NAME + "/"
159 + UrlParamEncoder.encode(thumbnailFileName);
162 public void initializeLayouts() {
164 layouts = Utils.getLayouts();
166 final String galleryTitle = commandlineHandler.getGalleryTitle();
168 for (final Layout layout : layouts)
169 layout.init(galleryTitle, pathElements, this, new IndexingContext(
173 public void initializeThumbnailsDirectory() {
174 final File thumbnailsDirectory = Utils
175 .getThumbnailsDirectory(directoryToIndex);
177 if (!thumbnailsDirectory.exists())
178 thumbnailsDirectory.mkdirs();
181 public void loadOrCreateMetadata() {
183 metadataFile = new File(
184 Utils.getThumbnailsDirectoryPath(directoryToIndex)
185 + Constants.METADATA_FILE_NAME);
187 metadata = MetadadaHelper.initDirectoryMetadata(metadataFile);
190 private void removeUnusedThumbnailFiles() {
192 for (final File file : Utils.getThumbnailsDirectory(directoryToIndex)
194 if (!validMetadataFiles.contains(file.getName()))
199 public void updateMetadata() throws Exception {
201 for (final File file : directoryToIndex.listFiles())
202 if (shallFileBeIndexed(file)) {
204 metadata.ensureFileMetainfoIsUpToDate(directoryToIndex, file);
206 if (file.isDirectory())
207 new FilesystemIndexer(file, commandlineHandler,