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
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.
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
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)
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)
50 return url.URL.fromString(redir_to)
51 d.addCallback(_done, when_done)
53 # return the Upload Results page, which includes the URI
54 d.addCallback(UploadResultsPage)
58 class UploadResultsPage(status.UploadResultsRendererMixin, rend.Page):
59 """'POST /uri', to create an unlinked file."""
60 docFactory = getxmlfile("upload-results.xhtml")
62 def __init__(self, upload_results):
63 rend.Page.__init__(self)
64 self.results = upload_results
66 def upload_results(self):
67 return defer.succeed(self.results)
69 def data_done(self, ctx, data):
70 d = self.upload_results()
71 d.addCallback(lambda res: "done!")
74 def data_uri(self, ctx, data):
75 d = self.upload_results()
76 d.addCallback(lambda res: res.uri)
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))
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"]
90 data = contents.file.read()
91 d = client.create_mutable_file(data)
92 d.addCallback(lambda n: n.get_uri())
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)
108 d.addCallback(_then_redir)
110 d.addCallback(lambda dirnode: dirnode.get_uri())