Permalink
Browse files

added multifile upload support

  • Loading branch information...
1 parent 1c0fe35 commit bdbb1330925d260145988259795c11e127765097 Mark A. Valentin committed Mar 24, 2012
Showing with 41 additions and 40 deletions.
  1. +40 −39 image/views.py
  2. +1 −1 templates/upload.html
View
@@ -15,26 +15,54 @@
from image.base62 import base62
import settings
+def image_handler(files, request):
+ tmp = tempfile.mkstemp()
+ md5 = hashlib.md5()
+ orig = files.name
+ fext = orig[orig.rfind('.')+1:]
+ f = os.fdopen(tmp[0], "wb+")
+ for chunk in files.chunks():
+ f.write(chunk)
+ md5.update(chunk)
+ f.close()
+
+ img = Image()
+ try:
+ next_id = Image.objects.order_by('-id')[0].id + 1
+ except IndexError:
+ next_id = settings.IMAGE_ID_OFFSET + 1
+
+ img.id = next_id
+ img.base62 = base62(next_id)
+ img.filename = base62(next_id) + "." + fext.lower()
+ img.orig_filename = orig
+ img.type = '' # todo
+ img.description = '' # not implemented yet.
+ img.uploader = request.user
+ img.md5sum = md5.hexdigest()
+ image_file = os.path.join(settings.MEDIA_ROOT,img.filename)
+ thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename)
+
+ try:
+ img.save()
+ except IntegrityError, e:
+ os.unlink(tmp[1]) # delete the uploaded file if it already exists
+ return HttpResponseRedirect( settings.MEDIA_URL + Image.objects.get(md5sum=img.md5sum).filename)
+ shutil.move(tmp[1], image_file)
+ os.system("/usr/bin/convert %s -thumbnail 150x150 %s" % (image_file, thumbnail))
+
@login_required
def upload(request):
if request.method == 'GET':
return render_to_response('upload.html', {'url': request.GET.get('url', ''),}, context_instance=RequestContext(request))
elif request.method == 'POST':
tmp = tempfile.mkstemp()
- md5 = hashlib.md5()
fext = ""
orig = ""
-
- if request.POST['upload_type'] == 'file':
- file = request.FILES['upload_file']
- fext = file.name[-3:]
- orig = file.name
- f = os.fdopen(tmp[0], "wb+")
- for chunk in file.chunks():
- f.write(chunk)
- md5.update(chunk)
- f.close()
+ if request.POST['upload_type'] == 'file':
+ for files in request.FILES.getlist('upload_file'):
+ image_handler(files, request)
elif request.POST['upload_type'] == 'url':
remote_image = urllib2.urlopen(request.POST['upload_url'])
data = remote_image.read()
@@ -45,34 +73,7 @@ def upload(request):
f = os.fdopen(tmp[0], "wb+")
f.write(data)
f.close()
-
- img = Image()
- try:
- next_id = Image.objects.order_by('-id')[0].id + 1
- except IndexError:
- next_id = settings.IMAGE_ID_OFFSET + 1
-
- img.id = next_id
- img.base62 = base62(next_id)
- img.filename = base62(next_id) + "." + fext.lower()
- img.orig_filename = orig
- img.type = '' # todo
- img.description = '' # not implemented yet.
- img.uploader = request.user
- img.md5sum = md5.hexdigest()
- image_file = os.path.join(settings.MEDIA_ROOT,img.filename)
- thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename)
-
- try:
- img.save()
- except IntegrityError, e:
- os.unlink(tmp[1]) # delete the uploaded file if it already exists
- return HttpResponseRedirect( settings.MEDIA_URL + Image.objects.get(md5sum=img.md5sum).filename)
-
- shutil.move(tmp[1], image_file)
- os.system("/usr/bin/convert %s -thumbnail 150x150 %s" % (image_file, thumbnail))
-
- return HttpResponseRedirect(settings.MEDIA_URL + img.filename)
+ return render_to_response('upload.html', {'url': request.GET.get('url', ''),}, context_instance=RequestContext(request))
@login_required
def view_image(request, id):
View
2 templates/upload.html 100644 → 100755
@@ -14,7 +14,7 @@ <h2 style="text-align:center;">Upload Image</h2>
<div class="row">
<div class="span8 offset2" style="text-align:center;">
- <input type="file" name="upload_file" />
+ <input type="file" name="upload_file" multiple="multiple" />
<div style="display:none;"><input type="text" name="upload_url" value="{{url}}" size="64" /></div>
<br />
<input type="submit" value="Upload" class="btn primary" />

0 comments on commit bdbb133

Please sign in to comment.