2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2018, 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 java.awt.image.BufferedImage;
15 import java.io.BufferedReader;
17 import java.io.FileReader;
18 import java.io.IOException;
19 import java.util.HashSet;
20 import java.util.zip.CRC32;
22 import static javax.imageio.ImageIO.read;
25 private static File lastLoadedFile;
26 private static BufferedImage lastLoadedBufferedImage;
29 * Load image into {@link BufferedImage} and return it. Caches last loaded
30 * image to speed up subsequent loading attempts.
32 * @throws ImageFormatError
35 public static BufferedImage getBufferedImage(final File file)
36 throws ImageFormatError, IOException {
37 if (file.equals(lastLoadedFile))
38 return lastLoadedBufferedImage;
40 System.out.println("Loading image: " + file.getPath());
41 lastLoadedBufferedImage = read(file);
42 lastLoadedFile = file;
44 if (lastLoadedBufferedImage == null) {
45 System.out.println("Error reading image: " + file);
46 throw new ImageFormatError("File: " + file
47 + " is not a valid image.");
51 return lastLoadedBufferedImage;
54 public static File getLayoutIndexFile(final Layout layout,
55 final File directoryToIndex) {
57 final String indexFilePath = directoryToIndex.getAbsolutePath()
58 + "/index" + layout.getFileNameSuffix() + ".html";
60 return new File(indexFilePath);
63 public static HashSet<Layout> getLayouts() {
64 final HashSet<Layout> layouts = new HashSet<>();
65 layouts.add(new MixedLayout());
69 public static String getStringCrcAsHex(final String input) {
71 // create a new CRC-calculating object
72 final CRC32 crc = new CRC32();
74 // loop, calculating CRC for each byte of the string
75 // There is no CRC16.update(byte[]) method.
76 for (final byte b : input.getBytes())
79 // note use crc.value, not crc.getValue()
81 return Integer.toHexString((int) crc.getValue())
85 public static File getThumbnailsDirectory(final File directoryToIndex) {
86 return new File(getThumbnailsDirectoryPath(directoryToIndex));
89 public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
90 return directoryToIndex.getAbsolutePath() + "/"
91 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
94 public static boolean isMevizGeneratedIndexFile(final File indexFile)
97 boolean isMevizFile = false;
99 final FileReader fileReader = new FileReader(indexFile);
100 final BufferedReader reader = new BufferedReader(fileReader);
105 final String line = reader.readLine();
110 if (line.contains(Constants.HTML_MAGIC_STRING)) {
123 * TODO: URL path component is encoded differently from URL query parameter.
124 * Also some URL encoding might work for HTML on local filesystem, while other
125 * stuff works for web. Things must be cleared up here. Currently they are mixed and
128 public static String urlEncode(String string) {
129 if (string.startsWith("./"))
130 string = string.substring(2);
132 // TODO: get rid of UrlParamEncoder.
133 return UrlParamEncoder.encode(string);
136 // return URLEncoder.encode(string, UTF_8).replace("+", "%20");
137 // } catch (UnsupportedEncodingException e) {
138 // throw new RuntimeException(e);