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;
12 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
14 import javax.imageio.ImageIO;
15 import java.awt.image.BufferedImage;
17 import java.net.URLEncoder;
18 import java.util.HashSet;
19 import java.util.zip.CRC32;
23 private static final String UTF_8 = "UTF-8";
24 private static File lastLoadedFile;
25 private static BufferedImage lastLoadedBufferedImage;
28 * Load image into {@link BufferedImage} and return it. Caches last loaded
29 * image to speed up subsequent loading attempts.
31 * @throws ImageFormatError
34 public static BufferedImage getBufferedImage(final File file)
35 throws ImageFormatError, IOException {
36 if (file.equals(lastLoadedFile))
37 return lastLoadedBufferedImage;
39 System.out.println("Loading image: " + file.getPath());
40 lastLoadedBufferedImage = ImageIO.read(file);
41 lastLoadedFile = file;
43 if (lastLoadedBufferedImage == null) {
44 System.out.println("Error reading image: " + file);
45 throw new ImageFormatError("File: " + file
46 + " is not a valid image.");
50 return lastLoadedBufferedImage;
53 public static File getLayoutIndexFile(final Layout layout,
54 final File directoryToIndex) {
56 final String indexFilePath = directoryToIndex.getAbsolutePath()
57 + "/index" + layout.getFileNameSuffix() + ".html";
59 return new File(indexFilePath);
62 public static HashSet<Layout> getLayouts() {
63 final HashSet<Layout> layouts = new HashSet<Layout>();
64 layouts.add(new MixedLayout());
68 public static String getStringCrcAsHex(final String input) {
70 // create a new CRC-calculating object
71 final CRC32 crc = new CRC32();
73 // loop, calculating CRC for each byte of the string
74 // There is no CRC16.update(byte[]) method.
75 for (final byte b : input.getBytes())
78 // note use crc.value, not crc.getValue()
79 final String hex = Integer.toHexString((int) crc.getValue())
82 // System.out.println("Input string: " + input);
83 // System.out.println("Result: " + hex);
88 public static File getThumbnailsDirectory(final File directoryToIndex) {
89 return new File(getThumbnailsDirectoryPath(directoryToIndex));
92 public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
93 return directoryToIndex.getAbsolutePath() + "/"
94 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
97 public static boolean isMevizGeneratedIndexFile(final File indexFile)
100 boolean isMevizFile = false;
102 final FileReader fileReader = new FileReader(indexFile);
103 final BufferedReader reader = new BufferedReader(fileReader);
108 final String line = reader.readLine();
113 if (line.contains(Constants.HTML_MAGIC_STRING)) {
125 public static String urlEncode(String string) {
126 if (string.startsWith("./"))
127 string = string.substring(2);
130 return URLEncoder.encode(string, UTF_8).replace("+", "%20");
131 } catch (UnsupportedEncodingException e) {
132 throw new RuntimeException(e);