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=30d4d7a3088d453079147b1d14ac475edd99017d;hpb=b084107c9af548804c526711b4e49e1b65c82f7a;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 30d4d7a..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,7 +297,6 @@ 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()) { @@ -327,21 +305,25 @@ public class ProjectInserter implements FileScannerListener, Runnable { 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);