From: Brian Warner Date: Tue, 10 Jul 2007 20:24:10 +0000 (-0700) Subject: webapi: normalized API: use t=upload or t=download when providing localdir= or localfile= X-Git-Url: https://git.rkrishnan.org/index.html?a=commitdiff_plain;h=13e84526a119a9ee6547d7d6f6933a48cb9ecc29;p=tahoe-lafs%2Ftahoe-lafs.git webapi: normalized API: use t=upload or t=download when providing localdir= or localfile= --- diff --git a/docs/webapi.txt b/docs/webapi.txt index 4597dbff..e59380cd 100644 --- a/docs/webapi.txt +++ b/docs/webapi.txt @@ -98,7 +98,7 @@ for files and directories which do not yet exist. [ 'filenode', { 'mutable': bool, 'uri': file_uri, 'size': bytes } ] - GET FILEURL?localfile=$FILENAME + GET FILEURL?t=download&localfile=$FILENAME This instructs the client to download the given file and write its contents into the local filesystem at $FILENAME. This request will only be accepted @@ -109,7 +109,7 @@ for files and directories which do not yet exist. (thoughts: we could use either the response headers or the response body to indicate download progress) - PUT NEWFILEURL?localfile=$FILENAME + PUT NEWFILEURL?t=upload&localfile=$FILENAME This uploads file to the vdrive and gets the contents from a file in the client's local filesystem. As with GET, this request will only be accepted @@ -191,7 +191,7 @@ for files and directories which do not yet exist. vdrive to its original state (it may leave some intermediate directory nodes). - GET DIRURL?localdir=$DIRNAME + GET DIRURL?t=download&localdir=$DIRNAME This instructs the client to perform a recursive download of the given directory and all its descendant files and directories, writing the results @@ -200,7 +200,7 @@ for files and directories which do not yet exist. (thoughts: we could use the response headers or the response body to indicate download progress) - PUT NEWDIRURL?localdir=$DIRNAME + PUT NEWDIRURL?t=upload&localdir=$DIRNAME This instructs the client to perform a recursive upload of a directory on the local filesystem into the vdrive at the given location. NEWDIRURL will @@ -213,7 +213,7 @@ for files and directories which do not yet exist. Note that the "curl" utility can be used to provoke this sort of recursive upload, since the -T option will make it use an HTTP 'PUT': - curl -T /dev/null http://localhost:8011/vdrive/global/newdir?localdir=/home/user/directory-to-upload' + curl -T /dev/null 'http://localhost:8011/vdrive/global/newdir?t=upload&localdir=/home/user/directory-to-upload' == POST Forms == diff --git a/src/allmydata/test/test_web.py b/src/allmydata/test/test_web.py index 7652ca8b..e5e531d3 100644 --- a/src/allmydata/test/test_web.py +++ b/src/allmydata/test/test_web.py @@ -407,7 +407,8 @@ class Web(unittest.TestCase): def test_GET_FILEURL_localfile(self): # YES localfile = os.path.abspath("web/GET_FILEURL_localfile") fileutil.make_dirs("web") - d = self.GET("/vdrive/global/foo/bar.txt?localfile=%s" % localfile) + d = self.GET("/vdrive/global/foo/bar.txt?t=download&localfile=%s" + % localfile) def _done(res): self.failUnless(os.path.exists(localfile)) data = open(localfile, "rb").read() @@ -423,7 +424,8 @@ class Web(unittest.TestCase): webish.LOCALHOST = "127.0.0.2" localfile = os.path.abspath("web/GET_FILEURL_localfile_nonlocal") fileutil.make_dirs("web") - d = self.GET("/vdrive/global/foo/bar.txt?localfile=%s" % localfile) + d = self.GET("/vdrive/global/foo/bar.txt?t=download&localfile=%s" + % localfile) d.addBoth(self.shouldFail, error.Error, "localfile non-local", "403 Forbidden") def _check(res): @@ -438,7 +440,8 @@ class Web(unittest.TestCase): def test_GET_FILEURL_localfile_nonabsolute(self): # YES localfile = "web/nonabsolute/path" fileutil.make_dirs("web/nonabsolute") - d = self.GET("/vdrive/global/foo/bar.txt?localfile=%s" % localfile) + d = self.GET("/vdrive/global/foo/bar.txt?t=download&localfile=%s" + % localfile) d.addBoth(self.shouldFail, error.Error, "localfile non-absolute", "403 Forbidden") def _check(res): @@ -452,7 +455,8 @@ class Web(unittest.TestCase): f = open(localfile, "wb") f.write(self.NEWFILE_CONTENTS) f.close() - d = self.PUT("/vdrive/global/foo/new.txt?localfile=%s" % localfile, "") + d = self.PUT("/vdrive/global/foo/new.txt?t=upload&localfile=%s" % + localfile, "") def _check(res): self.failUnless("new.txt" in self._foo_node.children) new_uri = self._foo_node.children["new.txt"] @@ -468,8 +472,8 @@ class Web(unittest.TestCase): f = open(localfile, "wb") f.write(self.NEWFILE_CONTENTS) f.close() - d = self.PUT("/vdrive/global/foo/newdir/new.txt?localfile=%s" % - localfile, "") + d = self.PUT("/vdrive/global/foo/newdir/new.txt?t=upload&localfile=%s" + % localfile, "") def _check(res): self.failIf("new.txt" in self._foo_node.children) self.failUnless("newdir" in self._foo_node.children) @@ -639,7 +643,7 @@ class Web(unittest.TestCase): def test_GET_DIRURL_localdir(self): # YES localdir = os.path.abspath("web/GET_DIRURL_localdir") fileutil.make_dirs("web") - d = self.GET("/vdrive/global/foo?localdir=%s" % localdir) + d = self.GET("/vdrive/global/foo?t=download&localdir=%s" % localdir) def _check(res): barfile = os.path.join(localdir, "bar.txt") self.failUnless(os.path.exists(barfile)) @@ -684,7 +688,8 @@ class Web(unittest.TestCase): self.touch(localdir, "three/bar.txt") self.touch(localdir, "zap.zip") - d = self.PUT("/vdrive/global/foo/newdir?localdir=%s" % localdir, "") + d = self.PUT("/vdrive/global/foo/newdir?t=upload&localdir=%s" + % localdir, "") def _check(res): self.failUnless("newdir" in self._foo_node.children) newnode = self.nodes[self._foo_node.children["newdir"]] @@ -713,7 +718,8 @@ class Web(unittest.TestCase): self.touch(localdir, "three/bar.txt") self.touch(localdir, "zap.zip") - d = self.PUT("/vdrive/global/foo/subdir/newdir?localdir=%s" % localdir, + d = self.PUT("/vdrive/global/foo/subdir/newdir?t=upload&localdir=%s" + % localdir, "") def _check(res): self.failUnless("subdir" in self._foo_node.children) diff --git a/src/allmydata/webish.py b/src/allmydata/webish.py index 37b5d972..2b7fe0f3 100644 --- a/src/allmydata/webish.py +++ b/src/allmydata/webish.py @@ -525,11 +525,14 @@ class PUTHandler(rend.Page): # we must traverse the path, creating new directories as necessary d = self._get_or_create_directories(self._node, self._path[:-1]) name = self._path[-1] - if localfile: - d.addCallback(self._upload_localfile, localfile, name) - elif localdir: - d.addCallback(self._get_or_create_directories, self._path[-1:]) - d.addCallback(self._upload_localdir, localdir) + if t == "upload": + if localfile: + d.addCallback(self._upload_localfile, localfile, name) + elif localdir: + d.addCallback(self._get_or_create_directories, self._path[-1:]) + d.addCallback(self._upload_localdir, localdir) + else: + raise RuntimeError("t=upload requires localfile= or localdir=") elif t == "uri": d.addCallback(self._attach_uri, req.content, name) elif t == "mkdir": @@ -702,9 +705,12 @@ class VDrive(rend.Page): filename = path[-1] if "filename" in req.args: filename = req.args["filename"][0] - if localfile: - # write contents to a local file - return LocalFileDownloader(node, localfile), () + if t == "download": + if localfile: + # write contents to a local file + return LocalFileDownloader(node, localfile), () + # send contents as the result + return FileDownloader(node, filename), () elif t == "": # send contents as the result return FileDownloader(node, filename), () @@ -717,9 +723,11 @@ class VDrive(rend.Page): else: raise RuntimeError("bad t=%s" % t) elif IDirectoryNode.providedBy(node): - if localdir: - # recursive download to a local directory - return LocalDirectoryDownloader(node, localdir), () + if t == "download": + if localdir: + # recursive download to a local directory + return LocalDirectoryDownloader(node, localdir), () + raise RuntimeError("t=download requires localdir=") elif t == "": # send an HTML representation of the directory return Directory(self.name, node, path), ()