The idea is, I would like to detect and abort an upload where the file
size is over a certain limit, or if it's not a video file. Now with
streaming uploads, this should be possible. I've written an upload
handler:
class UploadValidationHandler(FileUploadHandler):
def handle_raw_input(self, input_data, META, content_length,
boundary, encoding):
self.content_length = content_length
def new_file(self, field_name, file_name, content_type,
content_length, charset):
# check that the file size is within the limit
from django.conf import settings
max_bytes = settings.MAX_FILM_MB * 1024 * 1024
if self.content_length > max_bytes:
# UploadValidationError is a subclass of
django.core.files.uploadhandler.StopUpload
raise UploadValidationError("The file is larger than %d
MB" % settings.MAX_FILM_MB)
# check that it is a video file
import re
print content_type
if not re.search("video", content_type):
raise UploadValidationError("The file is not a video
file.")
def receive_data_chunk(self, raw_data, start):
return raw_data
def file_complete(self, file_size):
pass
In my view I wrap
request.upload_handlers.append(UploadValidationHandler(request)) with
a try-except, and if I catch a StopUpload exception, I add a message
to the form's validation errors and render the form page to show the
user.
At first when I was testing this, I was sure it was working, because
there seemed to be no significant time lapse between sending the file
and getting the page back with the error message. Unfortunately, this
isn't what happened once I tested from another machine (silly me,
trying to upload to my own machine). Now it takes a very long time for
the request to finish and to get back the form with the error... long
enough so that I think the file is actually completely uploaded first,
and THEN the StopUpload exception gets raised. But I need it to be
instantaneous... what's going on?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---