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<>();
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()
80 return Integer.toHexString((int) crc.getValue())
84 public static File getThumbnailsDirectory(final File directoryToIndex) {
85 return new File(getThumbnailsDirectoryPath(directoryToIndex));
88 public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
89 return directoryToIndex.getAbsolutePath() + "/"
90 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
93 public static boolean isMevizGeneratedIndexFile(final File indexFile)
96 boolean isMevizFile = false;
98 final FileReader fileReader = new FileReader(indexFile);
99 final BufferedReader reader = new BufferedReader(fileReader);
104 final String line = reader.readLine();
109 if (line.contains(Constants.HTML_MAGIC_STRING)) {
121 public static String urlEncode(String string) {
122 if (string.startsWith("./"))
123 string = string.substring(2);
126 return URLEncoder.encode(string, UTF_8).replace("+", "%20");
127 } catch (UnsupportedEncodingException e) {
128 throw new RuntimeException(e);