Skip to content

Commit

Permalink
Added option to skip md5 check
Browse files Browse the repository at this point in the history
Bonus: Better validation of chunk size (must match http header).
  • Loading branch information
juliomalegria committed Jan 5, 2015
1 parent 3a26f94 commit 492a3e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion chunked_upload/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
DateTimeAwareJSONEncoder as DjangoJSONEncoder
)
except ImportError:
raise ImportError('Dude! what Django version are you using?')
raise ImportError('Dude! what version of Django are you using?')


# How long after creation the upload will expire
Expand Down Expand Up @@ -42,6 +42,7 @@
DEFAULT_MIMETYPE = 'application/json'
MIMETYPE = getattr(settings, 'CHUNKED_UPLOAD_MIMETYPE', DEFAULT_MIMETYPE)


# Max amount of data (in bytes) that can be uploaded. `None` means no limit
DEFAULT_MAX_BYTES = None
MAX_BYTES = getattr(settings, 'CHUNKED_UPLOAD_MAX_BYTES', DEFAULT_MAX_BYTES)
27 changes: 21 additions & 6 deletions chunked_upload/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ def _post(self, request, *args, **kwargs):
start = int(match.group('start'))
end = int(match.group('end'))
total = int(match.group('total'))

chunk_size = end - start + 1
max_bytes = self.get_max_bytes(request)

if max_bytes is not None and total > max_bytes:
raise ChunkedUploadError(
status=http_status.HTTP_400_BAD_REQUEST,
Expand All @@ -193,8 +194,10 @@ def _post(self, request, *args, **kwargs):
raise ChunkedUploadError(status=http_status.HTTP_400_BAD_REQUEST,
detail='Offsets do not match',
offset=chunked_upload.offset)
if chunk.size != chunk_size:
raise ChunkedUploadError(status=http_status.HTTP_400_BAD_REQUEST,
detail="File size doesn't match headers")

chunk_size = end - start + 1
chunked_upload.append_chunk(chunk, chunk_size=chunk_size, save=False)

self._save(chunked_upload)
Expand All @@ -209,6 +212,10 @@ class ChunkedUploadCompleteView(ChunkedUploadBaseView):
define what to do when upload is complete.
"""

# I wouldn't recommend to turn off the md5 check, unless is really
# impacting your performance. Proceed at your own risk.
do_md5_check = True

def on_completion(self, uploaded_file, request):
"""
Placeholder method to define what to do when upload is complete.
Expand Down Expand Up @@ -236,16 +243,24 @@ def md5_check(self, chunked_upload, md5):
def _post(self, request, *args, **kwargs):
upload_id = request.POST.get('upload_id')
md5 = request.POST.get('md5')
if not upload_id or not md5:
error_msg = "Both 'upload_id' and 'md5' are required"

error_msg = None
if self.do_md5_check:
if not upload_id or not md5:
error_msg = "Both 'upload_id' and 'md5' are required"
elif not upload_id:
error_msg = "'upload_id' is required"
if error_msg:
raise ChunkedUploadError(status=http_status.HTTP_400_BAD_REQUEST,
detail=error_msg)
self.validate(request)

chunked_upload = get_object_or_404(self.get_queryset(request),
upload_id=upload_id)

self.validate(request)
self.is_valid_chunked_upload(chunked_upload)
self.md5_check(chunked_upload, md5)
if self.do_md5_check:
self.md5_check(chunked_upload, md5)

chunked_upload.status = COMPLETE
chunked_upload.completed_on = timezone.now()
Expand Down

0 comments on commit 492a3e6

Please sign in to comment.