added webm support
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / Utils.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
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.
8  */
9
10 package eu.svjatoslav.meviz.htmlindexer;
11
12 import java.awt.image.BufferedImage;
13 import java.io.BufferedReader;
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.io.FileReader;
17 import java.io.IOException;
18 import java.util.HashSet;
19 import java.util.zip.CRC32;
20
21 import javax.imageio.ImageIO;
22
23 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
24
25 public class Utils {
26
27         private static File lastLoadedFile;
28
29         private static BufferedImage lastLoadedBufferedImage;
30
31         /**
32          * Load image into {@link BufferedImage} and return it. Caches last loaded
33          * image to speed up subsequent loading attempts.
34          *
35          * @throws ImageFormatError
36          * @throws IOException
37          */
38         public static BufferedImage getBufferedImage(final File file)
39                         throws ImageFormatError, IOException {
40                 if (file.equals(lastLoadedFile))
41                         return lastLoadedBufferedImage;
42
43                 System.out.println("Loading image: " + file.getPath());
44                 lastLoadedBufferedImage = ImageIO.read(file);
45                 lastLoadedFile = file;
46
47                 if (lastLoadedBufferedImage == null) {
48                         System.out.println("Error reading image: " + file);
49                         throw new ImageFormatError("File: " + file
50                                         + " is not a valid image.");
51
52                 }
53
54                 return lastLoadedBufferedImage;
55         }
56
57         public static File getLayoutIndexFile(final Layout layout,
58                         final File directoryToIndex) {
59
60                 final String indexFilePath = directoryToIndex.getAbsolutePath()
61                                 + "/index" + layout.getFileNameSuffix() + ".html";
62
63                 return new File(indexFilePath);
64         }
65
66         public static HashSet<Layout> getLayouts() {
67                 final HashSet<Layout> layouts = new HashSet<Layout>();
68                 layouts.add(new MixedLayout());
69                 return layouts;
70         }
71
72         public static String getStringCrcAsHex(final String input) {
73
74                 // create a new CRC-calculating object
75                 final CRC32 crc = new CRC32();
76
77                 // loop, calculating CRC for each byte of the string
78                 // There is no CRC16.update(byte[]) method.
79                 for (final byte b : input.getBytes())
80                         crc.update(b);
81
82                 // note use crc.value, not crc.getValue()
83                 final String hex = Integer.toHexString((int) crc.getValue())
84                                 .toUpperCase();
85
86                 // System.out.println("Input string: " + input);
87                 // System.out.println("Result: " + hex);
88
89                 return hex;
90         }
91
92         public static File getThumbnailsDirectory(final File directoryToIndex) {
93                 return new File(getThumbnailsDirectoryPath(directoryToIndex));
94         }
95
96         public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
97                 return directoryToIndex.getAbsolutePath() + "/"
98                                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
99         }
100
101         public static boolean isMevizGeneratedIndexFile(final File indexFile)
102                         throws FileNotFoundException, IOException {
103
104                 boolean isMevizFile = false;
105
106                 final FileReader fileReader = new FileReader(indexFile);
107                 final BufferedReader reader = new BufferedReader(fileReader);
108
109                 parseFile: {
110                         while (true) {
111                                 final String line = reader.readLine();
112
113                                 if (line == null)
114                                         break parseFile;
115
116                                 if (line.contains(Constants.HTML_MAGIC_STRING)) {
117                                         isMevizFile = true;
118                                         break parseFile;
119                                 }
120                         }
121                 }
122
123                 reader.close();
124                 fileReader.close();
125                 return isMevizFile;
126         }
127
128 }