Skip to content

Commit

Permalink
Pattypan 22.03
Browse files Browse the repository at this point in the history
Pattypan 22.03
  • Loading branch information
Abbe98 authored Mar 29, 2022
2 parents 5b3c5f1 + 4c33790 commit ae10043
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 39 deletions.
Binary file removed lib/gson-2.8.5.jar
Binary file not shown.
Binary file added lib/gson-2.9.0.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions src/pattypan/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public static void main(String[] args) {
);

Session.WIKI = Wiki.newSession(wiki, scriptPath, protocol);
Session.WIKI.setUserAgent(Settings.USERAGENT);
Session.LOGGER.log(Level.INFO, "User-Agent: {0}", Session.WIKI.getUserAgent());
launch(args);
}
}
7 changes: 2 additions & 5 deletions src/pattypan/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ public final class Session {

public static Logger LOGGER = new LogManager().logger;

public static Wiki WIKI;

public static String METHOD = "template";
public static String TEMPLATE = "Artwork";
public static String WIKICODE = "";
public static ArrayList<String> VARIABLES = new ArrayList<>(Arrays.asList("path", "name"));

public static Wiki WIKI = Wiki.newSession("commons.wikimedia.org");
public static ArrayList<UploadElement> FILES_TO_UPLOAD = new ArrayList<>();

static {
WIKI.setUserAgent(Settings.USERAGENT);
}
}
2 changes: 1 addition & 1 deletion src/pattypan/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class Settings {
private Settings() {};

public static final String NAME = "pattypan";
public static final String VERSION = "22.02";
public static final String VERSION = "22.03";
public static final String USERAGENT = NAME + "/" + VERSION + " (https://github.com/yarl/pattypan)";

public static final Map<String, String> SETTINGS = new HashMap<>();
Expand Down
57 changes: 33 additions & 24 deletions src/pattypan/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,17 @@
*/
package pattypan;

import edu.stanford.ejalbert.BrowserLauncher;
import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException;
import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -50,6 +48,10 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;

import edu.stanford.ejalbert.BrowserLauncher;
import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException;
import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException;
import javafx.geometry.HPos;
import javafx.scene.layout.ColumnConstraints;

Expand Down Expand Up @@ -148,14 +150,24 @@ public static ColumnConstraints newColumn(int value, String unit, HPos position)
"SANY", "SAM")
);

public static boolean stringHasValidFileExtension(String string) {
// https://www.mediawiki.org/wiki/Manual:Page_title
private final static ArrayList<String> invalidFilenameCharacters = new ArrayList<>(
Arrays.asList("#", "<", ">", "[", "]", "|", "{", "}")
);


public static boolean hasValidFileExtension(String string) {
return allowedFileExtension.parallelStream().anyMatch(string::endsWith);
}

public static boolean isPossibleBadFilename(String name) {
public static boolean hasPossibleBadFilenamePrefix(String name) {
return filenamePrefixBlacklist.parallelStream().anyMatch(name::startsWith);
}

public static boolean hasInvalidFilenameCharacters(String name) {
return invalidFilenameCharacters.parallelStream().anyMatch(name::contains);
}

public static String getNameFromFilename(String filename) {
int pos = filename.lastIndexOf(".");
if (pos > 0) {
Expand Down Expand Up @@ -192,9 +204,10 @@ public static File[] getFilesAllowedToUpload(File directory, boolean includeSubd
}

public static File[] getFilesAllowedToUpload(File directory, String ext) {
return directory.listFiles((File dir, String name)
-> name.toLowerCase().endsWith(ext)
);
File[] files = directory.listFiles((File dir, String name) -> name.toLowerCase().endsWith(ext));
Arrays.sort(files);
return files;

}

public static Map<String, Integer> getFilesByExtention(File[] files) {
Expand Down Expand Up @@ -254,22 +267,18 @@ public static String getFileChecksum(MessageDigest digest, File file) throws IOE
}

public static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder buffer = new StringBuilder();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1) {
buffer.append(chars, 0, read);
}
HttpClient client = HttpClient.newHttpClient();

return buffer.toString();
} finally {
if (reader != null) {
reader.close();
}
HttpRequest request = HttpRequest.newBuilder(
URI.create(urlString))
.header("user-agent", Settings.USERAGENT)
.build();

var response = client.send(request, BodyHandlers.ofString());
return response.body();
} catch (Exception e) {
throw new Exception("Error while getting JSON from " + urlString);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pattypan/panes/ChooseColumnsPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private boolean showTemplateFields(String templateName) {
Hyperlink docLink = new Hyperlink(Util.text("choose-columns-template-doc"));
docLink.setMinHeight(25);
docLink.setOnAction(event -> {
Util.openUrl("https://commons.wikimedia.org/wiki/Template:" + template.name + "/doc");
Util.openUrl("https://commons.wikimedia.org/wiki/Template:" + template.name.replace(" ", "_") + "/doc");
});

templateDescContainer.getChildren().clear();
Expand Down
10 changes: 7 additions & 3 deletions src/pattypan/panes/LoadPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void addFilesToUpload(ArrayList<Map<String, String>> descriptions, Templ
}

// when uploaded from URL the extension is not automatically added
if (!Util.stringHasValidFileExtension(description.get("name"))) {
if (!Util.hasValidFileExtension(description.get("name"))) {
throw new Exception("filename does not include a valid file extension");
}
} else {
Expand All @@ -172,8 +172,12 @@ private void addFilesToUpload(ArrayList<Map<String, String>> descriptions, Templ
}
}

if (Util.isPossibleBadFilename(description.get("name"))) {
warnings.add(description.get("name") + ": filename shouldn't have name from camera (DSC, DSCF, etc");
if (Util.hasPossibleBadFilenamePrefix(description.get("name"))) {
warnings.add(description.get("name") + ": filename shouldn't have name from camera (DSC, DSCF, etc)");
}

if (Util.hasInvalidFilenameCharacters(description.get("name"))) {
throw new Exception(description.get("name") + ": filename shouldn't contain invalid characters (#, ], {, etc)");
}

Set<String> keys = Util.getKeysByValue(description, "");
Expand Down
12 changes: 7 additions & 5 deletions src/pattypan/panes/StartPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
*/
package pattypan.panes;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.net.UnknownHostException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import javafx.geometry.Pos;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.GridPane;
Expand Down Expand Up @@ -158,7 +160,7 @@ public ArrayList<String> getVersions() throws Exception {
ArrayList<String> versions = new ArrayList<>();
String json = Util.readUrl("https://api.github.com/repos/yarl/pattypan/releases");

JsonArray releases = new JsonParser().parse(json).getAsJsonArray();
JsonArray releases = JsonParser.parseString(json).getAsJsonArray();
for (JsonElement element : releases) {
JsonObject release = element.getAsJsonObject();
boolean draft = release.get("draft").getAsBoolean();
Expand Down

0 comments on commit ae10043

Please sign in to comment.