2008-07-01 23:10:51 +08:00
|
|
|
"""
|
|
|
|
Upload handlers to test the upload API.
|
|
|
|
"""
|
2021-03-16 17:19:00 +08:00
|
|
|
import os
|
|
|
|
from tempfile import NamedTemporaryFile
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2020-09-28 16:09:29 +08:00
|
|
|
from django.core.files.uploadhandler import (
|
|
|
|
FileUploadHandler, StopUpload, TemporaryFileUploadHandler,
|
|
|
|
)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class QuotaUploadHandler(FileUploadHandler):
|
|
|
|
"""
|
|
|
|
This test upload handler terminates the connection if more than a quota
|
|
|
|
(5MB) is uploaded.
|
|
|
|
"""
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
QUOTA = 5 * 2 ** 20 # 5 MB
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def __init__(self, request=None):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(request)
|
2008-07-01 23:10:51 +08:00
|
|
|
self.total_upload = 0
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def receive_data_chunk(self, raw_data, start):
|
|
|
|
self.total_upload += len(raw_data)
|
|
|
|
if self.total_upload >= self.QUOTA:
|
|
|
|
raise StopUpload(connection_reset=True)
|
|
|
|
return raw_data
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def file_complete(self, file_size):
|
2008-08-31 03:56:14 +08:00
|
|
|
return None
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2020-09-28 16:09:29 +08:00
|
|
|
class StopUploadTemporaryFileHandler(TemporaryFileUploadHandler):
|
|
|
|
"""A handler that raises a StopUpload exception."""
|
|
|
|
def receive_data_chunk(self, raw_data, start):
|
|
|
|
raise StopUpload()
|
|
|
|
|
|
|
|
|
2008-08-31 03:56:14 +08:00
|
|
|
class CustomUploadError(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-08-31 03:56:14 +08:00
|
|
|
class ErroringUploadHandler(FileUploadHandler):
|
|
|
|
"""A handler that raises an exception."""
|
|
|
|
def receive_data_chunk(self, raw_data, start):
|
|
|
|
raise CustomUploadError("Oops!")
|
2021-03-16 17:19:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TraversalUploadHandler(FileUploadHandler):
|
|
|
|
"""A handler with potential directory-traversal vulnerability."""
|
|
|
|
def __init__(self, request=None):
|
|
|
|
from .views import UPLOAD_TO
|
|
|
|
|
|
|
|
super().__init__(request)
|
|
|
|
self.upload_dir = UPLOAD_TO
|
|
|
|
|
|
|
|
def file_complete(self, file_size):
|
|
|
|
self.file.seek(0)
|
|
|
|
self.file.size = file_size
|
|
|
|
with open(os.path.join(self.upload_dir, self.file_name), 'wb') as fp:
|
|
|
|
fp.write(self.file.read())
|
|
|
|
return self.file
|
|
|
|
|
|
|
|
def new_file(
|
|
|
|
self, field_name, file_name, content_type, content_length, charset=None,
|
|
|
|
content_type_extra=None,
|
|
|
|
):
|
|
|
|
super().new_file(
|
|
|
|
file_name, file_name, content_length, content_length, charset,
|
|
|
|
content_type_extra,
|
|
|
|
)
|
|
|
|
self.file = NamedTemporaryFile(suffix='.upload', dir=self.upload_dir)
|
|
|
|
|
|
|
|
def receive_data_chunk(self, raw_data, start):
|
|
|
|
self.file.write(raw_data)
|