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.metadata;
9 import eu.svjatoslav.commons.file.FilePathParser;
10 import eu.svjatoslav.meviz.htmlindexer.FileSortComparator;
11 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.*;
14 import java.io.Serializable;
17 import static eu.svjatoslav.meviz.htmlindexer.indexer.AbstractIndexer.isImage;
18 import static eu.svjatoslav.meviz.htmlindexer.indexer.AbstractIndexer.isVideo;
21 * Corresponds to single filesystem directory.
23 public class DirectoryMetadata implements Serializable {
25 private static final long serialVersionUID = 3526489102725366334L;
27 private final HashMap<String, AbstractFile> fileNameToFileMap = new HashMap<>();
29 public transient boolean changed;
32 * Performs cleanup of directory model. In particular removes metadata for
33 * images that no longer exist in the filesystem.
35 public void cleanup() {
36 fileNameToFileMap.values().removeIf(file -> !file.isMetadataVerified());
39 public void ensureFileMetadataIsUpToDate(final File parentDirectory,
40 final File file) throws Exception {
42 final String fileName = file.getName();
44 if (fileNameToFileMap.containsKey(fileName)) {
45 final boolean metadataUpdated = fileNameToFileMap.get(fileName)
46 .ensureFileMetadataIsUpToDate(parentDirectory);
51 registerFile(parentDirectory, file);
55 private AbstractFile getAbstractFile(final File parentDirectory,
56 final File file, final String fileName) throws Exception {
58 if (file.isDirectory())
59 return new DirectoryFile(parentDirectory, fileName);
61 final String fileExtension = FilePathParser.getFileExtension(fileName)
64 if (isImage(fileExtension))
66 return new Picture(parentDirectory, fileName);
67 } catch (final Exception exception) {
68 System.out.println("Failed to decode image \"" + fileName +"\" indexing as normal file instead.");
70 // in case image decoding failed, handle image as general file
71 return new GeneralFile(parentDirectory, fileName);
74 if (isVideo(fileExtension))
76 return new Video(parentDirectory, fileName);
77 } catch (final Exception exception) {
78 // in case video decoding failed, handle it as general file
79 return new GeneralFile(parentDirectory, fileName);
82 if (fileName.endsWith(".meviz.org"))
83 return new MevizSpecialFile(parentDirectory, fileName);
85 if (fileName.endsWith(".meviz.html"))
86 return new MevizSpecialFile(parentDirectory, fileName);
88 return new GeneralFile(parentDirectory, fileName);
91 private AbstractFile getFile(final String fileName) {
92 return fileNameToFileMap.get(fileName);
95 public Collection<AbstractFile> getFiles() {
97 final ArrayList<AbstractFile> files = new ArrayList<>(
98 fileNameToFileMap.values());
100 files.sort(new FileSortComparator());
105 public String getHeaderMevizMetadataHtml() {
106 final AbstractFile headerFile = getFile("header.meviz.html");
107 if (headerFile == null)
110 final MevizSpecialFile mevizSpecialFile = (MevizSpecialFile) headerFile;
111 return mevizSpecialFile.getFileContent();
114 public Dimension getImageDimensions(final String fileName) {
116 return ((Picture) getFile(fileName)).getDimensions();
119 private AbstractFile registerFile(final File parentDirectory, final File file)
122 final String fileName = file.getName();
124 final AbstractFile abstractFile = getAbstractFile(parentDirectory,
127 fileNameToFileMap.put(fileName, abstractFile);
134 public void removeUnusedMetadataEntries() {
136 new HashSet<>(fileNameToFileMap.keySet()).stream().filter(key -> !fileNameToFileMap.get(key).isMetadataVerified()).forEach(key -> {
137 fileNameToFileMap.remove(key);