Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/common.py
Show First 20 Lines • Show All 357 Lines • ▼ Show 20 Lines | ) -> None: | ||||
filehandler: The file to check | filehandler: The file to check | ||||
content_length: the expected length if provided. | content_length: the expected length if provided. | ||||
Raises: | Raises: | ||||
DepositError if the actual length does not match | DepositError if the actual length does not match | ||||
""" | """ | ||||
max_upload_size = self.config["max_upload_size"] | max_upload_size = self.config["max_upload_size"] | ||||
if content_length: | if content_length: | ||||
if content_length > max_upload_size: | length = filehandler.size | ||||
if length != content_length: | |||||
raise DepositError(status.HTTP_412_PRECONDITION_FAILED, "Wrong length") | |||||
if filehandler.size > max_upload_size: | |||||
raise DepositError( | raise DepositError( | ||||
MAX_UPLOAD_SIZE_EXCEEDED, | MAX_UPLOAD_SIZE_EXCEEDED, | ||||
f"Upload size limit exceeded (max {max_upload_size} bytes)." | f"Upload size limit exceeded (max {max_upload_size} bytes)." | ||||
"Please consider sending the archive in multiple steps.", | "Please consider sending the archive in multiple steps.", | ||||
) | ) | ||||
length = filehandler.size | |||||
if length != content_length: | |||||
raise DepositError(status.HTTP_412_PRECONDITION_FAILED, "Wrong length") | |||||
def _check_file_md5sum( | def _check_file_md5sum( | ||||
self, filehandler: UploadedFile, md5sum: Optional[bytes], | self, filehandler: UploadedFile, md5sum: Optional[bytes], | ||||
) -> None: | ) -> None: | ||||
"""Check the filehandler passed as argument has the expected md5sum | """Check the filehandler passed as argument has the expected md5sum | ||||
Args: | Args: | ||||
filehandler: The file to check | filehandler: The file to check | ||||
md5sum: md5 hash expected from the file's content | md5sum: md5 hash expected from the file's content | ||||
▲ Show 20 Lines • Show All 200 Lines • ▼ Show 20 Lines | ) -> Receipt: | ||||
) | ) | ||||
filehandler = data["application/zip"] | filehandler = data["application/zip"] | ||||
if not filehandler: | if not filehandler: | ||||
filehandler = data["application/x-tar"] | filehandler = data["application/x-tar"] | ||||
assert isinstance(filehandler, UploadedFile), filehandler | assert isinstance(filehandler, UploadedFile), filehandler | ||||
self._check_file_length(filehandler) | |||||
self._check_file_md5sum(filehandler, headers.content_md5sum) | self._check_file_md5sum(filehandler, headers.content_md5sum) | ||||
try: | try: | ||||
raw_metadata, metadata = self._read_metadata(data["application/atom+xml"]) | raw_metadata, metadata = self._read_metadata(data["application/atom+xml"]) | ||||
except ParserError: | except ParserError: | ||||
raise DepositError( | raise DepositError( | ||||
PARSING_ERROR, | PARSING_ERROR, | ||||
"Malformed xml metadata", | "Malformed xml metadata", | ||||
▲ Show 20 Lines • Show All 553 Lines • Show Last 20 Lines |