]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
implemented upload side of webish
authorBrian Warner <warner@lothar.com>
Mon, 4 Dec 2006 12:15:36 +0000 (05:15 -0700)
committerBrian Warner <warner@lothar.com>
Mon, 4 Dec 2006 12:15:36 +0000 (05:15 -0700)
allmydata/vdrive.py
allmydata/web/directory.xhtml
allmydata/webish.py

index a676bb420021e8c2e62e00a54505751edfc9a9de..c0eed1fb57ba6d0296f17b0523a4e176f8d439db 100644 (file)
@@ -106,6 +106,8 @@ class VDrive(service.MultiService):
         d.addCallback(lambda parent: parent.callRemote("add_directory", name))
         return d
 
+    def remove(self, something): # TODO
+        pass
 
     def get_file(self, dir_and_name_or_path, download_target):
         """Retrieve a file from the virtual drive and put it somewhere.
index 1e68ca3d7bcbdd619d6ccb78a62d0c9685f59854..47f8cd473b653eb4e24b39cdddbc508264cc8883 100644 (file)
@@ -28,7 +28,7 @@
 
 </table>
 
-<!-- <div n:render="forms"/> -->
+<div n:render="forms"/>
 
 <!-- <div class="results" n:render="results"/> -->
 
index 499f405132fcd4a932d8252e4a44936ee9378ad9..c4f18f69537d8d6bff33c22537f64c89a6a32666 100644 (file)
@@ -1,12 +1,14 @@
 
 from twisted.application import service, internet
 from twisted.web import static, resource, server
-from twisted.python import util
-from nevow import inevow, rend, loaders, appserver, tags as T
+from twisted.python import util, log
+from nevow import inevow, rend, loaders, appserver, url, tags as T
 from allmydata.util import idlib
 from allmydata.download import IDownloadTarget#, IDownloader
+from allmydata import upload
 from zope.interface import implements
 import urllib
+from formless import annotate, webform
 
 def getxmlfile(name):
     return loaders.xmlfile(util.sibpath(__file__, "web/%s" % name))
@@ -26,8 +28,7 @@ class WebishServer(service.MultiService):
         internet.TCPServer(webport, site).setServiceParent(self)
 
     def set_root_dirnode(self, dirnode):
-        dl = self.parent.getServiceNamed("downloader")
-        self.root.putChild("vdrive", Directory(dirnode, "/", dl))
+        self.root.putChild("vdrive", Directory(dirnode, "/", self.parent))
         #print "REMEMBERING", self.site, dl, IDownloader
         #self.site.remember(dl, IDownloader)
 
@@ -36,14 +37,24 @@ class Welcome(rend.Page):
     addSlash = True
     docFactory = getxmlfile("welcome.xhtml")
 
+class IUpload(annotate.TypedInterface):
+    def upload(contents=annotate.FileUpload(label="Choose a file to upload: ",
+                                            required=True,
+                                            requiredFailMessage="Do iT!"),
+               ctx=annotate.Context(),
+               ):
+        #"""Upload a file"""
+        pass
+    upload = annotate.autocallable(upload, action="Upload file")
+
 class Directory(rend.Page):
     addSlash = True
     docFactory = getxmlfile("directory.xhtml")
 
-    def __init__(self, dirnode, dirname, downloader):
+    def __init__(self, dirnode, dirname, client):
         self._dirnode = dirnode
         self._dirname = dirname
-        self._downloader = downloader
+        self._client = client
 
     def childFactory(self, ctx, name):
         if name.startswith("freeform"): # ick
@@ -52,7 +63,7 @@ class Directory(rend.Page):
             args = inevow.IRequest(ctx).args
             filename = args["filename"][0]
             verifierid = args["verifierid"][0]
-            return Downloader(self._downloader,
+            return Downloader(self._client.getServiceNamed("downloader"),
                               self._dirname, filename, idlib.a2b(verifierid))
         if self._dirname == "/":
             dirname = "/" + name
@@ -60,7 +71,7 @@ class Directory(rend.Page):
             dirname = self._dirname + "/" + name
         d = self._dirnode.callRemote("get", name)
         d.addCallback(lambda newnode:
-                      Directory(newnode, dirname, self._downloader))
+                      Directory(newnode, dirname, self._client))
         return d
 
     def render_title(self, ctx, data):
@@ -91,6 +102,31 @@ class Directory(rend.Page):
             ctx.fillSlots("fileid", "-")
         return ctx.tag
 
+    # this tells formless about what functions can be invoked, giving it
+    # enough information to construct form contents
+    implements(IUpload)
+
+    child_webform_css = webform.defaultCSS
+    def render_forms(self, ctx, data):
+        return webform.renderForms()
+
+    def upload(self, contents, ctx):
+        # contents is a cgi.FieldStorage instance
+        log.msg("starting webish upload")
+
+        uploader = self._client.getServiceNamed("uploader")
+        d = uploader.upload(upload.Data(contents.value))
+        name = contents.filename
+        d.addCallback(lambda vid:
+                      self._dirnode.callRemote("add_file", name, vid))
+        def _done(res):
+            log.msg("webish upload complete")
+            return res
+        d.addCallback(_done)
+        return d
+        return url.here.add("results",
+                            "upload of '%s' complete!" % contents.filename)
+
 class WebDownloadTarget:
     implements(IDownloadTarget)
     def __init__(self, req):
@@ -143,3 +179,5 @@ class Downloader(resource.Resource):
         dl = self._downloader
         dl.download(self._verifierid, t)
         return server.NOT_DONE_YET
+
+