X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fde%2Ftodesbaum%2Fjsite%2Fapplication%2FProjectInserter.java;h=a3fb0da755badf79b474008b5570e056bfe18979;hb=593eba180ca6538a680810651f127fbcf0a620a3;hp=db4d514c98c640bd7a1a5d7c01d8b6fa03c8eca7;hpb=d087e3fdc2e752bc073dee0dcaf5c970c3f1bc7d;p=jSite.git diff --git a/src/de/todesbaum/jsite/application/ProjectInserter.java b/src/de/todesbaum/jsite/application/ProjectInserter.java index db4d514..a3fb0da 100644 --- a/src/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/de/todesbaum/jsite/application/ProjectInserter.java @@ -1,5 +1,5 @@ /* - * jSite - ProjectInserter.java - Copyright © 2006–2011 David Roden + * jSite - ProjectInserter.java - Copyright © 2006–2012 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 @@ -28,12 +28,14 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.CountDownLatch; import java.util.logging.Level; import java.util.logging.Logger; import de.todesbaum.jsite.gui.FileScanner; +import de.todesbaum.jsite.gui.FileScanner.ScannedFile; import de.todesbaum.jsite.gui.FileScannerListener; import de.todesbaum.util.freenet.fcp2.Client; import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir; @@ -42,6 +44,7 @@ import de.todesbaum.util.freenet.fcp2.Connection; import de.todesbaum.util.freenet.fcp2.DirectFileEntry; import de.todesbaum.util.freenet.fcp2.FileEntry; import de.todesbaum.util.freenet.fcp2.Message; +import de.todesbaum.util.freenet.fcp2.PriorityClass; import de.todesbaum.util.freenet.fcp2.RedirectFileEntry; import de.todesbaum.util.freenet.fcp2.Verbosity; import de.todesbaum.util.io.StreamCopier.ProgressListener; @@ -89,6 +92,15 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** Progress listener for payload transfers. */ private ProgressListener progressListener; + /** Whether to use “early encode.” */ + private boolean useEarlyEncode; + + /** The insert priority. */ + private PriorityClass priority; + + /** The manifest putter. */ + private ManifestPutter manifestPutter; + /** * Adds a listener to the list of registered listeners. * @@ -215,6 +227,37 @@ public class ProjectInserter implements FileScannerListener, Runnable { } /** + * Sets whether to use the “early encode“ flag for the insert. + * + * @param useEarlyEncode + * {@code true} to set the “early encode” flag for the insert, + * {@code false} otherwise + */ + public void setUseEarlyEncode(boolean useEarlyEncode) { + this.useEarlyEncode = useEarlyEncode; + } + + /** + * Sets the insert priority. + * + * @param priority + * The insert priority + */ + public void setPriority(PriorityClass priority) { + this.priority = priority; + } + + /** + * Sets the manifest putter to use for inserts. + * + * @param manifestPutter + * The manifest putter to use + */ + public void setManifestPutter(ManifestPutter manifestPutter) { + this.manifestPutter = manifestPutter; + } + + /** * Starts the insert. * * @param progressListener @@ -268,37 +311,34 @@ public class ProjectInserter implements FileScannerListener, Runnable { * Creates a file entry suitable for handing in to * {@link ClientPutComplexDir#addFileEntry(FileEntry)}. * - * @param filename - * The name of the file to insert + * @param file + * The name and hash of the file to insert * @param edition * The current edition - * @param containerFiles - * The container definitions * @return A file entry for the given file */ - private FileEntry createFileEntry(String filename, int edition, Map> containerFiles) { + 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())) { + /* 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()); + } try { - long[] containerLength = new long[1]; - InputStream containerInputStream = createContainerInputStream(containerFiles, containerName, edition, containerLength); - fileEntry = new DirectFileEntry(containerName + ".zip", "application/zip", containerInputStream, containerLength[0]); + 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]); } catch (IOException ioe1) { /* ignore, null is returned. */ } } else { - if (fileOption.isInsert()) { - try { - long[] fileLength = new long[1]; - InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength); - fileEntry = new DirectFileEntry(filename, project.getFileOption(filename).getMimeType(), fileEntryInputStream, fileLength[0]); - } catch (IOException ioe1) { - /* ignore, null is returned. */ - } - } else { - if (fileOption.isInsertRedirect()) { - fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey()); - } + if (fileOption.isInsertRedirect()) { + fileEntry = new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileOption.getCustomKey()); } } return fileEntry; @@ -368,15 +408,45 @@ public class ProjectInserter implements FileScannerListener, Runnable { checkReport.addIssue("error.duplicate-file", true, fileName); } } + long totalSize = 0; + FileScanner fileScanner = new FileScanner(project); + final CountDownLatch completionLatch = new CountDownLatch(1); + fileScanner.addFileScannerListener(new FileScannerListener() { + + @Override + public void fileScannerFinished(FileScanner fileScanner) { + completionLatch.countDown(); + } + }); + new Thread(fileScanner).start(); + while (completionLatch.getCount() > 0) { + try { + completionLatch.await(); + } catch (InterruptedException ie1) { + /* TODO: logging */ + } + } + for (ScannedFile scannedFile : fileScanner.getFiles()) { + String fileName = scannedFile.getFilename(); + FileOption fileOption = project.getFileOption(fileName); + if ((fileOption != null) && !fileOption.isInsert()) { + continue; + } + totalSize += new File(project.getLocalPath(), fileName).length(); + } + if (totalSize > 2 * 1024 * 1024) { + checkReport.addIssue("warning.site-larger-than-2-mib", false); + } return checkReport; } /** * {@inheritDoc} */ + @Override public void run() { fireProjectInsertStarted(); - List files = fileScanner.getFiles(); + List files = fileScanner.getFiles(); /* create connection to node */ synchronized (lockObject) { @@ -407,10 +477,11 @@ public class ProjectInserter implements FileScannerListener, Runnable { } putDir.setVerbosity(Verbosity.ALL); putDir.setMaxRetries(-1); - putDir.setEarlyEncode(false); - putDir.setManifestPutter(ManifestPutter.DEFAULT); - for (String filename : files) { - FileEntry fileEntry = createFileEntry(filename, edition, containerFiles); + putDir.setEarlyEncode(useEarlyEncode); + putDir.setPriorityClass(priority); + putDir.setManifestPutter(manifestPutter); + for (ScannedFile file : files) { + FileEntry fileEntry = createFileEntry(file, edition); if (fileEntry != null) { try { putDir.addFileEntry(fileEntry); @@ -466,6 +537,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { int newEdition = Integer.parseInt(editionPart); project.setEdition(newEdition); project.setLastInsertionTime(System.currentTimeMillis()); + project.onSuccessfulInsert(); } fireProjectInsertFinished(success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null)); } @@ -477,6 +549,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** * {@inheritDoc} */ + @Override public void fileScannerFinished(FileScanner fileScanner) { if (!fileScanner.isError()) { new Thread(this).start(); @@ -526,6 +599,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { /** * {@inheritDoc} */ + @Override public Iterator iterator() { return issues.iterator(); }