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