diff --git a/swh/deposit/models.py b/swh/deposit/models.py --- a/swh/deposit/models.py +++ b/swh/deposit/models.py @@ -7,6 +7,7 @@ # cd swh_deposit && \ # python3 -m manage inspectdb +import datetime from django.contrib.postgres.fields import JSONField, ArrayField from django.contrib.auth.models import User, UserManager @@ -161,19 +162,25 @@ return "%s/%s" % (self.client.provider_url.rstrip("/"), self.external_id) -def client_directory_path(instance, filename): - """Callable to upload archive in MEDIA_ROOT/user_/ +def client_directory_path(instance: "DepositRequest", filename: str) -> str: + """Callable to determine the upload archive path. This defaults to + MEDIA_ROOT/client_/%Y%m%d-%H%M%S.%f/. + + The format "%Y%m%d-%H%M%S.%f" is the reception date of the associated deposit + formatted using strftime. Args: - instance (DepositRequest): DepositRequest concerned by the upload - filename (str): Filename of the uploaded file + instance: DepositRequest concerned by the upload + filename: Filename of the uploaded file Returns: - A path to be prefixed by the MEDIA_ROOT to access physically - to the file uploaded. + The upload archive path. """ - return "client_{0}/{1}".format(instance.deposit.client.id, filename) + reception_date = instance.deposit.reception_date + assert isinstance(reception_date, datetime.datetime) + folder = reception_date.strftime("%Y%m%d-%H%M%S.%f") + return f"client_{instance.deposit.client.id}/{folder}/{filename}" REQUEST_TYPES = [(ARCHIVE_TYPE, ARCHIVE_TYPE), (METADATA_TYPE, METADATA_TYPE)] diff --git a/swh/deposit/tests/common.py b/swh/deposit/tests/common.py --- a/swh/deposit/tests/common.py +++ b/swh/deposit/tests/common.py @@ -133,6 +133,11 @@ archive_name_to_check """ + ARCHIVE_FILEPATH_PATTERN = re.compile( + r"client_[0-9].*/[0-9]{8}-[0-9]{6}\.[0-9]{6}/[a-zA-Z0-9.].*" + ) + assert ARCHIVE_FILEPATH_PATTERN.match(archive_name_to_check) + if "." in archive_name: filename, extension = archive_name.split(".") pattern = re.compile(".*/%s.*\\.%s" % (filename, extension)) diff --git a/swh/deposit/tests/test_common.py b/swh/deposit/tests/test_common.py --- a/swh/deposit/tests/test_common.py +++ b/swh/deposit/tests/test_common.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019 The Software Heritage developers +# Copyright (C) 2019-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -11,13 +11,15 @@ def test_check_archive_helper(): # success for archive_name, archive_name_to_check in [ - ("filename0", "something/filename0"), - ("archive.zip", "client_1/archive_noisynoise.zip"), + ("filename0", "client_666/20200601-092624.421886/filename0.zip"), + ("archive", "client_007/20190601-092624.532978/archive"), ]: check_archive(archive_name, archive_name_to_check) # failures for archive_name, archive_name_to_check in [ + ("filename0", "something/filename0"), + ("archive.zip", "client_1/archive_noisynoise.zip"), ("filename0", "something-filename0"), ("archive.zip", "client_1_archive_noisynoise.zip"), ("reference", "irrelevant"),