4 from zope.interface import implements
5 from twisted.internet.interfaces import IConsumer
6 from twisted.web import http, static, resource, server
7 from twisted.internet import defer
8 from nevow import url, rend
9 from nevow.inevow import IRequest
11 from allmydata.upload import FileHandle
12 from allmydata.interfaces import IDownloadTarget, ExistingChildError
13 from allmydata.mutable.common import MODE_READ
14 from allmydata.util import log
16 from allmydata.web.common import text_plain, WebError, IClient, RenderMixin, \
17 boolean_of_arg, get_arg, should_create_intermediate_directories
21 def replace_me_with_a_child(self, ctx, replace):
22 # a new file is being uploaded in our place.
25 mutable = boolean_of_arg(get_arg(req, "mutable", "false"))
28 data = req.content.read()
29 d = client.create_mutable_file(data)
30 def _uploaded(newnode):
31 d2 = self.parentnode.set_node(self.name, newnode,
33 d2.addCallback(lambda res: newnode)
35 d.addCallback(_uploaded)
37 uploadable = FileHandle(req.content, convergence=client.convergence)
38 d = self.parentnode.add_file(self.name, uploadable,
41 log.msg("webish upload complete",
42 facility="tahoe.webish", level=log.NOISY)
44 # we've replaced an existing file (or modified a mutable
45 # file), so the response code is 200
46 req.setResponseCode(http.OK)
48 # we've created a new file, so the code is 201
49 req.setResponseCode(http.CREATED)
50 return filenode.get_uri()
54 def replace_me_with_a_childcap(self, ctx, replace):
57 childcap = req.content.read()
59 childnode = client.create_node_from_uri(childcap)
60 d = self.parentnode.set_node(self.name, childnode, overwrite=replace)
61 d.addCallback(lambda res: childnode.get_uri())
64 def _read_data_from_formpost(self, req):
65 # SDMF: files are small, and we can only upload data, so we read
66 # the whole file into memory before uploading.
67 contents = req.fields["file"]
69 data = contents.file.read()
72 def replace_me_with_a_formpost(self, ctx, replace):
73 # create a new file, maybe mutable, maybe immutable
76 mutable = boolean_of_arg(get_arg(req, "mutable", "false"))
79 data = self._read_data_from_formpost(req)
80 d = client.create_mutable_file(data)
81 def _uploaded(newnode):
82 d2 = self.parentnode.set_node(self.name, newnode,
84 d2.addCallback(lambda res: newnode.get_uri())
86 d.addCallback(_uploaded)
88 # create an immutable file
89 contents = req.fields["file"]
90 uploadable = FileHandle(contents.file, convergence=client.convergence)
91 d = self.parentnode.add_file(self.name, uploadable, overwrite=replace)
92 d.addCallback(lambda newnode: newnode.get_uri())
95 class PlaceHolderNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin):
96 def __init__(self, parentnode, name):
97 rend.Page.__init__(self)
99 self.parentnode = parentnode
103 def render_PUT(self, ctx):
105 t = get_arg(req, "t", "").strip()
106 replace = boolean_of_arg(get_arg(req, "replace", "true"))
107 assert self.parentnode and self.name
109 return self.replace_me_with_a_child(ctx, replace)
111 return self.replace_me_with_a_childcap(ctx, replace)
113 raise WebError("PUT to a file: bad t=%s" % t)
115 def render_POST(self, ctx):
117 t = get_arg(req, "t", "").strip()
118 replace = boolean_of_arg(get_arg(req, "replace", "true"))
120 # like PUT, but get the file data from an HTML form's input field.
121 # We could get here from POST /uri/mutablefilecap?t=upload,
122 # or POST /uri/path/file?t=upload, or
123 # POST /uri/path/dir?t=upload&name=foo . All have the same
124 # behavior, we just ignore any name= argument
125 d = self.replace_me_with_a_formpost(ctx, replace)
127 # t=mkdir is handled in DirectoryNodeHandler._POST_mkdir, so
128 # there are no other t= values left to be handled by the
130 raise WebError("POST to a file: bad t=%s" % t)
132 when_done = get_arg(req, "when_done", None)
134 d.addCallback(lambda res: url.URL.fromString(when_done))
138 class FileNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin):
139 def __init__(self, node, parentnode=None, name=None):
140 rend.Page.__init__(self)
143 self.parentnode = parentnode
146 def childFactory(self, ctx, name):
148 if should_create_intermediate_directories(req):
149 raise WebError("Cannot create directory '%s', because its "
150 "parent is a file, not a directory" % name)
151 raise WebError("Files have no children, certainly not named '%s'"
154 def render_GET(self, ctx):
156 t = get_arg(req, "t", "").strip()
158 # just get the contents
159 filename = get_arg(req, "filename", self.name) or "unknown"
160 save_to_file = boolean_of_arg(get_arg(req, "save", "False"))
161 return FileDownloader(self.node, filename, save_to_file)
163 return FileJSONMetadata(ctx, self.node)
165 return FileURI(ctx, self.node)
166 if t == "readonly-uri":
167 return FileReadOnlyURI(ctx, self.node)
168 raise WebError("GET file: bad t=%s" % t)
170 def render_HEAD(self, ctx):
172 t = get_arg(req, "t", "").strip()
174 raise WebError("GET file: bad t=%s" % t)
175 if self.node.is_mutable():
176 # update the servermap to get the size of this file without
177 # downloading the full contents.
178 d = self.node.get_servermap(MODE_READ)
179 def _got_servermap(smap):
180 ver = smap.best_recoverable_version()
182 raise WebError("Unable to recover this file",
184 length = smap.size_of_version(ver)
186 d.addCallback(_got_servermap)
187 # otherwise, we can get the size from the URI
189 d = defer.succeed(self.node.get_size())
190 def _got_length(length):
191 req.setHeader("content-length", length)
193 d.addCallback(_got_length)
196 def render_PUT(self, ctx):
198 t = get_arg(req, "t", "").strip()
199 replace = boolean_of_arg(get_arg(req, "replace", "true"))
201 if self.node.is_mutable():
202 return self.replace_my_contents(ctx)
204 # this is the early trap: if someone else modifies the
205 # directory while we're uploading, the add_file(overwrite=)
206 # call in replace_me_with_a_child will do the late trap.
207 raise ExistingChildError()
208 assert self.parentnode and self.name
209 return self.replace_me_with_a_child(ctx, replace)
212 raise ExistingChildError()
213 assert self.parentnode and self.name
214 return self.replace_me_with_a_childcap(ctx, replace)
216 raise WebError("PUT to a file: bad t=%s" % t)
218 def render_POST(self, ctx):
220 t = get_arg(req, "t", "").strip()
221 replace = boolean_of_arg(get_arg(req, "replace", "true"))
223 d = self._POST_check(req)
225 # like PUT, but get the file data from an HTML form's input field
226 # We could get here from POST /uri/mutablefilecap?t=upload,
227 # or POST /uri/path/file?t=upload, or
228 # POST /uri/path/dir?t=upload&name=foo . All have the same
229 # behavior, we just ignore any name= argument
230 if self.node.is_mutable():
231 d = self.replace_my_contents_with_a_formpost(ctx)
234 raise ExistingChildError()
235 assert self.parentnode and self.name
236 d = self.replace_me_with_a_formpost(ctx, replace)
238 raise WebError("POST to file: bad t=%s" % t)
240 when_done = get_arg(req, "when_done", None)
242 d.addCallback(lambda res: url.URL.fromString(when_done))
245 def _POST_check(self, req):
246 d = self.node.check()
248 log.msg("checked %s, results %s" % (self.node, res),
249 facility="tahoe.webish", level=log.NOISY)
255 def render_DELETE(self, ctx):
256 assert self.parentnode and self.name
257 d = self.parentnode.delete(self.name)
258 d.addCallback(lambda res: self.node.get_uri())
261 def replace_my_contents(self, ctx):
264 new_contents = req.content.read()
265 d = self.node.overwrite(new_contents)
266 d.addCallback(lambda res: self.node.get_uri())
269 def replace_my_contents_with_a_formpost(self, ctx):
270 # we have a mutable file. Get the data from the formpost, and replace
271 # the mutable file's contents with it.
273 new_contents = self._read_data_from_formpost(req)
274 d = self.node.overwrite(new_contents)
275 d.addCallback(lambda res: self.node.get_uri())
279 class WebDownloadTarget:
280 implements(IDownloadTarget, IConsumer)
281 def __init__(self, req, content_type, content_encoding, save_to_filename):
283 self._content_type = content_type
284 self._content_encoding = content_encoding
286 self._producer = None
287 self._save_to_filename = save_to_filename
289 def registerProducer(self, producer, streaming):
290 self._req.registerProducer(producer, streaming)
291 def unregisterProducer(self):
292 self._req.unregisterProducer()
294 def open(self, size):
296 self._req.setHeader("content-type", self._content_type)
297 if self._content_encoding:
298 self._req.setHeader("content-encoding", self._content_encoding)
299 self._req.setHeader("content-length", str(size))
300 if self._save_to_filename is not None:
301 # tell the browser to save the file rather display it
302 # TODO: indicate charset of filename= properly
303 filename = self._save_to_filename.encode("utf-8")
304 self._req.setHeader("content-disposition",
305 'attachment; filename="%s"'
308 def write(self, data):
309 self._req.write(data)
315 # The content-type is already set, and the response code
316 # has already been sent, so we can't provide a clean error
317 # indication. We can emit text (which a browser might interpret
318 # as something else), and if we sent a Size header, they might
319 # notice that we've truncated the data. Keep the error message
320 # small to improve the chances of having our error response be
321 # shorter than the intended results.
323 # We don't have a lot of options, unfortunately.
324 self._req.write("problem during download\n")
326 # We haven't written anything yet, so we can provide a sensible
329 msg.replace("\n", "|")
330 self._req.setResponseCode(http.GONE, msg)
331 self._req.setHeader("content-type", "text/plain")
332 # TODO: HTML-formatted exception?
333 self._req.write(str(why))
336 def register_canceller(self, cb):
341 class FileDownloader(resource.Resource):
342 # since we override the rendering process (to let the tahoe Downloader
343 # drive things), we must inherit from regular old twisted.web.resource
344 # instead of nevow.rend.Page . Nevow will use adapters to wrap a
345 # nevow.appserver.OldResourceAdapter around any
346 # twisted.web.resource.IResource that it is given. TODO: it looks like
347 # that wrapper would allow us to return a Deferred from render(), which
348 # might could simplify the implementation of WebDownloadTarget.
350 def __init__(self, filenode, filename, save_to_file):
351 resource.Resource.__init__(self)
352 self.filenode = filenode
353 self.filename = filename
354 self.save_to_file = save_to_file
355 def render(self, req):
356 gte = static.getTypeAndEncoding
357 ctype, encoding = gte(self.filename,
358 static.File.contentTypes,
359 static.File.contentEncodings,
360 defaultType="text/plain")
361 save_to_filename = None
362 if self.save_to_file:
363 save_to_filename = self.filename
364 wdt = WebDownloadTarget(req, ctype, encoding, save_to_filename)
365 d = self.filenode.download(wdt)
366 # exceptions during download are handled by the WebDownloadTarget
367 d.addErrback(lambda why: None)
368 return server.NOT_DONE_YET
370 def FileJSONMetadata(ctx, filenode):
371 if filenode.is_readonly():
373 ro_uri = filenode.get_uri()
375 rw_uri = filenode.get_uri()
376 ro_uri = filenode.get_readonly_uri()
377 data = ("filenode", {})
378 data[1]['size'] = filenode.get_size()
380 data[1]['ro_uri'] = ro_uri
382 data[1]['rw_uri'] = rw_uri
383 return text_plain(simplejson.dumps(data, indent=1), ctx)
385 def FileURI(ctx, filenode):
386 return text_plain(filenode.get_uri(), ctx)
388 def FileReadOnlyURI(ctx, filenode):
389 if filenode.is_readonly():
390 return text_plain(filenode.get_uri(), ctx)
391 return text_plain(filenode.get_readonly_uri(), ctx)
393 class FileNodeDownloadHandler(FileNodeHandler):
394 def childFactory(self, ctx, name):
395 return FileNodeDownloadHandler(self.node, name=name)