X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fmeviz%2Fhtmlindexer%2FUtils.java;h=9da4656bc0bccadac2b5cb18417066bcb9cf3e7a;hb=115456739edd5ae7dc67ac9e9f90d628aa65b4a3;hp=b21f8e9a59e33175b9e481212b0a7a0851f27e00;hpb=50cb7085d553fdd82cd06806cd27b1675299f719;p=meviz.git diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java index b21f8e9..9da4656 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java @@ -1,7 +1,7 @@ /* * Meviz - Various tools collection to work with multimedia. * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public License * as published by the Free Software Foundation. @@ -10,58 +10,119 @@ package eu.svjatoslav.meviz.htmlindexer; import java.awt.image.BufferedImage; +import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; +import java.util.HashSet; import java.util.zip.CRC32; import javax.imageio.ImageIO; +import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout; + public class Utils { - private static File lastLoadedFile; + /** + * Load image into {@link BufferedImage} and return it. Caches last loaded + * image to speed up subsequent loading attempts. + * + * @throws ImageFormatError + * @throws IOException + */ + public static BufferedImage getBufferedImage(final File file) + throws ImageFormatError, IOException { + if (file.equals(lastLoadedFile)) + return lastLoadedBufferedImage; + + System.out.println("Loading image: " + file.getPath()); + lastLoadedBufferedImage = ImageIO.read(file); + lastLoadedFile = file; + + if (lastLoadedBufferedImage == null) { + System.out.println("Error reading image: " + file); + throw new ImageFormatError("File: " + file + + " is not a valid image."); + + } + + return lastLoadedBufferedImage; + } + + public static File getLayoutIndexFile(final Layout layout, + final File directoryToIndex) { + + final String indexFilePath = directoryToIndex.getAbsolutePath() + + "/index" + layout.getFileNameSuffix() + ".html"; + + return new File(indexFilePath); + } + + public static HashSet getLayouts() { + final HashSet layouts = new HashSet(); + layouts.add(new MixedLayout()); + return layouts; + } + + public static String getStringCrcAsHex(final String input) { + + // create a new CRC-calculating object + final CRC32 crc = new CRC32(); + + // loop, calculating CRC for each byte of the string + // There is no CRC16.update(byte[]) method. + for (final byte b : input.getBytes()) + crc.update(b); + + // note use crc.value, not crc.getValue() + final String hex = Integer.toHexString((int) crc.getValue()) + .toUpperCase(); + + // System.out.println("Input string: " + input); + // System.out.println("Result: " + hex); + + return hex; + } - private static BufferedImage lastLoadedBufferedImage; + public static File getThumbnailsDirectory(final File directoryToIndex) { + return new File(getThumbnailsDirectoryPath(directoryToIndex)); + } - /** - * Load image into {@link BufferedImage} and return it. Caches last loaded - * image to speed up subsequent loading attempts. - * - * @throws ImageFormatError - * @throws IOException - */ - public static BufferedImage getBufferedImage(final File file) throws ImageFormatError, IOException { - if (file.equals(lastLoadedFile)) { - return lastLoadedBufferedImage; - } + public static String getThumbnailsDirectoryPath(final File directoryToIndex) { + return directoryToIndex.getAbsolutePath() + "/" + + Constants.THUMBNAILS_DIRECTORY_NAME + "/"; + } - System.out.println("Loading image: " + file.getPath()); - lastLoadedBufferedImage = ImageIO.read(file); - lastLoadedFile = file; + public static boolean isMevizGeneratedIndexFile(final File indexFile) + throws FileNotFoundException, IOException { - if (lastLoadedBufferedImage == null) - throw new ImageFormatError("File: " + file + " is not a valid image."); + boolean isMevizFile = false; - return lastLoadedBufferedImage; - } + final FileReader fileReader = new FileReader(indexFile); + final BufferedReader reader = new BufferedReader(fileReader); - public static String getStringCrcAsHex(final String input) { + parseFile: { + while (true) { + final String line = reader.readLine(); - // create a new CRC-calculating object - final CRC32 crc = new CRC32(); + if (line == null) + break parseFile; - // loop, calculating CRC for each byte of the string - // There is no CRC16.update(byte[]) method. - for (final byte b : input.getBytes()) { - crc.update(b); - } + if (line.contains(Constants.HTML_MAGIC_STRING)) { + isMevizFile = true; + break parseFile; + } + } + } - // note use crc.value, not crc.getValue() - final String hex = Integer.toHexString((int) crc.getValue()).toUpperCase(); + reader.close(); + fileReader.close(); + return isMevizFile; + } - // System.out.println("Input string: " + input); - // System.out.println("Result: " + hex); + private static File lastLoadedFile; - return hex; - } + private static BufferedImage lastLoadedBufferedImage; }