|
23 | 23 | */ |
24 | 24 | package pattypan; |
25 | 25 |
|
26 | | -import edu.stanford.ejalbert.BrowserLauncher; |
27 | | -import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException; |
28 | | -import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException; |
29 | 26 | import java.awt.Desktop; |
30 | 27 | import java.awt.EventQueue; |
31 | | -import java.io.BufferedReader; |
32 | 28 | import java.io.File; |
33 | 29 | import java.io.FileInputStream; |
34 | 30 | import java.io.IOException; |
35 | | -import java.io.InputStreamReader; |
36 | 31 | import java.net.URI; |
37 | 32 | import java.net.URISyntaxException; |
38 | 33 | import java.net.URL; |
| 34 | +import java.net.http.HttpClient; |
| 35 | +import java.net.http.HttpRequest; |
| 36 | +import java.net.http.HttpResponse.BodyHandlers; |
39 | 37 | import java.nio.file.DirectoryStream; |
40 | 38 | import java.nio.file.Files; |
41 | 39 | import java.nio.file.Path; |
|
50 | 48 | import java.util.Set; |
51 | 49 | import java.util.logging.Level; |
52 | 50 | import java.util.stream.Collectors; |
| 51 | + |
| 52 | +import edu.stanford.ejalbert.BrowserLauncher; |
| 53 | +import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException; |
| 54 | +import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException; |
53 | 55 | import javafx.geometry.HPos; |
54 | 56 | import javafx.scene.layout.ColumnConstraints; |
55 | 57 |
|
@@ -148,14 +150,24 @@ public static ColumnConstraints newColumn(int value, String unit, HPos position) |
148 | 150 | "SANY", "SAM") |
149 | 151 | ); |
150 | 152 |
|
151 | | - public static boolean stringHasValidFileExtension(String string) { |
| 153 | + // https://www.mediawiki.org/wiki/Manual:Page_title |
| 154 | + private final static ArrayList<String> invalidFilenameCharacters = new ArrayList<>( |
| 155 | + Arrays.asList("#", "<", ">", "[", "]", "|", "{", "}") |
| 156 | + ); |
| 157 | + |
| 158 | + |
| 159 | + public static boolean hasValidFileExtension(String string) { |
152 | 160 | return allowedFileExtension.parallelStream().anyMatch(string::endsWith); |
153 | 161 | } |
154 | 162 |
|
155 | | - public static boolean isPossibleBadFilename(String name) { |
| 163 | + public static boolean hasPossibleBadFilenamePrefix(String name) { |
156 | 164 | return filenamePrefixBlacklist.parallelStream().anyMatch(name::startsWith); |
157 | 165 | } |
158 | 166 |
|
| 167 | + public static boolean hasInvalidFilenameCharacters(String name) { |
| 168 | + return invalidFilenameCharacters.parallelStream().anyMatch(name::contains); |
| 169 | + } |
| 170 | + |
159 | 171 | public static String getNameFromFilename(String filename) { |
160 | 172 | int pos = filename.lastIndexOf("."); |
161 | 173 | if (pos > 0) { |
@@ -192,9 +204,10 @@ public static File[] getFilesAllowedToUpload(File directory, boolean includeSubd |
192 | 204 | } |
193 | 205 |
|
194 | 206 | public static File[] getFilesAllowedToUpload(File directory, String ext) { |
195 | | - return directory.listFiles((File dir, String name) |
196 | | - -> name.toLowerCase().endsWith(ext) |
197 | | - ); |
| 207 | + File[] files = directory.listFiles((File dir, String name) -> name.toLowerCase().endsWith(ext)); |
| 208 | + Arrays.sort(files); |
| 209 | + return files; |
| 210 | + |
198 | 211 | } |
199 | 212 |
|
200 | 213 | public static Map<String, Integer> getFilesByExtention(File[] files) { |
@@ -254,22 +267,18 @@ public static String getFileChecksum(MessageDigest digest, File file) throws IOE |
254 | 267 | } |
255 | 268 |
|
256 | 269 | public static String readUrl(String urlString) throws Exception { |
257 | | - BufferedReader reader = null; |
258 | 270 | try { |
259 | | - URL url = new URL(urlString); |
260 | | - reader = new BufferedReader(new InputStreamReader(url.openStream())); |
261 | | - StringBuilder buffer = new StringBuilder(); |
262 | | - int read; |
263 | | - char[] chars = new char[1024]; |
264 | | - while ((read = reader.read(chars)) != -1) { |
265 | | - buffer.append(chars, 0, read); |
266 | | - } |
| 271 | + HttpClient client = HttpClient.newHttpClient(); |
267 | 272 |
|
268 | | - return buffer.toString(); |
269 | | - } finally { |
270 | | - if (reader != null) { |
271 | | - reader.close(); |
272 | | - } |
| 273 | + HttpRequest request = HttpRequest.newBuilder( |
| 274 | + URI.create(urlString)) |
| 275 | + .header("user-agent", Settings.USERAGENT) |
| 276 | + .build(); |
| 277 | + |
| 278 | + var response = client.send(request, BodyHandlers.ofString()); |
| 279 | + return response.body(); |
| 280 | + } catch (Exception e) { |
| 281 | + throw new Exception("Error while getting JSON from " + urlString); |
273 | 282 | } |
274 | 283 | } |
275 | 284 |
|
|
0 commit comments