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