Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/common.py
Show First 20 Lines • Show All 342 Lines • ▼ Show 20 Lines | def _delete_deposit(self, collection_name: str, deposit_id: int) -> Dict: | ||||
BAD_REQUEST, summary=summary, verbose_description=description | BAD_REQUEST, summary=summary, verbose_description=description | ||||
) | ) | ||||
DepositRequest.objects.filter(deposit=deposit).delete() | DepositRequest.objects.filter(deposit=deposit).delete() | ||||
deposit.delete() | deposit.delete() | ||||
return {} | return {} | ||||
def _check_preconditions_on( | def _check_file_length( | ||||
self, | self, filehandler: UploadedFile, content_length: Optional[int] = None, | ||||
filehandler: UploadedFile, | |||||
md5sum: Optional[bytes], | |||||
content_length: Optional[int] = None, | |||||
) -> None: | ) -> None: | ||||
"""Check preconditions on provided file are respected. That is the | """Check the filehandler passed as argument has exactly the | ||||
length and/or the md5sum hash match the file's content. | expected content_length | ||||
Args: | Args: | ||||
filehandler: The file to check | filehandler: The file to check | ||||
md5sum: md5 hash expected from the file's content | |||||
content_length: the expected length if provided. | content_length: the expected length if provided. | ||||
Returns: | Raises: | ||||
Either none if no error or a dictionary with a key error | DepositError if the actual length does not match | ||||
detailing the problem. | |||||
""" | """ | ||||
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: | if content_length > 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 | length = filehandler.size | ||||
if length != content_length: | if length != content_length: | ||||
raise DepositError(status.HTTP_412_PRECONDITION_FAILED, "Wrong length") | raise DepositError(status.HTTP_412_PRECONDITION_FAILED, "Wrong length") | ||||
def _check_file_md5sum( | |||||
self, filehandler: UploadedFile, md5sum: Optional[bytes], | |||||
) -> None: | |||||
"""Check the filehandler passed as argument has the expected md5sum | |||||
Args: | |||||
filehandler: The file to check | |||||
md5sum: md5 hash expected from the file's content | |||||
Raises: | |||||
DepositError if the md5sum does not match | |||||
""" | |||||
if md5sum: | if md5sum: | ||||
_md5sum = _compute_md5(filehandler) | _md5sum = _compute_md5(filehandler) | ||||
if _md5sum != md5sum: | if _md5sum != md5sum: | ||||
raise DepositError( | raise DepositError( | ||||
CHECKSUM_MISMATCH, | CHECKSUM_MISMATCH, | ||||
"Wrong md5 hash", | "Wrong md5 hash", | ||||
f"The checksum sent {hashutil.hash_to_hex(md5sum)} and the actual " | f"The checksum sent {hashutil.hash_to_hex(md5sum)} and the actual " | ||||
f"checksum {hashutil.hash_to_hex(_md5sum)} does not match.", | f"checksum {hashutil.hash_to_hex(_md5sum)} does not match.", | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | ) -> Receipt: | ||||
BAD_REQUEST, | BAD_REQUEST, | ||||
f"Only packaging {ACCEPT_PACKAGINGS} is supported", | f"Only packaging {ACCEPT_PACKAGINGS} is supported", | ||||
f"The packaging provided {packaging} is not supported", | f"The packaging provided {packaging} is not supported", | ||||
) | ) | ||||
filehandler = request.FILES["file"] | filehandler = request.FILES["file"] | ||||
assert isinstance(filehandler, UploadedFile), filehandler | assert isinstance(filehandler, UploadedFile), filehandler | ||||
self._check_preconditions_on( | self._check_file_length(filehandler, content_length) | ||||
filehandler, headers.content_md5sum, content_length | self._check_file_md5sum(filehandler, headers.content_md5sum) | ||||
) | |||||
slug = headers.slug | slug = headers.slug | ||||
if check_slug_is_present and not slug: | if check_slug_is_present and not slug: | ||||
raise_missing_slug_error() | raise_missing_slug_error() | ||||
# actual storage of data | # actual storage of data | ||||
archive_metadata = filehandler | archive_metadata = filehandler | ||||
deposit = self._deposit_put( | deposit = self._deposit_put( | ||||
▲ Show 20 Lines • Show All 108 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_preconditions_on(filehandler, headers.content_md5sum) | self._check_file_md5sum(filehandler, headers.content_md5sum) | ||||
ardumont: why not both checks as before. | |||||
Done Inline Actionsbecause it didn't check both before. content_length was None, so it was not checked. vlorentz: because it didn't check both before. content_length was None, so it was not checked. | |||||
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", | ||||
"The xml received is malformed. " | "The xml received is malformed. " | ||||
▲ Show 20 Lines • Show All 552 Lines • Show Last 20 Lines |
why not both checks as before.