]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/unlinked.py
webapi: pass client through constructor arguments, remove IClient, should make it...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / unlinked.py
1
2 import urllib
3 from twisted.web import http
4 from twisted.internet import defer
5 from nevow import rend, url, tags as T
6 from allmydata.immutable.upload import FileHandle
7 from allmydata.web.common import getxmlfile, get_arg, boolean_of_arg
8 from allmydata.web import status
9
10 def PUTUnlinkedCHK(req, client):
11     # "PUT /uri", to create an unlinked file.
12     uploadable = FileHandle(req.content, client.convergence)
13     d = client.upload(uploadable)
14     d.addCallback(lambda results: results.uri)
15     # that fires with the URI of the new file
16     return d
17
18 def PUTUnlinkedSSK(req, client):
19     # SDMF: files are small, and we can only upload data
20     req.content.seek(0)
21     data = req.content.read()
22     d = client.create_mutable_file(data)
23     d.addCallback(lambda n: n.get_uri())
24     return d
25
26 def PUTUnlinkedCreateDirectory(req, client):
27     # "PUT /uri?t=mkdir", to create an unlinked directory.
28     d = client.create_empty_dirnode()
29     d.addCallback(lambda dirnode: dirnode.get_uri())
30     # XXX add redirect_to_result
31     return d
32
33
34 def POSTUnlinkedCHK(req, client):
35     fileobj = req.fields["file"].file
36     uploadable = FileHandle(fileobj, client.convergence)
37     d = client.upload(uploadable)
38     when_done = get_arg(req, "when_done", None)
39     if when_done:
40         # if when_done= is provided, return a redirect instead of our
41         # usual upload-results page
42         def _done(upload_results, redir_to):
43             if "%(uri)s" in redir_to:
44                 redir_to = redir_to % {"uri": urllib.quote(upload_results.uri)
45                                          }
46             return url.URL.fromString(redir_to)
47         d.addCallback(_done, when_done)
48     else:
49         # return the Upload Results page, which includes the URI
50         d.addCallback(UploadResultsPage)
51     return d
52
53
54 class UploadResultsPage(status.UploadResultsRendererMixin, rend.Page):
55     """'POST /uri', to create an unlinked file."""
56     docFactory = getxmlfile("upload-results.xhtml")
57
58     def __init__(self, upload_results):
59         rend.Page.__init__(self)
60         self.results = upload_results
61
62     def upload_results(self):
63         return defer.succeed(self.results)
64
65     def data_done(self, ctx, data):
66         d = self.upload_results()
67         d.addCallback(lambda res: "done!")
68         return d
69
70     def data_uri(self, ctx, data):
71         d = self.upload_results()
72         d.addCallback(lambda res: res.uri)
73         return d
74
75     def render_download_link(self, ctx, data):
76         d = self.upload_results()
77         d.addCallback(lambda res: T.a(href="/uri/" + urllib.quote(res.uri))
78                       ["/uri/" + res.uri])
79         return d
80
81 def POSTUnlinkedSSK(req, client):
82     # "POST /uri", to create an unlinked file.
83     # SDMF: files are small, and we can only upload data
84     contents = req.fields["file"]
85     contents.file.seek(0)
86     data = contents.file.read()
87     d = client.create_mutable_file(data)
88     d.addCallback(lambda n: n.get_uri())
89     return d
90
91 def POSTUnlinkedCreateDirectory(req, client):
92     # "POST /uri?t=mkdir", to create an unlinked directory.
93     d = client.create_empty_dirnode()
94     redirect = get_arg(req, "redirect_to_result", "false")
95     if boolean_of_arg(redirect):
96         def _then_redir(res):
97             new_url = "uri/" + urllib.quote(res.get_uri())
98             req.setResponseCode(http.SEE_OTHER) # 303
99             req.setHeader('location', new_url)
100             req.finish()
101             return ''
102         d.addCallback(_then_redir)
103     else:
104         d.addCallback(lambda dirnode: dirnode.get_uri())
105     return d
106