]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/unlinked.py
670aff68520c15a3032b350e90cf6ae1ecf8109e
[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_children_json, WebError
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     d = client.create_dirnode()
30     d.addCallback(lambda dirnode: dirnode.get_uri())
31     # XXX add redirect_to_result
32     return d
33
34
35 def POSTUnlinkedCHK(req, client):
36     fileobj = req.fields["file"].file
37     uploadable = FileHandle(fileobj, client.convergence)
38     d = client.upload(uploadable)
39     when_done = get_arg(req, "when_done", None)
40     if when_done:
41         # if when_done= is provided, return a redirect instead of our
42         # usual upload-results page
43         def _done(upload_results, redir_to):
44             if "%(uri)s" in redir_to:
45                 redir_to = redir_to % {"uri": urllib.quote(upload_results.uri)
46                                          }
47             return url.URL.fromString(redir_to)
48         d.addCallback(_done, when_done)
49     else:
50         # return the Upload Results page, which includes the URI
51         d.addCallback(UploadResultsPage)
52     return d
53
54
55 class UploadResultsPage(status.UploadResultsRendererMixin, rend.Page):
56     """'POST /uri', to create an unlinked file."""
57     docFactory = getxmlfile("upload-results.xhtml")
58
59     def __init__(self, upload_results):
60         rend.Page.__init__(self)
61         self.results = upload_results
62
63     def upload_results(self):
64         return defer.succeed(self.results)
65
66     def data_done(self, ctx, data):
67         d = self.upload_results()
68         d.addCallback(lambda res: "done!")
69         return d
70
71     def data_uri(self, ctx, data):
72         d = self.upload_results()
73         d.addCallback(lambda res: res.uri)
74         return d
75
76     def render_download_link(self, ctx, data):
77         d = self.upload_results()
78         d.addCallback(lambda res: T.a(href="/uri/" + urllib.quote(res.uri))
79                       ["/uri/" + res.uri])
80         return d
81
82 def POSTUnlinkedSSK(req, client):
83     # "POST /uri", to create an unlinked file.
84     # SDMF: files are small, and we can only upload data
85     contents = req.fields["file"]
86     contents.file.seek(0)
87     data = contents.file.read()
88     d = client.create_mutable_file(data)
89     d.addCallback(lambda n: n.get_uri())
90     return d
91
92 def POSTUnlinkedCreateDirectory(req, client):
93     # "POST /uri?t=mkdir", to create an unlinked directory.
94     ct = req.getHeader("content-type") or ""
95     if not ct.startswith("multipart/form-data"):
96         # guard against accidental attempts to call t=mkdir as if it were
97         # t=mkdir-with-children, but make sure we tolerate the usual HTML
98         # create-directory form (in which the t=mkdir and redirect_to_result=
99         # and other arguments can be passed encoded as multipath/form-data,
100         # in the request body).
101         req.content.seek(0)
102         kids_json = req.content.read()
103         if kids_json:
104             raise WebError("t=mkdir does not accept children=, "
105                            "try t=mkdir-with-children instead",
106                            http.BAD_REQUEST)
107     d = client.create_dirnode()
108     redirect = get_arg(req, "redirect_to_result", "false")
109     if boolean_of_arg(redirect):
110         def _then_redir(res):
111             new_url = "uri/" + urllib.quote(res.get_uri())
112             req.setResponseCode(http.SEE_OTHER) # 303
113             req.setHeader('location', new_url)
114             req.finish()
115             return ''
116         d.addCallback(_then_redir)
117     else:
118         d.addCallback(lambda dirnode: dirnode.get_uri())
119     return d
120
121 def POSTUnlinkedCreateDirectoryWithChildren(req, client):
122     # "POST /uri?t=mkdir", to create an unlinked directory.
123     req.content.seek(0)
124     kids_json = req.content.read()
125     kids = convert_children_json(client.nodemaker, kids_json)
126     d = client.create_dirnode(initial_children=kids)
127     redirect = get_arg(req, "redirect_to_result", "false")
128     if boolean_of_arg(redirect):
129         def _then_redir(res):
130             new_url = "uri/" + urllib.quote(res.get_uri())
131             req.setResponseCode(http.SEE_OTHER) # 303
132             req.setHeader('location', new_url)
133             req.finish()
134             return ''
135         d.addCallback(_then_redir)
136     else:
137         d.addCallback(lambda dirnode: dirnode.get_uri())
138     return d
139
140 def POSTUnlinkedCreateImmutableDirectory(req, client):
141     # "POST /uri?t=mkdir", to create an unlinked directory.
142     req.content.seek(0)
143     kids_json = req.content.read()
144     kids = convert_children_json(client.nodemaker, kids_json)
145     d = client.create_immutable_dirnode(kids)
146     redirect = get_arg(req, "redirect_to_result", "false")
147     if boolean_of_arg(redirect):
148         def _then_redir(res):
149             new_url = "uri/" + urllib.quote(res.get_uri())
150             req.setResponseCode(http.SEE_OTHER) # 303
151             req.setHeader('location', new_url)
152             req.finish()
153             return ''
154         d.addCallback(_then_redir)
155     else:
156         d.addCallback(lambda dirnode: dirnode.get_uri())
157     return d