X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fapplication%2FProject.java;h=04b3143e45e2c4045c51cc8ad2e7308eb6afb1f8;hb=8db42d2121e8ee465ab8380a66febde1949a0106;hp=fa0b774dccac4f49922d285a46b27277287e0f84;hpb=0e88169c3e8decfcd99f39f5ecf3a85df50c3fca;p=jSite.git diff --git a/src/main/java/de/todesbaum/jsite/application/Project.java b/src/main/java/de/todesbaum/jsite/application/Project.java index fa0b774..04b3143 100644 --- a/src/main/java/de/todesbaum/jsite/application/Project.java +++ b/src/main/java/de/todesbaum/jsite/application/Project.java @@ -1,5 +1,5 @@ /* - * jSite - Project.java - Copyright © 2006–2012 David Roden + * jSite - Project.java - Copyright © 2006–2019 David Roden * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software @@ -19,12 +19,14 @@ package de.todesbaum.jsite.application; import java.io.File; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; -import de.todesbaum.util.mime.DefaultMIMETypes; +import net.pterodactylus.util.io.MimeTypes; /** * Container for project information. @@ -60,6 +62,9 @@ public class Project implements Comparable { /** The edition to insert to. */ protected int edition; + /** Whether to always force inserts. */ + private boolean alwaysForceInserts; + /** Whether to ignore hidden directory. */ private boolean ignoreHiddenFiles; @@ -89,8 +94,11 @@ public class Project implements Comparable { localPath = project.localPath; indexFile = project.indexFile; lastInsertionTime = project.lastInsertionTime; + alwaysForceInserts = project.alwaysForceInserts; ignoreHiddenFiles = project.ignoreHiddenFiles; - fileOptions = new HashMap(project.fileOptions); + for (Entry fileOption : fileOptions.entrySet()) { + fileOptions.put(fileOption.getKey(), new FileOption(fileOption.getValue())); + } } /** @@ -252,6 +260,29 @@ public class Project implements Comparable { } /** + * Returns whether files for this project should always be inserted, even + * when unchanged. + * + * @return {@code true} to always force inserts on this project, + * {@code false} otherwise + */ + public boolean isAlwaysForceInsert() { + return alwaysForceInserts; + } + + /** + * Sets whether files for this project should always be inserted, even when + * unchanged. + * + * @param alwaysForceInsert + * {@code true} to always force inserts on this project, + * {@code false} otherwise + */ + public void setAlwaysForceInsert(boolean alwaysForceInsert) { + this.alwaysForceInserts = alwaysForceInsert; + } + + /** * Returns whether hidden files are ignored, i.e. not inserted. * * @return {@code true} if hidden files are not inserted, {@code false} @@ -289,7 +320,7 @@ public class Project implements Comparable { * The URI to shorten * @return The shortened URI */ - private String shortenURI(String uri) { + private static String shortenURI(String uri) { String shortUri = uri; if (shortUri.startsWith("freenet:")) { shortUri = shortUri.substring("freenet:".length()); @@ -336,13 +367,33 @@ public class Project implements Comparable { */ public FileOption getFileOption(String filename) { FileOption fileOption = fileOptions.get(filename); + String defaultMimeType = "application/octet-stream"; if (fileOption == null) { - fileOption = new FileOption(DefaultMIMETypes.guessMIMEType(filename)); - fileOptions.put(filename, fileOption); + List suffixes = getSuffixes(filename); + for (String suffix : suffixes) { + String mimeType = MimeTypes.getMimeType(suffix); + if (!mimeType.equals(defaultMimeType)) { + defaultMimeType = mimeType; + break; + } + } + fileOption = new FileOption(defaultMimeType); } + fileOptions.put(filename, fileOption); return fileOption; } + private List getSuffixes(String filename) { + List suffixes = new ArrayList<>(); + int dot = filename.lastIndexOf("."); + while (dot > -1) { + String suffix = filename.substring(dot + 1); + suffixes.add(0, suffix); + dot = filename.lastIndexOf(".", dot - 1); + } + return suffixes; + } + /** * Sets options for a file. * @@ -386,6 +437,7 @@ public class Project implements Comparable { *

* Projects are compared by their name only. */ + @Override public int compareTo(Project project) { return name.compareToIgnoreCase(project.name); } @@ -432,7 +484,7 @@ public class Project implements Comparable { if ((fileOption.getCurrentHash() != null) && (fileOption.getCurrentHash().length() > 0) && (!fileOption.getCurrentHash().equals(fileOption.getLastInsertHash()) || fileOption.isForceInsert())) { fileOption.setLastInsertEdition(edition); fileOption.setLastInsertHash(fileOption.getCurrentHash()); - fileOption.setLastInsertFilename(fileOption.hasChangedName() ? fileOption.getChangedName() : fileOptionEntry.getKey()); + fileOption.setLastInsertFilename(fileOption.getChangedName().orElse(fileOptionEntry.getKey())); } fileOption.setForceInsert(false); }