Use Apache Tika to handle videos.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / metadata / fileTypes / AbstractFile.java
1 package eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes;
2
3 import eu.svjatoslav.commons.file.FilePathParser;
4
5 import java.io.File;
6 import java.io.Serializable;
7
8 public abstract class AbstractFile implements Serializable {
9
10     private static final long serialVersionUID = -2547388204667088033L;
11
12     /**
13      * File name relative to the immediate parent directory.
14      */
15     public final String fileName;
16
17     /**
18      * File length in bytes.
19      */
20     private long fileLength = -1;
21
22     private transient boolean metadataVerified;
23
24     AbstractFile(final File parentDirectory, final String fileName)
25             throws Exception {
26         this.fileName = fileName;
27         ensureFileMetadataIsUpToDate(parentDirectory);
28     }
29
30     /**
31      * @return <code>true</code> if file metadata was updated.
32      */
33     public boolean ensureFileMetadataIsUpToDate(final File parentDirectory)
34             throws Exception {
35         if (!isMetadataUpToDate(parentDirectory)) {
36             fileLength = getFile(parentDirectory).length();
37             updateFileMetadata(parentDirectory);
38             metadataVerified = true;
39             return true;
40         }
41         return false;
42     }
43
44     public boolean fileExists(final File parentDirectory) {
45         return getFile(parentDirectory).exists();
46     }
47
48     File getFile(final File parentDirectory) {
49         return new File(parentDirectory.getAbsolutePath(), fileName);
50     }
51
52     public String getFileExtension() {
53         return FilePathParser.getFileExtension(fileName);
54     }
55
56     public long getFileLength() {
57         return fileLength;
58     }
59
60     private boolean isMetadataUpToDate(final java.io.File parentDirectory) {
61
62         if (metadataVerified)
63             return true;
64
65         final File file = getFile(parentDirectory);
66
67         // first check that file exists at all
68         if (!file.exists()) {
69             System.out.println(file);
70             throw new RuntimeException("Picture file by name \"" + fileName
71                     + "\" does not exist in the parent directory \""
72                     + parentDirectory.getAbsolutePath() + "\"");
73         }
74
75         // check that file length is the same as before
76         if (file.length() != fileLength)
77             return false;
78
79         metadataVerified = true;
80         return true;
81     }
82
83     public boolean isMetadataVerified() {
84         return metadataVerified;
85     }
86
87     protected abstract void updateFileMetadata(final File parentDirectory) throws Exception;
88
89 }