Author: bombe
Date: 2006-08-06 16:40:33 +0000 (Sun, 06 Aug 2006)
New Revision: 9942

Added:
   trunk/freenet/src/freenet/clients/http/LocalFileInsertToadlet.java
   trunk/freenet/src/freenet/support/HTMLNode.java
Modified:
   trunk/freenet/src/freenet/clients/http/FProxyToadlet.java
   trunk/freenet/src/freenet/clients/http/QueueToadlet.java
Log:
add toadlet to insert local files from disk

Modified: trunk/freenet/src/freenet/clients/http/FProxyToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/FProxyToadlet.java   2006-08-06 
16:02:08 UTC (rev 9941)
+++ trunk/freenet/src/freenet/clients/http/FProxyToadlet.java   2006-08-06 
16:40:33 UTC (rev 9942)
@@ -364,6 +364,9 @@

                        QueueToadlet queueToadlet = new QueueToadlet(node, 
node.getFCPServer(), client);
                        server.register(queueToadlet, "/queue/", true);
+                       
+                       LocalFileInsertToadlet localFileInsertToadlet = new 
LocalFileInsertToadlet(node, client);
+                       server.register(localFileInsertToadlet, "/files/", 
true);

                        // Now start the server.
                        server.start();

Added: trunk/freenet/src/freenet/clients/http/LocalFileInsertToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/LocalFileInsertToadlet.java  
2006-08-06 16:02:08 UTC (rev 9941)
+++ trunk/freenet/src/freenet/clients/http/LocalFileInsertToadlet.java  
2006-08-06 16:40:33 UTC (rev 9942)
@@ -0,0 +1,150 @@
+package freenet.clients.http;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Comparator;
+
+import freenet.client.HighLevelSimpleClient;
+import freenet.node.Node;
+import freenet.support.HTMLEncoder;
+import freenet.support.HTMLNode;
+import freenet.support.URLEncoder;
+
+/**
+ * @author David 'Bombe' Roden <bombe at freenetproject.org>
+ * @version $Id$
+ */
+public class LocalFileInsertToadlet extends Toadlet {
+
+       private Node node;
+
+       private File currentPath;
+
+       /**
+        * 
+        */
+       public LocalFileInsertToadlet(Node node, HighLevelSimpleClient 
highLevelSimpleClient) {
+               super(highLevelSimpleClient);
+               this.node = node;
+       }
+
+       /**
+        * @see freenet.clients.http.Toadlet#handleGet(java.net.URI,
+        *      freenet.clients.http.ToadletContext)
+        */
+       public void handleGet(URI uri, ToadletContext toadletContext) throws 
ToadletContextClosedException, IOException, RedirectException {
+               HTTPRequest request = new HTTPRequest(uri, null, 
toadletContext);
+
+               String path = request.getParam("path");
+               if (path.length() == 0) {
+                       if (currentPath == null) {
+                               currentPath = new 
File(System.getProperty("user.dir"));
+                       }
+                       writePermanentRedirect(toadletContext, "Found", 
"?path=" + URLEncoder.encode(currentPath.getAbsolutePath()));
+                       return;
+               }
+
+               currentPath = new File(path).getCanonicalFile();
+
+               StringBuffer pageBuffer = new StringBuffer(16384);
+               PageMaker pageMaker = toadletContext.getPageMaker();
+
+               pageMaker.makeHead(pageBuffer, "Listing of " + 
HTMLEncoder.encode(currentPath.getAbsolutePath()));
+               node.alerts.toSummaryHtml(pageBuffer);
+
+               HTMLNode pageDiv = new HTMLNode("div", "class", "page");
+               HTMLNode infoboxDiv = pageDiv.addChild("div", "class", 
"infobox");
+               infoboxDiv.addChild("div", "class", "infobox-header", 
"Directory Listing: " + currentPath.getAbsolutePath());
+               HTMLNode listingDiv = infoboxDiv.addChild("div", "class", 
"infobox-content");
+
+               if (currentPath.exists() && currentPath.isDirectory() && 
currentPath.canRead()) {
+                       File[] files = currentPath.listFiles();
+                       Arrays.sort(files, new Comparator() {
+
+                               public int compare(Object first, Object second) 
{
+                                       File firstFile = (File) first;
+                                       File secondFile = (File) second;
+                                       if (firstFile.isDirectory() && 
!secondFile.isDirectory()) {
+                                               return -1;
+                                       }
+                                       if (!firstFile.isDirectory() && 
secondFile.isDirectory()) {
+                                               return 1;
+                                       }
+                                       return 
firstFile.getName().compareToIgnoreCase(secondFile.getName());
+                               }
+                       });
+                       HTMLNode listingTable = listingDiv.addChild("table");
+                       HTMLNode headerRow = listingTable.addChild("tr");
+                       headerRow.addChild("th");
+                       headerRow.addChild("th", "File");
+                       headerRow.addChild("th", "Size");
+                       /* add filesystem roots (fsck windows) */
+                       File[] roots = File.listRoots();
+                       for (int rootIndex = 0, rootCount = roots.length; 
rootIndex < rootCount; rootIndex++) {
+                               File currentRoot = roots[rootIndex];
+                               HTMLNode rootRow = listingTable.addChild("tr");
+                               rootRow.addChild("td");
+                               HTMLNode rootLinkCellNode = 
rootRow.addChild("td");
+                               rootLinkCellNode.addChild("a", "href", "?path=" 
+ URLEncoder.encode(currentRoot.getCanonicalPath()), currentRoot.getName() + 
File.separator);
+                               rootRow.addChild("td");
+                       }
+                       /* add back link */
+                       if (currentPath.getParent() != null) {
+                               HTMLNode backlinkRow = 
listingTable.addChild("tr");
+                               backlinkRow.addChild("td");
+                               HTMLNode backlinkCellNode = 
backlinkRow.addChild("td");
+                               backlinkCellNode.addChild("a", "href", "?path=" 
+ URLEncoder.encode(currentPath.getParent()), "..");
+                               backlinkRow.addChild("td");
+                       }
+                       for (int fileIndex = 0, fileCount = files.length; 
fileIndex < fileCount; fileIndex++) {
+                               File currentFile = files[fileIndex];
+                               HTMLNode fileRow = listingTable.addChild("tr");
+                               if (currentFile.isDirectory()) {
+                                       fileRow.addChild("td");
+                                       if (currentFile.canRead()) {
+                                               HTMLNode directoryCellNode = 
fileRow.addChild("td");
+                                               directoryCellNode.addChild("a", 
"href", "?path=" + URLEncoder.encode(currentFile.getAbsolutePath()), 
currentFile.getName());
+                                       } else {
+                                               fileRow.addChild("td", "class", 
"unreadable-file", currentFile.getName());
+                                       }
+                                       fileRow.addChild("td");
+                               } else {
+                                       if (currentFile.canRead()) {
+                                               HTMLNode cellNode = 
fileRow.addChild("td");
+                                               HTMLNode formNode = 
cellNode.addChild("form", new String[] { "action", "method", "accept-charset" 
}, new String[] { "/queue/", "post", "utf-8" });
+                                               formNode.addChild("input", new 
String[] { "type", "name", "value" }, new String[] { "hidden", "formPassword", 
node.formPassword });
+                                               formNode.addChild("input", new 
String[] { "type", "name", "value" }, new String[] { "hidden", "filename", 
currentFile.getAbsolutePath() });
+                                               formNode.addChild("input", new 
String[] { "type", "name", "value" }, new String[] { "submit", "insert-local", 
"Insert" });
+                                               fileRow.addChild("td", 
currentFile.getName());
+                                               fileRow.addChild("td", "class", 
"right-align", String.valueOf(currentFile.length()));
+                                       } else {
+                                               fileRow.addChild("td");
+                                               fileRow.addChild("td", "class", 
"unreadable-file", currentFile.getName());
+                                               fileRow.addChild("td", "class", 
"right-align", String.valueOf(currentFile.length()));
+                                       }
+                               }
+                       }
+               } else {
+                       listingDiv.addChild("#", "The directory \u201c" + 
currentPath.getAbsolutePath() + "\u201d can not be read.");
+                       HTMLNode ulNode = listingDiv.addChild("ul");
+                       ulNode.addChild("li", "Check that the specified path 
does exist.");
+                       ulNode.addChild("li", "Check that the specified path is 
a directory.");
+                       ulNode.addChild("li", "Check that the specified path is 
readable by the user running the node.");
+               }
+
+               pageDiv.generate(pageBuffer);
+               pageMaker.makeTail(pageBuffer);
+
+               writeReply(toadletContext, 200, "text/html; charset=utf-8", 
"OK", pageBuffer.toString());
+       }
+
+       /**
+        * @see freenet.clients.http.Toadlet#supportedMethods()
+        */
+       public String supportedMethods() {
+               return "GET,POST";
+       }
+
+}


Property changes on: 
trunk/freenet/src/freenet/clients/http/LocalFileInsertToadlet.java
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: trunk/freenet/src/freenet/clients/http/QueueToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/QueueToadlet.java    2006-08-06 
16:02:08 UTC (rev 9941)
+++ trunk/freenet/src/freenet/clients/http/QueueToadlet.java    2006-08-06 
16:40:33 UTC (rev 9942)
@@ -10,6 +10,7 @@
 import java.util.Iterator;
 import java.util.LinkedList;

+import freenet.client.DefaultMIMETypes;
 import freenet.client.HighLevelSimpleClient;
 import freenet.keys.FreenetURI;
 import freenet.node.Node;
@@ -30,6 +31,7 @@
 import freenet.support.URLEncoder;
 import freenet.support.io.Bucket;
 import freenet.support.io.BucketTools;
+import freenet.support.io.FileBucket;

 public class QueueToadlet extends Toadlet {

@@ -159,6 +161,24 @@
                                }
                                writePermanentRedirect(ctx, "Done", "/queue/");
                                return;
+                       } else if (request.isParameterSet("insert-local")) {
+                               String filename = request.getParam("filename");
+                               try {
+                                       filename = new 
String(filename.getBytes("ISO-8859-1"), "UTF-8");
+                               } catch (Throwable t) {
+                               }
+                               File file = new File(filename);
+                               String identifier = file.getName() + "-fred-" + 
System.currentTimeMillis();
+                               String contentType = 
DefaultMIMETypes.guessMIMEType(filename);
+                               try {
+                                       ClientPut clientPut = new 
ClientPut(fcp.getGlobalClient(), new FreenetURI("CHK@"), identifier, 
Integer.MAX_VALUE, RequestStarter.BULK_SPLITFILE_PRIORITY_CLASS, 
ClientRequest.PERSIST_FOREVER, null, false, false, -1, 
ClientPutMessage.UPLOAD_FROM_DISK, file, contentType, new FileBucket(file, 
true, false, false, false), null);
+                                       clientPut.start();
+                                       fcp.forceStorePersistentRequests();
+                               } catch (IdentifierCollisionException e) {
+                                       e.printStackTrace();
+                               }
+                               writePermanentRedirect(ctx, "Done", "/queue/");
+                               return;
                        }
                } finally {
                        request.freeParts();


Property changes on: trunk/freenet/src/freenet/clients/http/QueueToadlet.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: trunk/freenet/src/freenet/support/HTMLNode.java
===================================================================
--- trunk/freenet/src/freenet/support/HTMLNode.java     2006-08-06 16:02:08 UTC 
(rev 9941)
+++ trunk/freenet/src/freenet/support/HTMLNode.java     2006-08-06 16:40:33 UTC 
(rev 9942)
@@ -0,0 +1,153 @@
+package freenet.support;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class HTMLNode {
+
+       private final String name;
+
+       private final String content;
+
+       private final Map attributes = new HashMap();
+
+       private final List children = new ArrayList();
+
+       public HTMLNode(String name) {
+               this(name, null);
+       }
+
+       public HTMLNode(String name, String content) {
+               this(name, (String[]) null, (String[]) null, content);
+       }
+
+       public HTMLNode(String name, String attributeName, String 
attributeValue) {
+               this(name, attributeName, attributeValue, null);
+       }
+
+       public HTMLNode(String name, String attributeName, String 
attributeValue, String content) {
+               this(name, new String[] { attributeName }, new String[] { 
attributeValue }, content);
+       }
+
+       public HTMLNode(String name, String[] attributeNames, String[] 
attributeValues) {
+               this(name, attributeNames, attributeValues, null);
+       }
+
+       public HTMLNode(String name, String[] attributeNames, String[] 
attributeValues, String content) {
+               this.name = name;
+               if ((attributeNames != null) && (attributeValues != null)) {
+                       if (attributeNames.length != attributeValues.length) {
+                               throw new IllegalArgumentException("attribute 
names and values differ");
+                       }
+                       for (int attributeIndex = 0, attributeCount = 
attributeNames.length; attributeIndex < attributeCount; attributeIndex++) {
+                               attributes.put(attributeNames[attributeIndex], 
attributeValues[attributeIndex]);
+                       }
+               }
+               if (content != null) {
+                       if (!name.equals("#")) {
+                               addChild(new HTMLNode("#", content));
+                               this.content = null;
+                       } else {
+                               this.content = content;
+                       }
+               } else {
+                       this.content = null;
+               }
+       }
+
+       /**
+        * @return the name
+        */
+       public String getName() {
+               return name;
+       }
+
+       /**
+        * @return the content
+        */
+       public String getContent() {
+               return content;
+       }
+
+       public void addAttribute(String attributeName) {
+               addAttribute(attributeName, attributeName);
+       }
+
+       public void addAttribute(String attributeName, String attributeValue) {
+               attributes.put(attributeName, attributeValue);
+       }
+
+       public Map getAttributes() {
+               return Collections.unmodifiableMap(attributes);
+       }
+
+       public String getAttribute(String attributeName) {
+               return (String) attributes.get(attributeName);
+       }
+
+       public HTMLNode addChild(HTMLNode childNode) {
+               children.add(childNode);
+               return childNode;
+       }
+
+       public HTMLNode addChild(String nodeName) {
+               return addChild(nodeName, null);
+       }
+
+       public HTMLNode addChild(String nodeName, String content) {
+               return addChild(nodeName, (String[]) null, (String[]) null, 
content);
+       }
+
+       public HTMLNode addChild(String nodeName, String attributeName, String 
attributeValue) {
+               return addChild(nodeName, attributeName, attributeValue, null);
+       }
+
+       public HTMLNode addChild(String nodeName, String attributeName, String 
attributeValue, String content) {
+               return addChild(nodeName, new String[] { attributeName }, new 
String[] { attributeValue }, content);
+       }
+
+       public HTMLNode addChild(String nodeName, String[] attributeNames, 
String[] attributeValues) {
+               return addChild(nodeName, attributeNames, attributeValues, 
null);
+       }
+
+       public HTMLNode addChild(String nodeName, String[] attributeNames, 
String[] attributeValues, String content) {
+               return addChild(new HTMLNode(nodeName, attributeNames, 
attributeValues, content));
+       }
+
+       public String generate() {
+               StringBuffer tagBuffer = new StringBuffer();
+               return generate(tagBuffer).toString();
+       }
+
+       public StringBuffer generate(StringBuffer tagBuffer) {
+               if (name.equals("#")) {
+                       tagBuffer.append(HTMLEncoder.encode(content));
+                       return tagBuffer;
+               }
+               tagBuffer.append("<").append(name);
+               Set attributeSet = attributes.entrySet();
+               for (Iterator attributeIterator = attributeSet.iterator(); 
attributeIterator.hasNext();) {
+                       Map.Entry attributeEntry = (Map.Entry) 
attributeIterator.next();
+                       String attributeName = (String) attributeEntry.getKey();
+                       String attributeValue = (String) 
attributeEntry.getValue();
+                       tagBuffer.append(" 
").append(HTMLEncoder.encode(attributeName)).append("=\"").append(HTMLEncoder.encode(attributeValue)).append("\"");
+               }
+               if (children.size() == 0) {
+                       tagBuffer.append(" />");
+               } else {
+                       tagBuffer.append(">");
+                       for (int childIndex = 0, childCount = children.size(); 
childIndex < childCount; childIndex++) {
+                               HTMLNode childNode = (HTMLNode) 
children.get(childIndex);
+                               childNode.generate(tagBuffer);
+                       }
+                       tagBuffer.append("</").append(name).append(">");
+               }
+               return tagBuffer;
+       }
+
+}
\ No newline at end of file


Property changes on: trunk/freenet/src/freenet/support/HTMLNode.java
___________________________________________________________________
Name: svn:keywords
   + Id


Reply via email to