Skip to content

Commit 084fb8e

Browse files
committed
🐛(back) fix management command to rename deposited files
After some tests on preprod, the management commands was not working as some deposited files have an extension in their filename, but not in their object storage key. Fixing the command by using the extension field instead of computing the extension from the filename.
1 parent 17364de commit 084fb8e

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

src/backend/marsha/deposit/management/commands/rename_deposited_files.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Rename deposited files in Scaleway S3 for serving them with the right name."""
22

33
import logging
4-
from os.path import splitext
54

65
from django.conf import settings
76
from django.core.management.base import BaseCommand
@@ -51,9 +50,7 @@ def handle(self, *args, **options):
5150
for file in files:
5251
# Get the file stored on Scaleway S3 under `aws/`
5352
stamp = time_utils.to_timestamp(file.uploaded_on)
54-
extension = ""
55-
if "." in file.filename:
56-
extension = splitext(file.filename)[1]
53+
extension = "." + file.extension if file.extension else ""
5754

5855
file_key_src = file.get_storage_key(
5956
filename=f"{stamp}{extension}", base_dir=AWS_STORAGE_BASE_DIRECTORY

src/backend/marsha/deposit/tests/test_command_rename_deposited_files.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Test the ``rename_deposited_files`` management command."""
22

33
from datetime import datetime, timezone
4-
from os.path import splitext
54
from unittest import mock
65

76
from django.core.management import call_command
@@ -43,6 +42,7 @@ def test_rename_deposited_files(self, mock_exists):
4342
for filename_src, _ in filenames:
4443
file = DepositedFileFactory(
4544
filename=filename_src,
45+
extension="pdf",
4646
uploaded_on=now,
4747
upload_state=READY,
4848
storage_location=AWS_S3,
@@ -54,9 +54,7 @@ def test_rename_deposited_files(self, mock_exists):
5454
# were created, so we must iterate over objects.all() in the same sequence
5555
for file in DepositedFile.objects.all():
5656
stamp = time_utils.to_timestamp(file.uploaded_on)
57-
extension = ""
58-
if "." in file.filename:
59-
extension = splitext(file.filename)[1]
57+
extension = "." + file.extension if file.extension else ""
6058

6159
file_key_src = (
6260
f"aws/{file.file_depository.id}/depositedfile/"

0 commit comments

Comments
 (0)