Updated copyright message.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / Utils.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012 -- 2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
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.
8  */
9
10 package eu.svjatoslav.meviz.htmlindexer;
11
12 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
13
14 import javax.imageio.ImageIO;
15 import java.awt.image.BufferedImage;
16 import java.io.BufferedReader;
17 import java.io.File;
18 import java.io.FileReader;
19 import java.io.IOException;
20 import java.util.HashSet;
21 import java.util.zip.CRC32;
22
23 public class Utils {
24
25     private static final String UTF_8 = "UTF-8";
26     private static File lastLoadedFile;
27     private static BufferedImage lastLoadedBufferedImage;
28
29     /**
30      * Load image into {@link BufferedImage} and return it. Caches last loaded
31      * image to speed up subsequent loading attempts.
32      *
33      * @throws ImageFormatError
34      * @throws IOException
35      */
36     public static BufferedImage getBufferedImage(final File file)
37             throws ImageFormatError, IOException {
38         if (file.equals(lastLoadedFile))
39             return lastLoadedBufferedImage;
40
41         System.out.println("Loading image: " + file.getPath());
42         lastLoadedBufferedImage = ImageIO.read(file);
43         lastLoadedFile = file;
44
45         if (lastLoadedBufferedImage == null) {
46             System.out.println("Error reading image: " + file);
47             throw new ImageFormatError("File: " + file
48                     + " is not a valid image.");
49
50         }
51
52         return lastLoadedBufferedImage;
53     }
54
55     public static File getLayoutIndexFile(final Layout layout,
56                                           final File directoryToIndex) {
57
58         final String indexFilePath = directoryToIndex.getAbsolutePath()
59                 + "/index" + layout.getFileNameSuffix() + ".html";
60
61         return new File(indexFilePath);
62     }
63
64     public static HashSet<Layout> getLayouts() {
65         final HashSet<Layout> layouts = new HashSet<>();
66         layouts.add(new MixedLayout());
67         return layouts;
68     }
69
70     public static String getStringCrcAsHex(final String input) {
71
72         // create a new CRC-calculating object
73         final CRC32 crc = new CRC32();
74
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())
78             crc.update(b);
79
80         // note use crc.value, not crc.getValue()
81
82         return Integer.toHexString((int) crc.getValue())
83                 .toUpperCase();
84     }
85
86     public static File getThumbnailsDirectory(final File directoryToIndex) {
87         return new File(getThumbnailsDirectoryPath(directoryToIndex));
88     }
89
90     public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
91         return directoryToIndex.getAbsolutePath() + "/"
92                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
93     }
94
95     public static boolean isMevizGeneratedIndexFile(final File indexFile)
96             throws IOException {
97
98         boolean isMevizFile = false;
99
100         final FileReader fileReader = new FileReader(indexFile);
101         final BufferedReader reader = new BufferedReader(fileReader);
102
103         parseFile:
104         {
105             while (true) {
106                 final String line = reader.readLine();
107
108                 if (line == null)
109                     break parseFile;
110
111                 if (line.contains(Constants.HTML_MAGIC_STRING)) {
112                     isMevizFile = true;
113                     break parseFile;
114                 }
115             }
116         }
117
118         reader.close();
119         fileReader.close();
120         return isMevizFile;
121     }
122
123     /**
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
127      * hacked together.
128      */
129     public static String urlEncode(String string) {
130         if (string.startsWith("./"))
131             string = string.substring(2);
132
133         // TODO: get rid of UrlParamEncoder.
134         return UrlParamEncoder.encode(string);
135         //
136 //        try {
137 //            return URLEncoder.encode(string, UTF_8).replace("+", "%20");
138 //        } catch (UnsupportedEncodingException e) {
139 //            throw new RuntimeException(e);
140 //        }
141     }
142 }