2013-07-29 19:19:04 +02:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 20:51:33 +02:00
|
|
|
|
2015-07-01 23:37:10 +02:00
|
|
|
import contextlib
|
2011-03-28 04:11:19 +02:00
|
|
|
import hashlib
|
2012-04-29 19:58:00 +02:00
|
|
|
import json
|
2008-07-01 17:10:51 +02:00
|
|
|
import os
|
2011-10-13 20:51:33 +02:00
|
|
|
|
2008-07-01 17:10:51 +02:00
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
|
|
|
from django.http import HttpResponse, HttpResponseServerError
|
2012-07-20 14:48:51 +02:00
|
|
|
from django.utils import six
|
2013-04-19 19:20:23 +02:00
|
|
|
from django.utils.encoding import force_bytes, smart_str
|
2011-10-13 20:51:33 +02:00
|
|
|
|
2012-10-30 22:20:42 +01:00
|
|
|
from .models import FileModel
|
|
|
|
from .tests import UNICODE_FILENAME, UPLOAD_TO
|
2015-01-28 13:35:27 +01:00
|
|
|
from .uploadhandler import ErroringUploadHandler, QuotaUploadHandler
|
2011-10-13 20:51:33 +02:00
|
|
|
|
2008-07-01 17:10:51 +02:00
|
|
|
|
|
|
|
def file_upload_view(request):
|
|
|
|
"""
|
|
|
|
Check that a file upload can be updated into the POST dictionary without
|
|
|
|
going pear-shaped.
|
|
|
|
"""
|
|
|
|
form_data = request.POST.copy()
|
|
|
|
form_data.update(request.FILES)
|
2012-07-20 14:48:51 +02:00
|
|
|
if isinstance(form_data.get('file_field'), UploadedFile) and isinstance(form_data['name'], six.text_type):
|
2008-07-01 17:10:51 +02:00
|
|
|
# If a file is posted, the dummy client should only post the file name,
|
|
|
|
# not the full path.
|
2008-07-08 01:16:00 +02:00
|
|
|
if os.path.dirname(form_data['file_field'].name) != '':
|
2010-03-02 22:58:49 +01:00
|
|
|
return HttpResponseServerError()
|
2008-07-01 17:10:51 +02:00
|
|
|
return HttpResponse('')
|
|
|
|
else:
|
|
|
|
return HttpResponseServerError()
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2008-07-01 17:10:51 +02:00
|
|
|
def file_upload_view_verify(request):
|
|
|
|
"""
|
|
|
|
Use the sha digest hash to verify the uploaded contents.
|
|
|
|
"""
|
|
|
|
form_data = request.POST.copy()
|
|
|
|
form_data.update(request.FILES)
|
|
|
|
|
|
|
|
for key, value in form_data.items():
|
|
|
|
if key.endswith('_hash'):
|
|
|
|
continue
|
|
|
|
if key + '_hash' not in form_data:
|
|
|
|
continue
|
|
|
|
submitted_hash = form_data[key + '_hash']
|
|
|
|
if isinstance(value, UploadedFile):
|
2011-03-28 04:11:19 +02:00
|
|
|
new_hash = hashlib.sha1(value.read()).hexdigest()
|
2008-07-01 17:10:51 +02:00
|
|
|
else:
|
2012-08-28 20:59:56 +02:00
|
|
|
new_hash = hashlib.sha1(force_bytes(value)).hexdigest()
|
2008-07-01 17:10:51 +02:00
|
|
|
if new_hash != submitted_hash:
|
|
|
|
return HttpResponseServerError()
|
|
|
|
|
2008-08-23 19:56:02 +02:00
|
|
|
# Adding large file to the database should succeed
|
|
|
|
largefile = request.FILES['file_field2']
|
|
|
|
obj = FileModel()
|
|
|
|
obj.testfile.save(largefile.name, largefile)
|
|
|
|
|
2008-07-01 17:10:51 +02:00
|
|
|
return HttpResponse('')
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2009-04-04 19:34:58 +02:00
|
|
|
def file_upload_unicode_name(request):
|
|
|
|
|
|
|
|
# Check to see if unicode name came through properly.
|
|
|
|
if not request.FILES['file_unicode'].name.endswith(UNICODE_FILENAME):
|
|
|
|
return HttpResponseServerError()
|
|
|
|
|
|
|
|
response = None
|
|
|
|
|
|
|
|
# Check to make sure the exotic characters are preserved even
|
|
|
|
# through file save.
|
|
|
|
uni_named_file = request.FILES['file_unicode']
|
|
|
|
obj = FileModel.objects.create(testfile=uni_named_file)
|
2012-06-07 18:08:47 +02:00
|
|
|
full_name = '%s/%s' % (UPLOAD_TO, uni_named_file.name)
|
2010-03-02 22:58:49 +01:00
|
|
|
if not os.path.exists(full_name):
|
2009-04-04 19:34:58 +02:00
|
|
|
response = HttpResponseServerError()
|
|
|
|
|
|
|
|
# Cleanup the object with its exotic file name immediately.
|
|
|
|
# (shutil.rmtree used elsewhere in the tests to clean up the
|
|
|
|
# upload directory has been seen to choke on unicode
|
|
|
|
# filenames on Windows.)
|
|
|
|
obj.delete()
|
2011-02-21 13:35:19 +01:00
|
|
|
os.unlink(full_name)
|
2009-04-04 19:34:58 +02:00
|
|
|
|
|
|
|
if response:
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
return HttpResponse('')
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2008-07-01 17:10:51 +02:00
|
|
|
def file_upload_echo(request):
|
|
|
|
"""
|
|
|
|
Simple view to echo back info about uploaded files for tests.
|
|
|
|
"""
|
2014-12-06 22:00:09 +01:00
|
|
|
r = {k: f.name for k, f in request.FILES.items()}
|
2012-04-29 19:58:00 +02:00
|
|
|
return HttpResponse(json.dumps(r))
|
2010-03-02 22:58:49 +01:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2011-05-07 18:59:16 +02:00
|
|
|
def file_upload_echo_content(request):
|
|
|
|
"""
|
|
|
|
Simple view to echo back the content of uploaded files for tests.
|
|
|
|
"""
|
2015-07-01 23:37:10 +02:00
|
|
|
def read_and_close(f):
|
|
|
|
with contextlib.closing(f):
|
|
|
|
return f.read().decode('utf-8')
|
|
|
|
r = {k: read_and_close(f) for k, f in request.FILES.items()}
|
2012-04-29 19:58:00 +02:00
|
|
|
return HttpResponse(json.dumps(r))
|
2011-05-07 18:59:16 +02:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2008-07-01 17:10:51 +02:00
|
|
|
def file_upload_quota(request):
|
|
|
|
"""
|
|
|
|
Dynamically add in an upload handler.
|
|
|
|
"""
|
|
|
|
request.upload_handlers.insert(0, QuotaUploadHandler())
|
|
|
|
return file_upload_echo(request)
|
2010-03-02 22:58:49 +01:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2008-07-01 17:10:51 +02:00
|
|
|
def file_upload_quota_broken(request):
|
|
|
|
"""
|
|
|
|
You can't change handlers after reading FILES; this view shouldn't work.
|
|
|
|
"""
|
|
|
|
response = file_upload_echo(request)
|
|
|
|
request.upload_handlers.insert(0, QuotaUploadHandler())
|
2008-07-08 00:06:32 +02:00
|
|
|
return response
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2008-07-08 00:06:32 +02:00
|
|
|
def file_upload_getlist_count(request):
|
|
|
|
"""
|
|
|
|
Check the .getlist() function to ensure we receive the correct number of files.
|
|
|
|
"""
|
|
|
|
file_counts = {}
|
|
|
|
|
|
|
|
for key in request.FILES.keys():
|
|
|
|
file_counts[key] = len(request.FILES.getlist(key))
|
2012-04-29 19:58:00 +02:00
|
|
|
return HttpResponse(json.dumps(file_counts))
|
2008-08-30 21:56:14 +02:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2008-08-30 21:56:14 +02:00
|
|
|
def file_upload_errors(request):
|
|
|
|
request.upload_handlers.insert(0, ErroringUploadHandler())
|
|
|
|
return file_upload_echo(request)
|
2011-05-23 01:56:42 +02:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2011-05-23 01:56:42 +02:00
|
|
|
def file_upload_filename_case_view(request):
|
|
|
|
"""
|
|
|
|
Check adding the file to the database will preserve the filename case.
|
|
|
|
"""
|
|
|
|
file = request.FILES['file_field']
|
|
|
|
obj = FileModel()
|
|
|
|
obj.testfile.save(file.name, file)
|
|
|
|
return HttpResponse('%d' % obj.pk)
|
2013-04-19 19:20:23 +02:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2013-04-19 19:20:23 +02:00
|
|
|
def file_upload_content_type_extra(request):
|
|
|
|
"""
|
|
|
|
Simple view to echo back extra content-type parameters.
|
|
|
|
"""
|
|
|
|
params = {}
|
|
|
|
for file_name, uploadedfile in request.FILES.items():
|
2014-12-06 22:00:09 +01:00
|
|
|
params[file_name] = {
|
|
|
|
k: smart_str(v) for k, v in uploadedfile.content_type_extra.items()
|
|
|
|
}
|
2013-04-19 19:20:23 +02:00
|
|
|
return HttpResponse(json.dumps(params))
|
2014-05-25 22:52:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
def file_upload_fd_closing(request, access):
|
|
|
|
if access == 't':
|
|
|
|
request.FILES # Trigger file parsing.
|
|
|
|
return HttpResponse('')
|