Revision: d9f35f2ee678
Author: Roger Tawa <roge
...@chromium.org>
Date: Thu May 10 13:24:19 2012
Log: Add an endpoint to download the binary content of a patch.
Review:
http://codereview.appspot.com/6197071/
http://code.google.com/p/rietveld/source/detail?r=d9f35f2ee678
Modified:
/codereview/urls.py
/codereview/views.py
/codereview/views_chromium.py
=======================================
--- /codereview/urls.py Fri Dec 16 08:31:56 2011
+++ /codereview/urls.py Thu May 10 13:24:19 2012
@@ -102,6 +102,7 @@
urlpatterns += patterns(
'codereview.views_chromium',
(r'^(\d+)/edit_flags$', 'edit_flags'),
+ (r'^(\d+)/binary/(\d+)/(\d+)/(\d+)$', 'download_binary'),
(r'^conversions$', 'conversions'),
(r'^lint/issue(\d+)_(\d+)', 'lint'),
(r'^lint_patch/issue(\d+)_(\d+)_(\d+)', 'lint_patch'),
=======================================
--- /codereview/views.py Thu May 10 09:39:31 2012
+++ /codereview/views.py Thu May 10 13:24:19 2012
@@ -2401,7 +2401,7 @@
@image_required
def image(request):
- """/<issue>/content/<patchset>/<patch>/<content> - Return patch's
content."""
+ """/<issue>/image/<patchset>/<patch>/<content> - Return patch's
content."""
response = HttpResponse(request.content.data,
content_type=request.mime_type)
filename = re.sub(
r'[^\w\.]', '_', request.patch.filename.encode('ascii', 'replace'))
=======================================
--- /codereview/views_chromium.py Thu Apr 26 12:37:05 2012
+++ /codereview/views_chromium.py Thu May 10 13:24:19 2012
@@ -17,6 +17,7 @@
import cgi
import datetime
import logging
+import mimetypes
import os
import re
import sha
@@ -77,6 +78,32 @@
return key_wrapper
+def binary_required(func):
+ """Decorator that processes the content argument.
+
+ Attributes set on the request:
+ content: a Content entity.
+ """
+
+ @patch_required
+ def binary_wrapper(request, content_type, *args, **kwds):
+ content = None
+ if content_type == "0":
+ content = request.patch.content
+ elif content_type == "1":
+ content = request.patch.patched_content
+ # Other values are erroneous so request.content won't be set.
+ if not content or not content.data:
+ return views.HttpTextResponse(
+ 'Invalid content type: %s, expected 0 or 1' % content_type,
+ status=404)
+ request.mime_type = mimetypes.guess_type(request.patch.filename)[0]
+ request.content = content
+ return func(request, *args, **kwds)
+
+ return binary_wrapper
+
+
def string_to_datetime(text):
"""Parses a string into datetime including microseconds.
@@ -478,6 +505,21 @@
return HttpResponse('OK')
+@binary_required
+def download_binary(request):
+ """/<issue>/binary/<patchset>/<patch>/<content>
+
+ Return patch's binary content. If the patch is not binary, an empty
stream
+ is returned. <content> may be 0 for the base content or 1 for the new
+ content. All other values are invalid.
+ """
+ response = HttpResponse(request.content.data,
content_type=request.mime_type)
+ filename = re.sub(
+ r'[^\w\.]', '_', request.patch.filename.encode('ascii', 'replace'))
+ response['Content-Disposition'] = 'attachment; filename="%s"' % filename
+ return response
+
+
@views.json_response
def get_pending_try_patchsets(request):
limit = int(request.GET.get('limit', '10'))