source file: /home/buildslave/tahoe/edgy/build/src/allmydata/web/unlinked.py
file stats: 83 lines, 83 executed: 100.0% covered
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 nevow.inevow import IRequest
7. from allmydata.immutable.upload import FileHandle
8. from allmydata.web.common import IClient, getxmlfile, get_arg, boolean_of_arg
9. from allmydata.web import status
10.
11. def PUTUnlinkedCHK(ctx):
12. req = IRequest(ctx)
13. # "PUT /uri", to create an unlinked file.
14. client = IClient(ctx)
15. uploadable = FileHandle(req.content, client.convergence)
16. d = client.upload(uploadable)
17. d.addCallback(lambda results: results.uri)
18. # that fires with the URI of the new file
19. return d
20.
21. def PUTUnlinkedSSK(ctx):
22. req = IRequest(ctx)
23. # SDMF: files are small, and we can only upload data
24. req.content.seek(0)
25. data = req.content.read()
26. d = IClient(ctx).create_mutable_file(data)
27. d.addCallback(lambda n: n.get_uri())
28. return d
29.
30. def PUTUnlinkedCreateDirectory(ctx):
31. req = IRequest(ctx)
32. # "PUT /uri?t=mkdir", to create an unlinked directory.
33. d = IClient(ctx).create_empty_dirnode()
34. d.addCallback(lambda dirnode: dirnode.get_uri())
35. # XXX add redirect_to_result
36. return d
37.
38.
39. def POSTUnlinkedCHK(ctx):
40. req = IRequest(ctx)
41. client = IClient(ctx)
42. fileobj = req.fields["file"].file
43. uploadable = FileHandle(fileobj, client.convergence)
44. d = client.upload(uploadable)
45. when_done = get_arg(req, "when_done", None)
46. if when_done:
47. # if when_done= is provided, return a redirect instead of our
48. # usual upload-results page
49. def _done(upload_results, redir_to):
50. if "%(uri)s" in redir_to:
51. redir_to = redir_to % {"uri": urllib.quote(upload_results.uri)
52. }
53. return url.URL.fromString(redir_to)
54. d.addCallback(_done, when_done)
55. else:
56. # return the Upload Results page, which includes the URI
57. d.addCallback(UploadResultsPage, ctx)
58. return d
59.
60.
61. class UploadResultsPage(status.UploadResultsRendererMixin, rend.Page):
62. """'POST /uri', to create an unlinked file."""
63. docFactory = getxmlfile("upload-results.xhtml")
64.
65. def __init__(self, upload_results, ctx):
66. rend.Page.__init__(self)
67. self.results = upload_results
68.
69. def upload_results(self):
70. return defer.succeed(self.results)
71.
72. def data_done(self, ctx, data):
73. d = self.upload_results()
74. d.addCallback(lambda res: "done!")
75. return d
76.
77. def data_uri(self, ctx, data):
78. d = self.upload_results()
79. d.addCallback(lambda res: res.uri)
80. return d
81.
82. def render_download_link(self, ctx, data):
83. d = self.upload_results()
84. d.addCallback(lambda res: T.a(href="/uri/" + urllib.quote(res.uri))
85. ["/uri/" + res.uri])
86. return d
87.
88. def POSTUnlinkedSSK(ctx):
89. req = IRequest(ctx)
90. # "POST /uri", to create an unlinked file.
91. # SDMF: files are small, and we can only upload data
92. contents = req.fields["file"]
93. contents.file.seek(0)
94. data = contents.file.read()
95. d = IClient(ctx).create_mutable_file(data)
96. d.addCallback(lambda n: n.get_uri())
97. return d
98.
99. def POSTUnlinkedCreateDirectory(ctx):
100. req = IRequest(ctx)
101. # "POST /uri?t=mkdir", to create an unlinked directory.
102. d = IClient(ctx).create_empty_dirnode()
103. redirect = get_arg(req, "redirect_to_result", "false")
104. if boolean_of_arg(redirect):
105. def _then_redir(res):
106. new_url = "uri/" + urllib.quote(res.get_uri())
107. req.setResponseCode(http.SEE_OTHER) # 303
108. req.setHeader('location', new_url)
109. req.finish()
110. return ''
111. d.addCallback(_then_redir)
112. else:
113. d.addCallback(lambda dirnode: dirnode.get_uri())
114. return d
115.