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.metadata;
12 import eu.svjatoslav.commons.file.FilePathParser;
13 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
14 import eu.svjatoslav.meviz.htmlindexer.FileSortComparator;
15 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.*;
18 import java.io.Serializable;
22 * Corresponds to single filesystem directory.
24 public class DirectoryMetadata implements Serializable {
26 private static final long serialVersionUID = 3526489102725366334L;
28 private final HashMap<String, AbstractFile> fileNameToFileMap = new HashMap<>();
30 public transient boolean changed;
33 * Performs cleanup of directory model. In particular removes metadata for
34 * images that no longer exist in the filesystem.
36 public void cleanup() {
37 final Iterator<AbstractFile> fileIterator = fileNameToFileMap.values()
40 while (fileIterator.hasNext()) {
41 final AbstractFile file = fileIterator.next();
43 if (!file.isMetaInfoVerified())
44 fileIterator.remove();
48 public void ensureFileMetainfoIsUpToDate(final File parentDirectory,
49 final File file) throws Exception {
51 final String fileName = file.getName();
53 if (fileNameToFileMap.containsKey(fileName)) {
54 final boolean metainfoUpdated = fileNameToFileMap.get(fileName)
55 .ensureFileMetainfoIsUpToDate(parentDirectory);
60 registerFile(parentDirectory, file);
64 private AbstractFile getAbstractFile(final File parentDirectory,
65 final File file, final String fileName) throws Exception {
67 if (file.isDirectory())
68 return new DirectoryFile(parentDirectory, fileName);
70 final String fileExtension = FilePathParser.getFileExtension(fileName)
73 if (AbstractIndexer.isImage(fileExtension))
75 return new Picture(parentDirectory, fileName);
76 } catch (final Exception exception) {
77 // in case image decoding failed, handle image as general file
78 return new GeneralFile(parentDirectory, fileName);
81 if (fileName.endsWith(".meviz.org"))
82 return new MevizSpecialFile(parentDirectory, fileName);
84 if (fileName.endsWith(".meviz.html"))
85 return new MevizSpecialFile(parentDirectory, fileName);
87 return new GeneralFile(parentDirectory, fileName);
90 private AbstractFile getFile(final String fileName) {
91 return fileNameToFileMap.get(fileName);
94 public Collection<AbstractFile> getFiles() {
96 final ArrayList<AbstractFile> files = new ArrayList<>(
97 fileNameToFileMap.values());
99 Collections.sort(files, new FileSortComparator());
104 public String getHeaderMevizMetadataHtml() {
105 final AbstractFile headerFile = getFile("header.meviz.html");
106 if (headerFile == null)
109 final MevizSpecialFile mevizSpecialFile = (MevizSpecialFile) headerFile;
110 return mevizSpecialFile.getFileContent();
113 public Dimension getImageDimensions(final String fileName) {
115 return ((Picture) getFile(fileName)).getDimensions();
118 private AbstractFile registerFile(final File parentDirectory, final File file)
121 final String fileName = file.getName();
123 final AbstractFile abstractFile = getAbstractFile(parentDirectory,
126 fileNameToFileMap.put(fileName, abstractFile);
133 public void removeUnusedMetadataEntries() {
135 new HashSet<>(fileNameToFileMap.keySet()).stream().filter(key -> !fileNameToFileMap.get(key).isMetaInfoVerified()).forEach(key -> {
136 fileNameToFileMap.remove(key);