2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2017, 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;
16 import java.io.BufferedReader;
18 import java.io.FileReader;
19 import java.io.IOException;
20 import java.util.HashSet;
21 import java.util.zip.CRC32;
25 private static final String UTF_8 = "UTF-8";
26 private static File lastLoadedFile;
27 private static BufferedImage lastLoadedBufferedImage;
30 * Load image into {@link BufferedImage} and return it. Caches last loaded
31 * image to speed up subsequent loading attempts.
33 * @throws ImageFormatError
36 public static BufferedImage getBufferedImage(final File file)
37 throws ImageFormatError, IOException {
38 if (file.equals(lastLoadedFile))
39 return lastLoadedBufferedImage;
41 System.out.println("Loading image: " + file.getPath());
42 lastLoadedBufferedImage = ImageIO.read(file);
43 lastLoadedFile = file;
45 if (lastLoadedBufferedImage == null) {
46 System.out.println("Error reading image: " + file);
47 throw new ImageFormatError("File: " + file
48 + " is not a valid image.");
52 return lastLoadedBufferedImage;
55 public static File getLayoutIndexFile(final Layout layout,
56 final File directoryToIndex) {
58 final String indexFilePath = directoryToIndex.getAbsolutePath()
59 + "/index" + layout.getFileNameSuffix() + ".html";
61 return new File(indexFilePath);
64 public static HashSet<Layout> getLayouts() {
65 final HashSet<Layout> layouts = new HashSet<>();
66 layouts.add(new MixedLayout());
70 public static String getStringCrcAsHex(final String input) {
72 // create a new CRC-calculating object
73 final CRC32 crc = new CRC32();
75 // loop, calculating CRC for each byte of the string
76 // There is no CRC16.update(byte[]) method.
77 for (final byte b : input.getBytes())
80 // note use crc.value, not crc.getValue()
82 return Integer.toHexString((int) crc.getValue())
86 public static File getThumbnailsDirectory(final File directoryToIndex) {
87 return new File(getThumbnailsDirectoryPath(directoryToIndex));
90 public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
91 return directoryToIndex.getAbsolutePath() + "/"
92 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
95 public static boolean isMevizGeneratedIndexFile(final File indexFile)
98 boolean isMevizFile = false;
100 final FileReader fileReader = new FileReader(indexFile);
101 final BufferedReader reader = new BufferedReader(fileReader);
106 final String line = reader.readLine();
111 if (line.contains(Constants.HTML_MAGIC_STRING)) {
124 * TODO: URL path component is encoded differently from URL query parameter.
125 * Also some URL encoding might work for HTML on local filesystem, while other
126 * stuff works for web. Things must be cleared up here. Currently they are mixed and
129 public static String urlEncode(String string) {
130 if (string.startsWith("./"))
131 string = string.substring(2);
133 // TODO: get rid of UrlParamEncoder.
134 return UrlParamEncoder.encode(string);
137 // return URLEncoder.encode(string, UTF_8).replace("+", "%20");
138 // } catch (UnsupportedEncodingException e) {
139 // throw new RuntimeException(e);