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
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
19 def PUTUnlinkedSSK(req, client):
20 # SDMF: files are small, and we can only upload data
22 data = req.content.read()
23 d = client.create_mutable_file(data)
24 d.addCallback(lambda n: n.get_uri())
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
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)
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)
47 return url.URL.fromString(redir_to)
48 d.addCallback(_done, when_done)
50 # return the Upload Results page, which includes the URI
51 d.addCallback(UploadResultsPage)
55 class UploadResultsPage(status.UploadResultsRendererMixin, rend.Page):
56 """'POST /uri', to create an unlinked file."""
57 docFactory = getxmlfile("upload-results.xhtml")
59 def __init__(self, upload_results):
60 rend.Page.__init__(self)
61 self.results = upload_results
63 def upload_results(self):
64 return defer.succeed(self.results)
66 def data_done(self, ctx, data):
67 d = self.upload_results()
68 d.addCallback(lambda res: "done!")
71 def data_uri(self, ctx, data):
72 d = self.upload_results()
73 d.addCallback(lambda res: res.uri)
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))
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"]
87 data = contents.file.read()
88 d = client.create_mutable_file(data)
89 d.addCallback(lambda n: n.get_uri())
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).
102 kids_json = req.content.read()
104 raise WebError("t=mkdir does not accept children=, "
105 "try t=mkdir-with-children instead",
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)
116 d.addCallback(_then_redir)
118 d.addCallback(lambda dirnode: dirnode.get_uri())
121 def POSTUnlinkedCreateDirectoryWithChildren(req, client):
122 # "POST /uri?t=mkdir", to create an unlinked directory.
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)
135 d.addCallback(_then_redir)
137 d.addCallback(lambda dirnode: dirnode.get_uri())
140 def POSTUnlinkedCreateImmutableDirectory(req, client):
141 # "POST /uri?t=mkdir", to create an unlinked directory.
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)
154 d.addCallback(_then_redir)
156 d.addCallback(lambda dirnode: dirnode.get_uri())