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
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
18 def PUTUnlinkedSSK(req, client):
19 # SDMF: files are small, and we can only upload data
21 data = req.content.read()
22 d = client.create_mutable_file(data)
23 d.addCallback(lambda n: n.get_uri())
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
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)
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)
46 return url.URL.fromString(redir_to)
47 d.addCallback(_done, when_done)
49 # return the Upload Results page, which includes the URI
50 d.addCallback(UploadResultsPage)
54 class UploadResultsPage(status.UploadResultsRendererMixin, rend.Page):
55 """'POST /uri', to create an unlinked file."""
56 docFactory = getxmlfile("upload-results.xhtml")
58 def __init__(self, upload_results):
59 rend.Page.__init__(self)
60 self.results = upload_results
62 def upload_results(self):
63 return defer.succeed(self.results)
65 def data_done(self, ctx, data):
66 d = self.upload_results()
67 d.addCallback(lambda res: "done!")
70 def data_uri(self, ctx, data):
71 d = self.upload_results()
72 d.addCallback(lambda res: res.uri)
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))
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"]
86 data = contents.file.read()
87 d = client.create_mutable_file(data)
88 d.addCallback(lambda n: n.get_uri())
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):
97 new_url = "uri/" + urllib.quote(res.get_uri())
98 req.setResponseCode(http.SEE_OTHER) # 303
99 req.setHeader('location', new_url)
102 d.addCallback(_then_redir)
104 d.addCallback(lambda dirnode: dirnode.get_uri())