diff --git a/swh/deposit/api/common.py b/swh/deposit/api/common.py --- a/swh/deposit/api/common.py +++ b/swh/deposit/api/common.py @@ -10,6 +10,7 @@ from typing import Any, Dict, Optional, Sequence, Tuple, Type, Union import attr +from django.core.files.uploadedfile import InMemoryUploadedFile from django.http import FileResponse, HttpResponse from django.shortcuts import render from django.urls import reverse @@ -88,6 +89,13 @@ swhid = attr.ib(type=Optional[str]) +def _compute_md5(filehandler: InMemoryUploadedFile) -> bytes: + h = hashlib.md5() + for chunk in filehandler: + h.update(chunk) # type: ignore + return h.digest() + + class AuthenticatedAPIView(APIView): """Mixin intended as a based API view to enforce the basic authentication check @@ -150,22 +158,6 @@ swhid=meta.get("HTTP_X_CHECK_SWHID"), ) - def _compute_md5(self, filehandler) -> bytes: - """Compute uploaded file's md5 sum. - - Args: - filehandler (InMemoryUploadedFile): the file to compute the md5 - hash - - Returns: - the md5 checksum (str) - - """ - h = hashlib.md5() - for chunk in filehandler: - h.update(chunk) - return h.digest() - def _deposit_put( self, request: Request, @@ -371,7 +363,7 @@ ) if md5sum: - _md5sum = self._compute_md5(filehandler) + _md5sum = _compute_md5(filehandler) if _md5sum != md5sum: return make_error_dict( CHECKSUM_MISMATCH,