content = readLines(reader, false);
}
// Insert
byte[] data = content.getBytes(ENCODING);
InsertBlock block = new InsertBlock(new ArrayBucket(data), null, FreenetURI.EMPTY_CHK_URI);
FreenetURI uri;
try {
uri = client.insert(block, getCHKOnly, null);
} catch (InsertException e) {
outsb.append("Error: ").append(e.getMessage());
if(e.uri != null)
outsb.append("URI would have been: ").append(e.uri);
InsertExceptionMode mode = e.getMode();
if((mode == InsertExceptionMode.FATAL_ERRORS_IN_BLOCKS) || (mode == InsertExceptionMode.TOO_MANY_RETRIES_IN_BLOCKS)) {
outsb.append("Splitfile-specific error:\n").append(e.errorCodes.toVerboseString());
}
outsb.append("\r\n");
w.write(outsb.toString());
w.flush();
return false;
}
outsb.append("URI: ").append(uri);
////////////////////////////////////////////////////////////////////////////////
} else if(uline.startsWith("PUTDIR:") || (uline.startsWith("PUTSSKDIR")) || (getCHKOnly = uline.startsWith("GETCHKDIR:"))) {
// TODO: Check for errors?
boolean ssk = false;
if(uline.startsWith("PUTDIR:"))
line = line.substring("PUTDIR:".length());
else if(uline.startsWith("PUTSSKDIR:")) {
line = line.substring("PUTSSKDIR:".length());
ssk = true;
} else if(uline.startsWith("GETCHKDIR:"))
line = line.substring(("GETCHKDIR:").length());
else {
System.err.println("Impossible");
outsb.append("Impossible");
}
line = line.trim();
if(line.length() < 1) {
printHeader(w);
outsb.append("\r\n");
w.write(outsb.toString());
w.flush();
return false;
}
String defaultFile = null;
FreenetURI insertURI = FreenetURI.EMPTY_CHK_URI;
// set default file?
if (line.matches("^.*#.*$")) {
String[] split = line.split("#");
if(ssk) {
insertURI = new FreenetURI(split[0]);
line = split[1];
if(split.length > 2)
defaultFile = split[2];
} else {
defaultFile = split[1];
line = split[0];
}
}
HashMap<String, Object> bucketsByName =
makeBucketsByName(line);
if(defaultFile == null) {
String[] defaultFiles =
new String[] { "index.html", "index.htm", "default.html", "default.htm" };
for(String file: defaultFiles) {
if(bucketsByName.containsKey(file)) {
defaultFile = file;
break;
}
}
}
FreenetURI uri;
try {
uri = client.insertManifest(insertURI, bucketsByName, defaultFile);
uri = uri.addMetaStrings(new String[] { "" });
outsb.append("=======================================================");
outsb.append("URI: ").append(uri);
outsb.append("=======================================================");
} catch (InsertException e) {
outsb.append("Finished insert but: ").append(e.getMessage());
if(e.uri != null) {
uri = e.uri;
uri = uri.addMetaStrings(new String[] { "" });
outsb.append("URI would have been: ").append(uri);
}
if(e.errorCodes != null) {
outsb.append("Splitfile errors breakdown:");
outsb.append(e.errorCodes.toVerboseString());
}
Logger.error(this, "Caught "+e, e);
}
} else if(uline.startsWith("PUTFILE:") || (getCHKOnly = uline.startsWith("GETCHKFILE:"))) {
// Just insert to local store
if(getCHKOnly) {
line = line.substring(("GETCHKFILE:").length()).trim();
} else {
line = line.substring("PUTFILE:".length()).trim();
}
String mimeType = DefaultMIMETypes.guessMIMEType(line, false);
if (line.indexOf('#') > -1) {
String[] splittedLine = line.split("#");
line = splittedLine[0];
mimeType = splittedLine[1];
}
File f = new File(line);
outsb.append("Attempting to read file ").append(line);
long startTime = System.currentTimeMillis();
try {
if(!(f.exists() && f.canRead())) {
throw new FileNotFoundException();
}
// Guess MIME type
outsb.append(" using MIME type: ").append(mimeType).append("\r\n");
if(mimeType.equals(DefaultMIMETypes.DEFAULT_MIME_TYPE))
mimeType = ""; // don't need to override it
FileBucket fb = new FileBucket(f, true, false, false, false);
InsertBlock block = new InsertBlock(fb, new ClientMetadata(mimeType), FreenetURI.EMPTY_CHK_URI);
startTime = System.currentTimeMillis();
FreenetURI uri = client.insert(block, getCHKOnly, f.getName());
// FIXME depends on CHK's still being renamable