2008-03-05 22:12:42 +00:00
|
|
|
|
|
|
|
import urllib
|
|
|
|
from twisted.web import http
|
2008-05-19 19:57:04 +00:00
|
|
|
from twisted.internet import defer
|
|
|
|
from nevow import rend, url, tags as T
|
2008-07-16 20:14:39 +00:00
|
|
|
from allmydata.immutable.upload import FileHandle
|
2011-08-07 00:43:48 +00:00
|
|
|
from allmydata.mutable.publish import MutableFileHandle
|
2009-10-13 02:34:44 +00:00
|
|
|
from allmydata.web.common import getxmlfile, get_arg, boolean_of_arg, \
|
2011-10-13 16:29:51 +00:00
|
|
|
convert_children_json, WebError, get_format, get_mutable_type
|
2008-03-05 22:12:42 +00:00
|
|
|
from allmydata.web import status
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def PUTUnlinkedCHK(req, client):
|
2008-05-19 19:57:04 +00:00
|
|
|
# "PUT /uri", to create an unlinked file.
|
|
|
|
uploadable = FileHandle(req.content, client.convergence)
|
|
|
|
d = client.upload(uploadable)
|
2012-05-22 04:14:44 +00:00
|
|
|
d.addCallback(lambda results: results.get_uri())
|
2008-05-19 19:57:04 +00:00
|
|
|
# that fires with the URI of the new file
|
|
|
|
return d
|
|
|
|
|
2011-08-07 00:43:48 +00:00
|
|
|
def PUTUnlinkedSSK(req, client, version):
|
2008-05-19 19:57:04 +00:00
|
|
|
# SDMF: files are small, and we can only upload data
|
|
|
|
req.content.seek(0)
|
2011-08-07 00:43:48 +00:00
|
|
|
data = MutableFileHandle(req.content)
|
|
|
|
d = client.create_mutable_file(data, version=version)
|
2008-05-19 19:57:04 +00:00
|
|
|
d.addCallback(lambda n: n.get_uri())
|
|
|
|
return d
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def PUTUnlinkedCreateDirectory(req, client):
|
2008-05-19 19:57:04 +00:00
|
|
|
# "PUT /uri?t=mkdir", to create an unlinked directory.
|
2011-10-13 16:29:51 +00:00
|
|
|
file_format = get_format(req, None)
|
|
|
|
if file_format == "CHK":
|
2011-10-13 16:32:29 +00:00
|
|
|
raise WebError("format=CHK not accepted for PUT /uri?t=mkdir",
|
2011-10-13 16:29:51 +00:00
|
|
|
http.BAD_REQUEST)
|
|
|
|
mt = None
|
|
|
|
if file_format:
|
|
|
|
mt = get_mutable_type(file_format)
|
|
|
|
d = client.create_dirnode(version=mt)
|
2008-05-19 19:57:04 +00:00
|
|
|
d.addCallback(lambda dirnode: dirnode.get_uri())
|
|
|
|
# XXX add redirect_to_result
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def POSTUnlinkedCHK(req, client):
|
2008-05-19 19:57:04 +00:00
|
|
|
fileobj = req.fields["file"].file
|
|
|
|
uploadable = FileHandle(fileobj, client.convergence)
|
|
|
|
d = client.upload(uploadable)
|
|
|
|
when_done = get_arg(req, "when_done", None)
|
|
|
|
if when_done:
|
|
|
|
# if when_done= is provided, return a redirect instead of our
|
|
|
|
# usual upload-results page
|
|
|
|
def _done(upload_results, redir_to):
|
|
|
|
if "%(uri)s" in redir_to:
|
2012-11-15 04:16:19 +00:00
|
|
|
redir_to = redir_to.replace("%(uri)s", urllib.quote(upload_results.get_uri()))
|
2008-05-19 19:57:04 +00:00
|
|
|
return url.URL.fromString(redir_to)
|
|
|
|
d.addCallback(_done, when_done)
|
|
|
|
else:
|
|
|
|
# return the Upload Results page, which includes the URI
|
2009-02-20 19:15:54 +00:00
|
|
|
d.addCallback(UploadResultsPage)
|
2008-05-19 19:57:04 +00:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
class UploadResultsPage(status.UploadResultsRendererMixin, rend.Page):
|
2008-03-05 22:12:42 +00:00
|
|
|
"""'POST /uri', to create an unlinked file."""
|
|
|
|
docFactory = getxmlfile("upload-results.xhtml")
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def __init__(self, upload_results):
|
2008-03-05 22:12:42 +00:00
|
|
|
rend.Page.__init__(self)
|
2008-05-19 19:57:04 +00:00
|
|
|
self.results = upload_results
|
2008-03-05 22:12:42 +00:00
|
|
|
|
|
|
|
def upload_results(self):
|
2008-05-19 19:57:04 +00:00
|
|
|
return defer.succeed(self.results)
|
2008-03-05 22:12:42 +00:00
|
|
|
|
|
|
|
def data_done(self, ctx, data):
|
|
|
|
d = self.upload_results()
|
|
|
|
d.addCallback(lambda res: "done!")
|
|
|
|
return d
|
|
|
|
|
|
|
|
def data_uri(self, ctx, data):
|
|
|
|
d = self.upload_results()
|
2012-05-22 04:14:44 +00:00
|
|
|
d.addCallback(lambda res: res.get_uri())
|
2008-03-05 22:12:42 +00:00
|
|
|
return d
|
|
|
|
|
|
|
|
def render_download_link(self, ctx, data):
|
|
|
|
d = self.upload_results()
|
2012-05-22 04:14:44 +00:00
|
|
|
d.addCallback(lambda res:
|
|
|
|
T.a(href="/uri/" + urllib.quote(res.get_uri()))
|
|
|
|
["/uri/" + res.get_uri()])
|
2008-03-05 22:12:42 +00:00
|
|
|
return d
|
|
|
|
|
2011-08-07 00:43:48 +00:00
|
|
|
def POSTUnlinkedSSK(req, client, version):
|
2008-05-19 19:57:04 +00:00
|
|
|
# "POST /uri", to create an unlinked file.
|
|
|
|
# SDMF: files are small, and we can only upload data
|
2011-08-07 00:43:48 +00:00
|
|
|
contents = req.fields["file"].file
|
|
|
|
data = MutableFileHandle(contents)
|
|
|
|
d = client.create_mutable_file(data, version=version)
|
2008-05-19 19:57:04 +00:00
|
|
|
d.addCallback(lambda n: n.get_uri())
|
|
|
|
return d
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def POSTUnlinkedCreateDirectory(req, client):
|
2009-10-26 01:13:21 +00:00
|
|
|
# "POST /uri?t=mkdir", to create an unlinked directory.
|
2010-01-21 06:50:52 +00:00
|
|
|
ct = req.getHeader("content-type") or ""
|
|
|
|
if not ct.startswith("multipart/form-data"):
|
|
|
|
# guard against accidental attempts to call t=mkdir as if it were
|
|
|
|
# t=mkdir-with-children, but make sure we tolerate the usual HTML
|
|
|
|
# create-directory form (in which the t=mkdir and redirect_to_result=
|
|
|
|
# and other arguments can be passed encoded as multipath/form-data,
|
|
|
|
# in the request body).
|
|
|
|
req.content.seek(0)
|
|
|
|
kids_json = req.content.read()
|
|
|
|
if kids_json:
|
|
|
|
raise WebError("t=mkdir does not accept children=, "
|
|
|
|
"try t=mkdir-with-children instead",
|
|
|
|
http.BAD_REQUEST)
|
2011-10-13 16:29:51 +00:00
|
|
|
file_format = get_format(req, None)
|
|
|
|
if file_format == "CHK":
|
|
|
|
raise WebError("format=CHK not currently accepted for POST /uri?t=mkdir",
|
|
|
|
http.BAD_REQUEST)
|
|
|
|
mt = None
|
|
|
|
if file_format:
|
|
|
|
mt = get_mutable_type(file_format)
|
|
|
|
d = client.create_dirnode(version=mt)
|
2009-10-26 01:13:21 +00:00
|
|
|
redirect = get_arg(req, "redirect_to_result", "false")
|
|
|
|
if boolean_of_arg(redirect):
|
|
|
|
def _then_redir(res):
|
|
|
|
new_url = "uri/" + urllib.quote(res.get_uri())
|
|
|
|
req.setResponseCode(http.SEE_OTHER) # 303
|
|
|
|
req.setHeader('location', new_url)
|
|
|
|
req.finish()
|
|
|
|
return ''
|
|
|
|
d.addCallback(_then_redir)
|
|
|
|
else:
|
|
|
|
d.addCallback(lambda dirnode: dirnode.get_uri())
|
|
|
|
return d
|
|
|
|
|
|
|
|
def POSTUnlinkedCreateDirectoryWithChildren(req, client):
|
2008-05-19 19:57:04 +00:00
|
|
|
# "POST /uri?t=mkdir", to create an unlinked directory.
|
2009-11-18 07:09:00 +00:00
|
|
|
req.content.seek(0)
|
|
|
|
kids_json = req.content.read()
|
2009-10-26 01:13:21 +00:00
|
|
|
kids = convert_children_json(client.nodemaker, kids_json)
|
2009-10-17 19:28:29 +00:00
|
|
|
d = client.create_dirnode(initial_children=kids)
|
2008-05-19 19:57:04 +00:00
|
|
|
redirect = get_arg(req, "redirect_to_result", "false")
|
|
|
|
if boolean_of_arg(redirect):
|
|
|
|
def _then_redir(res):
|
|
|
|
new_url = "uri/" + urllib.quote(res.get_uri())
|
|
|
|
req.setResponseCode(http.SEE_OTHER) # 303
|
|
|
|
req.setHeader('location', new_url)
|
|
|
|
req.finish()
|
|
|
|
return ''
|
|
|
|
d.addCallback(_then_redir)
|
|
|
|
else:
|
|
|
|
d.addCallback(lambda dirnode: dirnode.get_uri())
|
|
|
|
return d
|
2008-03-05 22:12:42 +00:00
|
|
|
|
2009-11-18 07:09:00 +00:00
|
|
|
def POSTUnlinkedCreateImmutableDirectory(req, client):
|
|
|
|
# "POST /uri?t=mkdir", to create an unlinked directory.
|
|
|
|
req.content.seek(0)
|
|
|
|
kids_json = req.content.read()
|
|
|
|
kids = convert_children_json(client.nodemaker, kids_json)
|
|
|
|
d = client.create_immutable_dirnode(kids)
|
|
|
|
redirect = get_arg(req, "redirect_to_result", "false")
|
|
|
|
if boolean_of_arg(redirect):
|
|
|
|
def _then_redir(res):
|
|
|
|
new_url = "uri/" + urllib.quote(res.get_uri())
|
|
|
|
req.setResponseCode(http.SEE_OTHER) # 303
|
|
|
|
req.setHeader('location', new_url)
|
|
|
|
req.finish()
|
|
|
|
return ''
|
|
|
|
d.addCallback(_then_redir)
|
|
|
|
else:
|
|
|
|
d.addCallback(lambda dirnode: dirnode.get_uri())
|
|
|
|
return d
|