Permalink
Browse files

Add FilepickerFile helper and use in middleware

Avoids reading the complete file in memory
  • Loading branch information...
1 parent ce2b999 commit 1bc2007d87609a8588dade1de258e70bac16bcf5 @jonasvp jonasvp committed Oct 5, 2012
Showing with 37 additions and 25 deletions.
  1. +9 −25 django_filepicker/middleware.py
  2. +28 −0 django_filepicker/utils.py
@@ -1,11 +1,4 @@
-import re
-import urllib2
-from django.core.files import File
-
-try:
- from cStringIO import StringIO
-except ImportError:
- from StringIO import StringIO
+from .utils import FilepickerFile
class URLFileMapperMiddleware(object):
@@ -20,27 +13,18 @@ class URLFileMapperMiddleware(object):
Note that the original filepicker.io url will still be available in POST if you need it.
"""
- filepicker_url_regex = re.compile(
- r'https?:\/\/www.filepicker.io\/api\/file\/.*')
-
def process_request(self, request):
#Iterate over GET or POST data, search for filepicker.io urls
for key, val in request.POST.items():
- if self.isFilepickerURL(val):
+ try:
+ fp = FilepickerFile(val)
+ except ValueError:
+ pass
+ else:
splits = val.split(",")
for url in splits:
- url_fp = urllib2.urlopen(url)
- disposition = url_fp.info().getheader('Content-Disposition')
- if disposition:
- name = disposition.rpartition("filename=")[2].strip('" ')
- else:
- name = "fp-file"
-
- fp = File(StringIO(url_fp.read()), name=name)
if key in request.FILES:
- request.FILES.setlist(key, list(request.FILES.getlist(key) + [fp]))
+ request.FILES.setlist(key, list(
+ request.FILES.getlist(key) + [fp.get_file()]))
else:
- request.FILES[key] = fp
-
- def isFilepickerURL(self, val):
- return bool(self.filepicker_url_regex.match(val))
+ request.FILES[key] = fp.get_file()
View
@@ -0,0 +1,28 @@
+import re
+import urllib
+from os.path import basename
+
+from django.core.files import File
+
+
+class FilepickerFile(object):
+ filepicker_url_regex = re.compile(
+ r'https?:\/\/www.filepicker.io\/api\/file\/.*')
+
+ def __init__(self, url):
+ if not self.filepicker_url_regex.match(url):
+ raise ValueError('Not a filepicker.io URL: %s' % url)
+ self.url = url
+
+ def get_file(self):
+ '''
+ Downloads the file from filepicker.io and returns a
+ Django File wrapper object
+ '''
+ filename, header = urllib.urlretrieve(self.url)
+ name = basename(filename)
+ disposition = header.get('Content-Disposition')
+ if disposition:
+ name = disposition.rpartition("filename=")[2].strip('" ')
+
+ return File(open(filename, 'r'), name=name)

0 comments on commit 1bc2007

Please sign in to comment.