Ticket #10902: handlers.py

File handlers.py, 1.6 KB (added by tkopczuk@…, 16 years ago)
Line 
1try:
2 from cStringIO import StringIO
3except ImportError:
4 from StringIO import StringIO
5
6from django.core.files.uploadhandler import FileUploadHandler, SkipFile, StopUpload, StopFutureHandlers
7import ImageFile
8
9IMAGE_MAX_SIZE = 1024*1024
10
11class ImageFileUploadHandler(FileUploadHandler):
12 def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None):
13 print "<IFUH>: Got new file of size: ", content_length
14
15 if content_length > IMAGE_MAX_SIZE:
16 print "<IFUH>: And it exceeds ", IMAGE_MAX_SIZE
17 raise SkipFile
18
19 def new_file(self, *args, **kwargs):
20 super(ImageFileUploadHandler, self).new_file(*args, **kwargs)
21
22 print "<IFUH>: Started new upload."
23 self.file = ImageFile.Parser()
24 raise StopFutureHandlers()
25
26 def receive_data_chunk(self, raw_data, start):
27 print "<IFUH>: Got new data chunk.", start, start+len(raw_data)
28 if start+len(raw_data) > IMAGE_MAX_SIZE:
29 print "<IFUH>: Image exceeds ", IMAGE_MAX_SIZE
30 raise StopUpload
31 try:
32 self.file.feed(raw_data)
33 print "<IFUH>: It seems all right."
34 except IOError:
35 print "<IFUH>: It's not all right."
36 raise StopUpload
37
38 return None
39
40 def file_complete(self, file_size):
41 print "<IFUH>: Upload completed."
42 try:
43 self.file = self.file.close()
44 print "<IFUH>: Valid image upload completed."
45 except IOError:
46 print "<IFUH>: Image upload completed, it's not valid."
47 return None
48
49 return self.file
Back to Top