import de.todesbaum.util.freenet.fcp2.Message;
import de.todesbaum.util.freenet.fcp2.RedirectFileEntry;
import de.todesbaum.util.freenet.fcp2.Verbosity;
+import de.todesbaum.util.io.Closer;
import de.todesbaum.util.io.ReplacingOutputStream;
import de.todesbaum.util.io.StreamCopier;
/**
- * @author David Roden <droden@gmail.com>
- * @version $Id$
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
public class ProjectInserter implements FileScannerListener, Runnable {
protected Project project;
private FileScanner fileScanner;
protected final Object lockObject = new Object();
- private int maxRetries = 99999;
public void addInsertListener(InsertListener insertListener) {
insertListeners.add(insertListener);
this.freenetInterface = freenetInterface;
}
- /**
- * @param maxRetries
- * The maxRetries to set.
- */
- public void setMaxRetries(int maxRetries) {
- this.maxRetries = maxRetries;
- }
-
public void start() {
fileScanner = new FileScanner(project);
fileScanner.addFileScannerListener(this);
ByteArrayOutputStream filteredByteOutputStream = new ByteArrayOutputStream(Math.min(Integer.MAX_VALUE, (int) length[0]));
ReplacingOutputStream outputStream = new ReplacingOutputStream(filteredByteOutputStream);
FileInputStream fileInput = new FileInputStream(file);
- outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
- outputStream.addReplacement("$[URI]", project.getFinalRequestURI(0));
- for (int index = 1; index <= fileOption.getEditionRange(); index++) {
- outputStream.addReplacement("$[URI+" + index + "]", project.getFinalRequestURI(index));
- outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
+ try {
+ outputStream.addReplacement("$[EDITION]", String.valueOf(edition));
+ outputStream.addReplacement("$[URI]", project.getFinalRequestURI(0));
+ for (int index = 1; index <= fileOption.getEditionRange(); index++) {
+ outputStream.addReplacement("$[URI+" + index + "]", project.getFinalRequestURI(index));
+ outputStream.addReplacement("$[EDITION+" + index + "]", String.valueOf(edition + index));
+ }
+ StreamCopier.copy(fileInput, outputStream, length[0]);
+ } finally {
+ Closer.close(fileInput);
+ Closer.close(outputStream);
+ Closer.close(filteredByteOutputStream);
}
- StreamCopier.copy(fileInput, outputStream, length[0]);
- outputStream.close();
- filteredByteOutputStream.close();
byte[] filteredBytes = filteredByteOutputStream.toByteArray();
length[0] = filteredBytes.length;
return new ByteArrayInputStream(filteredBytes);
tempFile.deleteOnExit();
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
- for (String filename: containerFiles.get(containerName)) {
- File dataFile = new File(project.getLocalPath(), filename);
- if (dataFile.exists()) {
- ZipEntry zipEntry = new ZipEntry(filename);
- long[] fileLength = new long[1];
- InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
- zipOutputStream.putNextEntry(zipEntry);
- StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
- zipOutputStream.closeEntry();
- wrappedInputStream.close();
+ try {
+ for (String filename: containerFiles.get(containerName)) {
+ File dataFile = new File(project.getLocalPath(), filename);
+ if (dataFile.exists()) {
+ ZipEntry zipEntry = new ZipEntry(filename);
+ long[] fileLength = new long[1];
+ InputStream wrappedInputStream = createFileInputStream(filename, project.getFileOption(filename), edition, fileLength);
+ try {
+ zipOutputStream.putNextEntry(zipEntry);
+ StreamCopier.copy(wrappedInputStream, zipOutputStream, fileLength[0]);
+ } finally {
+ zipOutputStream.closeEntry();
+ wrappedInputStream.close();
+ }
+ }
}
+ } finally {
+ zipOutputStream.closeEntry();
+ Closer.close(zipOutputStream);
+ Closer.close(fileOutputStream);
}
- zipOutputStream.closeEntry();
-
- /* FIXME - create metadata */
- // ZipEntry metadataEntry = new ZipEntry("metadata");
- // zipOutputStream.putNextEntry(metadataEntry);
- // Metadata zipMetadata = new Metadata();
- // for (String filename: containerFiles.get(containerName)) {
- // if (new File(project.getLocalPath(), filename).exists()) {
- // DocumentMetadata zipEntryMetadata = new DocumentMetadata();
- // zipEntryMetadata.setName(filename);
- // zipEntryMetadata.setFormat(project.getFileOption(filename).getMimeType());
- // zipMetadata.addDocument(zipEntryMetadata);
- // }
- // }
- // zipOutputStream.write(zipMetadata.toByteArray());
- // zipOutputStream.closeEntry();
- zipOutputStream.close();
containerLength[0] = tempFile.length();
return new FileInputStream(tempFile);
createContainers(files, containers, containerFiles);
/* collect files */
- int edition = ((EditionProject) project).getEdition();
- String dirURI = "freenet:USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
+ int edition = project.getEdition();
+ String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
putDir.setDefaultName(project.getIndexFile());
putDir.setVerbosity(Verbosity.ALL);
- putDir.setMaxRetries(maxRetries);
+ putDir.setMaxRetries(-1);
for (String filename: files) {
FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
if (fileEntry != null) {
}
/* parse progress and success messages */
+ String finalURI = null;
boolean success = false;
boolean finished = false;
boolean disconnected = false;
if (!finished) {
String messageName = message.getName();
if ("URIGenerated".equals(messageName)) {
- fireProjectURIGenerated(message.get("URI"));
+ finalURI = message.get("URI");
+ fireProjectURIGenerated(finalURI);
}
if ("SimpleProgress".equals(messageName)) {
int total = Integer.parseInt(message.get("Total"));
fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
}
success = "PutSuccessful".equals(messageName);
- finished = success || "PutFailed".equals(messageName);
+ finished = success || "PutFailed".equals(messageName) || messageName.endsWith("Error");
}
}
/* post-insert work */
fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
if (success) {
- if (project instanceof EditionProject) {
- ((EditionProject) project).setEdition(edition + 1);
- }
+ String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
+ int newEdition = Integer.parseInt(editionPart);
+ project.setEdition(newEdition);
}
}