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
|
2009-10-13 02:34:44 +00:00
|
|
|
from allmydata.web.common import getxmlfile, get_arg, boolean_of_arg, \
|
|
|
|
convert_initial_children_json
|
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)
|
|
|
|
d.addCallback(lambda results: results.uri)
|
|
|
|
# that fires with the URI of the new file
|
|
|
|
return d
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def PUTUnlinkedSSK(req, client):
|
2008-05-19 19:57:04 +00:00
|
|
|
# SDMF: files are small, and we can only upload data
|
|
|
|
req.content.seek(0)
|
|
|
|
data = req.content.read()
|
2009-02-20 19:15:54 +00:00
|
|
|
d = client.create_mutable_file(data)
|
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.
|
2009-10-13 02:34:44 +00:00
|
|
|
req.content.seek(0)
|
|
|
|
initial_children_json = req.content.read()
|
|
|
|
initial_children = convert_initial_children_json(initial_children_json)
|
|
|
|
d = client.create_dirnode(initial_children=initial_children)
|
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:
|
|
|
|
redir_to = redir_to % {"uri": urllib.quote(upload_results.uri)
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
d.addCallback(lambda res: res.uri)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def render_download_link(self, ctx, data):
|
|
|
|
d = self.upload_results()
|
|
|
|
d.addCallback(lambda res: T.a(href="/uri/" + urllib.quote(res.uri))
|
|
|
|
["/uri/" + res.uri])
|
|
|
|
return d
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def POSTUnlinkedSSK(req, client):
|
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
|
|
|
|
contents = req.fields["file"]
|
|
|
|
contents.file.seek(0)
|
|
|
|
data = contents.file.read()
|
2009-02-20 19:15:54 +00:00
|
|
|
d = client.create_mutable_file(data)
|
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):
|
2008-05-19 19:57:04 +00:00
|
|
|
# "POST /uri?t=mkdir", to create an unlinked directory.
|
2009-10-13 02:34:44 +00:00
|
|
|
initial_json = get_arg(req, "children", "")
|
|
|
|
initial_children = convert_initial_children_json(initial_json)
|
|
|
|
d = client.create_dirnode(initial_children=initial_children)
|
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
|
|
|
|