X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fapplication%2FProjectInserter.java;h=3a68ea03a188112ebcff74056c423c032ec42b08;hb=91dcc42ab8695f77c853f6d84aa96ba6a53b1cfb;hp=a9ea8d1c0d693bb59cc91b4b0a05c92fb7049b02;hpb=fbfe4707a1e3e9f23c8e1904d1a0eb0c3fbf587f;p=jSite.git diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java index a9ea8d1..3a68ea0 100644 --- a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java @@ -20,6 +20,7 @@ package de.todesbaum.jsite.application; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -35,6 +36,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.util.io.StreamCopier.ProgressListener; + +import com.google.common.base.Optional; import de.todesbaum.jsite.gui.FileScanner; import de.todesbaum.jsite.gui.FileScanner.ScannedFile; import de.todesbaum.jsite.gui.FileScannerListener; @@ -69,16 +72,16 @@ public class ProjectInserter implements FileScannerListener, Runnable { private List insertListeners = new ArrayList(); /** The freenet interface. */ - protected Freenet7Interface freenetInterface; + private Freenet7Interface freenetInterface; /** The project to insert. */ - protected Project project; + private Project project; /** The file scanner. */ private FileScanner fileScanner; /** Object used for synchronization. */ - protected final Object lockObject = new Object(); + private final Object lockObject = new Object(); /** The temp directory. */ private String tempDirectory; @@ -284,30 +287,6 @@ public class ProjectInserter implements FileScannerListener, Runnable { } /** - * Creates an input stream that delivers the given file, replacing edition - * tokens in the file’s content, if necessary. - * - * @param filename - * The name of the file - * @param fileOption - * The file options - * @param edition - * The current edition - * @param length - * An array containing a single long which is used to - * return the final length of the file, after all - * replacements - * @return The input stream for the file - * @throws IOException - * if an I/O error occurs - */ - private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException { - File file = new File(project.getLocalPath(), filename); - length[0] = file.length(); - return new FileInputStream(file); - } - - /** * Creates a file entry suitable for handing in to * {@link ClientPutComplexDir#addFileEntry(FileEntry)}. * @@ -318,30 +297,33 @@ public class ProjectInserter implements FileScannerListener, Runnable { * @return A file entry for the given file */ private FileEntry createFileEntry(ScannedFile file, int edition) { - FileEntry fileEntry = null; String filename = file.getFilename(); FileOption fileOption = project.getFileOption(filename); if (fileOption.isInsert()) { fileOption.setCurrentHash(file.getHash()); /* check if file was modified. */ - if (!fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) { + if (!project.isAlwaysForceInsert() && !fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) { /* only insert a redirect. */ logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename)); - return new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()); + return new RedirectFileEntry(fileOption.getChangedName().or(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()); } try { - long[] fileLength = new long[1]; - InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength); - fileEntry = new DirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileEntryInputStream, fileLength[0]); + return createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType()); } catch (IOException ioe1) { /* ignore, null is returned. */ } } else { if (fileOption.isInsertRedirect()) { - fileEntry = new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileOption.getCustomKey()); + return new RedirectFileEntry(fileOption.getChangedName().or(filename), fileOption.getMimeType(), fileOption.getCustomKey()); } } - return fileEntry; + return null; + } + + private FileEntry createFileEntry(String filename, Optional changedName, String mimeType) throws FileNotFoundException { + File physicalFile = new File(project.getLocalPath(), filename); + InputStream fileEntryInputStream = new FileInputStream(physicalFile); + return new DirectFileEntry(changedName.or(filename), mimeType, fileEntryInputStream, physicalFile.length()); } /** @@ -399,10 +381,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { logger.log(Level.FINEST, "Ignoring {0}.", fileOptionEntry.getKey()); continue; } - String fileName = fileOptionEntry.getKey(); - if (fileOption.hasChangedName()) { - fileName = fileOption.getChangedName(); - } + String fileName = fileOption.getChangedName().or(fileOptionEntry.getKey()); logger.log(Level.FINEST, "Adding “{0}” for {1}.", new Object[] { fileName, fileOptionEntry.getKey() }); if (!fileNames.add(fileName)) { checkReport.addIssue("error.duplicate-file", true, fileName); @@ -443,6 +422,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** * {@inheritDoc} */ + @Override public void run() { fireProjectInsertStarted(); List files = fileScanner.getFiles(); @@ -548,6 +528,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** * {@inheritDoc} */ + @Override public void fileScannerFinished(FileScanner fileScanner) { if (!fileScanner.isError()) { new Thread(this).start(); @@ -597,6 +578,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** * {@inheritDoc} */ + @Override public Iterator iterator() { return issues.iterator(); }