3 from twisted.web import http
4 from nevow import rend, inevow, url, tags as T
5 from allmydata.upload import FileHandle
6 from allmydata.web.common import IClient, getxmlfile, get_arg, boolean_of_arg
7 from allmydata.web import status
8 from allmydata.util import observer
10 class UnlinkedPUTCHKUploader(rend.Page):
11 def renderHTTP(self, ctx):
12 req = inevow.IRequest(ctx)
13 assert req.method == "PUT"
14 # "PUT /uri", to create an unlinked file. This is like PUT but
15 # without the associated set_uri.
17 uploadable = FileHandle(req.content)
18 d = IClient(ctx).upload(uploadable)
19 d.addCallback(lambda results: results.uri)
20 # that fires with the URI of the new file
23 class UnlinkedPUTSSKUploader(rend.Page):
24 def renderHTTP(self, ctx):
25 req = inevow.IRequest(ctx)
26 assert req.method == "PUT"
27 # SDMF: files are small, and we can only upload data
29 data = req.content.read()
30 d = IClient(ctx).create_mutable_file(data)
31 d.addCallback(lambda n: n.get_uri())
34 class UnlinkedPUTCreateDirectory(rend.Page):
35 def renderHTTP(self, ctx):
36 req = inevow.IRequest(ctx)
37 assert req.method == "PUT"
38 # "PUT /uri?t=mkdir", to create an unlinked directory.
39 d = IClient(ctx).create_empty_dirnode()
40 d.addCallback(lambda dirnode: dirnode.get_uri())
41 # XXX add redirect_to_result
44 class UnlinkedPOSTCHKUploader(status.UploadResultsRendererMixin, rend.Page):
45 """'POST /uri', to create an unlinked file."""
46 docFactory = getxmlfile("upload-results.xhtml")
48 def __init__(self, client, req):
49 rend.Page.__init__(self)
50 # we start the upload now, and distribute notification of its
51 # completion to render_ methods with an ObserverList
52 assert req.method == "POST"
53 self._done = observer.OneShotObserverList()
54 fileobj = req.fields["file"].file
55 uploadable = FileHandle(fileobj)
56 d = client.upload(uploadable)
57 d.addBoth(self._done.fire)
59 def renderHTTP(self, ctx):
60 req = inevow.IRequest(ctx)
61 when_done = get_arg(req, "when_done", None)
63 # if when_done= is provided, return a redirect instead of our
64 # usual upload-results page
65 d = self._done.when_fired()
66 d.addCallback(lambda res: url.URL.fromString(when_done))
68 return rend.Page.renderHTTP(self, ctx)
70 def upload_results(self):
71 return self._done.when_fired()
73 def data_done(self, ctx, data):
74 d = self.upload_results()
75 d.addCallback(lambda res: "done!")
78 def data_uri(self, ctx, data):
79 d = self.upload_results()
80 d.addCallback(lambda res: res.uri)
83 def render_download_link(self, ctx, data):
84 d = self.upload_results()
85 d.addCallback(lambda res: T.a(href="/uri/" + urllib.quote(res.uri))
89 class UnlinkedPOSTSSKUploader(rend.Page):
90 def renderHTTP(self, ctx):
91 req = inevow.IRequest(ctx)
92 assert req.method == "POST"
94 # "POST /uri", to create an unlinked file.
95 # SDMF: files are small, and we can only upload data
96 contents = req.fields["file"]
98 data = contents.file.read()
99 d = IClient(ctx).create_mutable_file(data)
100 d.addCallback(lambda n: n.get_uri())
103 class UnlinkedPOSTCreateDirectory(rend.Page):
104 def renderHTTP(self, ctx):
105 req = inevow.IRequest(ctx)
106 assert req.method == "POST"
108 # "POST /uri?t=mkdir", to create an unlinked directory.
109 d = IClient(ctx).create_empty_dirnode()
110 redirect = get_arg(req, "redirect_to_result", "false")
111 if boolean_of_arg(redirect):
112 def _then_redir(res):
113 new_url = "uri/" + urllib.quote(res.get_uri())
114 req.setResponseCode(http.SEE_OTHER) # 303
115 req.setHeader('location', new_url)
118 d.addCallback(_then_redir)
120 d.addCallback(lambda dirnode: dirnode.get_uri())